When I store this class:
class MyClass{
...
public Type SomeType {get;set;}
...
}
SomeType property gets serialized like this:
"SomeType" : {
"_t" : "RuntimeType"
}
and every subsequent query fails.
I’m using the official C# driver. How do I get it to store the actual Type?
Thanks.
Here’s a sample serializer for System.Type that serializes the name of the Type as a BSON string. This has some limitations in that the Deserialize method fails if the type name is not a system type or in the same assembly, but you could tweak this sample serializer to write the AssemblyQualifiedName instead.
The trick is to get it registered properly. You need to register it for both System.Type and System.RuntimeType, but System.RuntimeType is not public so you can’t refer to it in your code. But you can get at it using Type.GetType. Here’s the code to register the serializer:
I used this test loop to verify that it worked:
where C is just an empty class: