如何将C#中的Type类对象序列化和反序列化为YAML?

时间:2016-11-23 10:40:35

标签: c# serialization deserialization yaml yamldotnet

我有一个要求,我必须将以下类型层次序列化为YAML

var Variable1 = new
{
    Name = "Variable1",
    Type = typeof(Int32),
    OverWrite = true,
    Value = 10
};
var Variable2 = new
{
    Name = "Variable1",
    Type = typeof(Int32),
    OverWrite = true,
    Value = 10
};

var Job = new
{
    Name = "Job1",
    JobID = 1,
    JobState = "Draft",
    JobStatus = false,
    Parameters = new[]
    {
        Variable1,
        Variable2
    },
    LocalVariables = new[]
    {
        Variable1
    }
};

我在这里得到一个例外

An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll

附加信息:调用目标抛出了异常。{"只能在Type.IsGenericParameter为true的Type上调用Method。"}

请帮忙!!

1 个答案:

答案 0 :(得分:1)

这是因为您正在尝试序列化System.Type。该类型具有许多属性,其中一些属性抛出了您所看到的异常。这在issue #212上进行了讨论,但在这种情况下,修复是为了完全避免序列化System.Type

理想情况下,您可以注册一个自定义类型转换器来处理System.Type并将其序列化为字符串,但由于a shortcoming with the way the object graph is traversed,该属性仍将被访问。

您最好的解决方案可能是将System.Type包装在一个自定义类中,该类可以根据需要进行序列化:

public struct SerializableType : IYamlConvertible
{
    private Type type;

    void IYamlConvertible.Read(IParser parser, Type expectedType, ObjectDeserializer nestedObjectDeserializer)
    {
        var typeName = (string)nestedObjectDeserializer(typeof(string));
        type = typeName != null ? Type.GetType(typeName) : null;
    }

    void IYamlConvertible.Write(IEmitter emitter, ObjectSerializer nestedObjectSerializer)
    {
        nestedObjectSerializer(type != null ? type.AssemblyQualifiedName : null);
    }

    public static implicit operator Type(SerializableType value)
    {
        return value.type;
    }

    public static implicit operator SerializableType(Type value)
    {
        return new SerializableType { type = value };
    }
}

修改

上述问题已得到修复。如果您尝试the latest pre-release package,则可以通过注册自定义IYamlTypeConverter来实现您的目标:

public class SystemTypeTypeConverter : IYamlTypeConverter
{
    public bool Accepts(Type type)
    {
        return typeof(Type).IsAssignableFrom(type);
    }

    public object ReadYaml(IParser parser, Type type)
    {
        var scalar = parser.Expect<Scalar>();
        return Type.GetType(scalar.Value);
    }

    public void WriteYaml(IEmitter emitter, object value, Type type)
    {
        var typeName = ((Type)value).AssemblyQualifiedName;
        emitter.Emit(new Scalar(typeName));
    }
}

// ....

var serializer = new SerializerBuilder()
    .WithTypeConverter(new SystemTypeTypeConverter())
    .Build();

var yaml = serializer.Serialize(new TypeContainer
{
    Type = typeof(string),
});

var deserializer = new DeserializerBuilder()
    .WithTypeConverter(new SystemTypeTypeConverter())
    .Build();

var result = deserializer.Deserialize<TypeContainer>(yaml);

Assert.Equal(typeof(string), result.Type);