这是因为您正在尝试序列化System.Type
. 该类型有很多属性,其中一些会抛出您所看到的异常。这在问题 #212上进行了讨论,尽管在这种情况下修复是完全避免序列化System.Type
.
理想情况下,您将能够注册一个自定义类型转换器以System.Type
将其作为字符串处理和序列化,但由于对象图遍历方式的缺陷,仍将访问该属性。
您最好的解决方案可能是在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 };
}
}
编辑
提到的问题已经修复。如果您尝试最新的预发布包,您将能够通过注册自定义来实现您想要的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);