Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7164461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:04:45+00:00 2026-05-28T14:04:45+00:00

When I store this class: class MyClass{ … public Type SomeType {get;set;} … }

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:04:46+00:00Added an answer on May 28, 2026 at 2:04 pm

    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.

    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();
        }
    }
    

    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:

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

    I used this test loop to verify that it worked:

    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);
    }
    

    where C is just an empty class:

    public static class C
    {
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class which looks something like this: class MyClass { public: //
I want to store different types in a Map like this: public Map< String,
Suppose I have a template class MyClass. Is it possible to store the type
I have a class like this class MyClass { int Identifier; int Context; int
I have this class: public class MyClass { [Column(Name=StoredColumn, DbType=int)] public int Stored; public
I have this algorithm in Java to store passwords in database. I'd like to
So I have this App in the store, and I would like to release
I have a PersistenceCapable class @PersistenceCapable(identityType = IdentityType.APPLICATION) public class MyClass { @PrimaryKey @Persistent(valueStrategy
class MyClass { public: void setVar(const char *str); private: std::string mStr; int maxLength; //we
I have a class, class MyClass { private int val; public static final MyClass

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.