2017-03-04 9 views
1

방금 ​​Yaml과 작업하기 시작했고, 약간의 의견을 정말 고맙게 생각합니다. 나는 YAML을 만들고 그것을 기존의 C# 클래스로 황폐화하려고 노력 중이다. 기존 C# 클래스 : 내가 YAML 위를 deserialze 할 때Yaml을 C# 객체로 역 직렬화

[System.Xml.Serialization.XmlIncludeAttribute(typeof(FooType))] 

    public partial class BarType { 

     private int barVariable; 

     public Int Bar { 
     get { 
      return this.barVariable; 
     } 
     set { 
      this.barVariable = value; 
     } 
    } 


    } 

    public partial class FooType : BarType { 

     private string fooVariable; 
     public string Foo { 
     get { 
      return this.fooVariable; 
     } 
     set { 
      this.fooVariable = value; 
     } 
    } 


[System.Xml.Serialization.XmlRootAttribute("HeadType", Namespace="xyz", IsNullable=false)] 

public partial class HeadType { 

    private BarType[] barTypesField; 

    [System.Xml.Serialization.XmlArrayItemAttribute("FooService", typeof(FooType), IsNullable=false)] 

     public BarType[] BarTypes { 
      get { 
       return this.barTypesField; 
       } 
      set { 
       this.barTypesField = value; 
       } 
      } 

지금 내가 어떤 오류가 발생하지 마십시오 YAML,

HeadType: 
    - Bar: 0 
    - Bar: 29 

있습니다.

하지만 내 Yaml을 아래와 같이 변경하면 Foo라는 태그에 대해 알지 못합니다.

HeadType: 
    - Bar: 0 
    Foo: FooTest 

내가이 방법을 사용할 수 있습니까? 내가 YAML 순 직렬화 "YamlDotNet.Serialization을"점 사용하고

HeadType: 
    FooType: 
    - Bar: 0 
     Foo: FooTest 

이 직렬화가 작동하는 방법은 다음과 같습니다 :

는 루트 클래스
Deserializer deserializer = new Deserializer(); 
    var result = deserializer.Deserialize<RootType>(yamlInput1); 

어디도 doesnot 일하는 아래를 시도했다 HeadType을 포함합니다.

답변

0

당신이 태그를 사용하는 것입니다 입력을 알려줄 필요가 있으므로,

HeadType: 
    - !bar 
    Bar: 0 
    - !foo 
    Bar: 0 
    Foo: FooTest 

YamlDotNet 콘텐츠 존재에 따라 실제 유형을 추론하지 않습니다 사용해보십시오. 로드 할 때이 태그를 디시리얼라이저에 등록해야합니다.

deserializer.RegisterTagMapping("!bar", typeof(BarType)); 
deserializer.RegisterTagMapping("!foo", typeof(FooType));