Defining an unspecified type to a static variable inside of a class

I am making a simple and specific file for use in several of my projects so I need to be able to simply 'Plug and play' so-to-say.

Working with MongoDB (I don't want comments about swapping to different storage solutions.), I am fetching a collection with type to cast to, however storing it within the actual class locally rather than passing it as a return method.

public static IMongoCollection<object> ConnectedCollection;
public async Task Connect<TDoc>(string ConnectionString, string Database, string Collection)
{
    //Do some stuff here;
    //some more stuff;
    //Get collection here: 
    var MongoCollection = ConnectedDatabase.GetCollection<TDoc>(Collection);
    /// MongoCollection returns a collection of TDoc, as specified within the method,
    /// now to cast it to a static variable defined outside of the method
    ConnectedCollection = MongoCollection;
}

For obvious reasons it can't be object, I need to have ConnectedCollection be of type IMongoCollection<TDoc> however, as it is not an actual class but a unspecified type being cast into the method, it doesn't exist, How would I allow it to remain as a static collection within, without creating an accessible class for this file.



Read more here: https://stackoverflow.com/questions/68460395/defining-an-unspecified-type-to-a-static-variable-inside-of-a-class

Content Attribution

This content was originally published by michaelukz at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: