使用MongoDb存储System.Type

时间:2012-01-19 17:23:06

标签: mongodb mongodb-.net-driver

当我存储这个课程时:

class MyClass{
    ...
    public Type SomeType {get;set;} 
    ...
}

SomeType属性被序列化为:

"SomeType" : {
    "_t" : "RuntimeType"
}

并且每个后续查询都失败。

我正在使用官方的C#驱动程序。如何让它存储实际类型? 感谢。

2 个答案:

答案 0 :(得分:7)

这是System.Type的示例序列化程序,它将Type的名称序列化为BSON字符串。这有一些限制,如果类型名称不是系统类型或在同一程序集中,则Deserialize方法会失败,但您可以调整此示例序列化程序以编写AssemblyQualifiedName。

public class TypeSerializer : IBsonSerializer
{
    public object Deserialize(BsonReader reader, Type nominalType, IBsonSerializationOptions options)
    {
        var actualType = nominalType;
        return Deserialize(reader, nominalType, actualType, options);
    }

    public object Deserialize(BsonReader reader, Type nominalType, Type actualType, IBsonSerializationOptions options)
    {
        if (reader.CurrentBsonType == BsonType.Null)
        {
            return null;
        }
        else
        {
            var fullName = reader.ReadString();
            return Type.GetType(fullName);
        }
    }

    public bool GetDocumentId(object document, out object id, out Type idNominalType, out IIdGenerator idGenerator)
    {
        throw new InvalidOperationException();
    }

    public void Serialize(BsonWriter writer, Type nominalType, object value, IBsonSerializationOptions options)
    {
        if (value == null)
        {
            writer.WriteNull();
        }
        else
        {
            writer.WriteString(((Type)value).FullName);
        }
    }

    public void SetDocumentId(object document, object id)
    {
        throw new InvalidOperationException();
    }
}

诀窍是让它正确注册。您需要为System.Type和System.RuntimeType注册它,但System.RuntimeType不是公共的,因此您不能在代码中引用它。但是你可以使用Type.GetType来实现它。这是注册序列化器的代码:

var typeSerializer = new TypeSerializer();
BsonSerializer.RegisterSerializer(typeof(Type), typeSerializer);
BsonSerializer.RegisterSerializer(Type.GetType("System.RuntimeType"), typeSerializer);

我使用此测试循环来验证它是否有效:

var types = new Type[] { typeof(int), typeof(string), typeof(Guid), typeof(C) };
foreach (var type in types)
{
    var json = type.ToJson();
    Console.WriteLine(json);
    var rehydratedType = BsonSerializer.Deserialize<Type>(json);
    Console.WriteLine("{0} -> {1}", type.FullName, rehydratedType.FullName);
}

其中C只是一个空类:

public static class C
{
}

答案 1 :(得分:2)

不支持序列化System.Type(至少目前不支持)。您必须将类型名称存储为字符串。

或者,你可以为System.Type编写一个序列化程序并注册它,但这可能比简单地将类型名称存储为字符串更有用。

相关问题