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

  • SEARCH
  • Home
  • 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 7836255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T14:03:18+00:00 2026-06-02T14:03:18+00:00

I am building an application that uses Mongo and the offical C# driver. I

  • 0

I am building an application that uses Mongo and the offical C# driver. I am having an odd problem when using interfaces and generic type constraints. Consider the following:

interface IMongoObject
{
    BsonObjectId Id { get; set; }
    string Name { get; set; }
}

class MongoObject : IMongoObject
{
    public BsonObjectId Id { get; set; }
    public string Name { get; set; }
}

class Program
{
    MongoServer m_db;
    string m_dbName;

    protected void Save<T>(T entity) where T : IMongoObject
    {
        GetDatabase().GetCollection<T>(typeof(T).Name).Save(entity);
    }

    protected T Get<T>(string name) where T : IMongoObject
    {
        Type t = typeof(T);

        // Throws
        return GetDatabase().GetCollection<T>(typeof(T).Name).AsQueryable().Where(o => o.Name == name).FirstOrDefault();
    }

    protected MongoDatabase GetDatabase()
    {
        if (m_db == null)
        {
            var conString = "mongodb://localhost/MongoTest";
            MongoUrl url = new MongoUrl(conString);
            m_dbName = url.DatabaseName;
            m_db = MongoServer.Create(url);
        }

        return m_db.GetDatabase(m_dbName);
    }

    void Run()
    {
        MongoObject o = new MongoObject();
        o.Name = "Foo";
        Save(o);

        MongoObject b = Get<MongoObject>("Foo");
        Console.ReadKey();
    }

    static void Main(string[] args)
    {
        Program p = new Program();
        p.Run();
    }
}

Everything works fine, apart from the call to Get(…). Using the offical driver, I get a null reference exception from the call var memberSerializationInfo = containingSerializationInfo.Serializer.GetMemberSerializationInfo(memberName); in the function private BsonSerializationInfo GetSerializationInfoMember(IBsonSerializer serializer, MemberExpression memberExpression) in SelectQuery.cs.

If I switch to using FluentMongo for LINQ, I get a message saying

“Discriminators can only be registered for classes, not for interface MyLib.Services.IRepoData.”

This is basically the same as this guy:

FluentMongo throwing error all of a sudden

I understand that the Mongo driver is complaining because it doesnt know what IMongoObject is. What I dont understand is why the expression within the where call is seeing “o” as type IMongoObject not of type MongoObject (confirmed with debugger this is whats happening), which is what it is.

If I switch to using an abstract base class, it all works fine. I dont what to do this because not all “MongoObjects”

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-06-02T14:03:23+00:00Added an answer on June 2, 2026 at 2:03 pm

    So, the issue actually lies in how the C# compiler is putting together the expression tree for the object. Because of your type constraint, it is casting your MongoObject to an IMongoObject when accessing the Name property. Hence, IMongoObject is the type of the expression and we can’t find any members on IMongoObject. I’ll see if there is something we can do for this circumstance in the next version of the driver, but for now, I’d stick with abstract classes.

    On a separate note, it might be best to not mix your “domain” objects with your DTOs. Instead, have 2, one for each responsibility and map between them.

    –UPDATE–
    Interesting note. Apparently, this has been fixed based on a different issue with how VB.NET creates expression trees differently that c#. So, in the near future, this will work correctly. You can see the test proving this here: https://github.com/craiggwilson/mongo-csharp-driver/commit/153b9862b122521eee681a86e56806d94fed8b21#diff-1.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am building an application using OpenCV that uses the webcam and runs some
I'm building an application that uses an elaborate API to fetch data from Youtube,
I am building a C++ application that uses Intel's IPP library. This library is
We're building a plugin for a commercial application that uses a recent version of
I have an application that uses the .NET framework 3.5. I am building this
I am building an AJAX application that uses both HTTP Content and HTTP Header
I am building a C# application that uses GDI+ to draw images and shapes
I am building an application in Cocoa that uses a local Ruby on Rails
I am currently building a Rails 3.2 application that uses the gem sunspot to
We're building a latency-sensitive web application that uses websockets (or a Flash fallback) for

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.