I have to be able to take the name of a class, as a String
and use that name to extract from the application's IServiceProvider
collection an instance of the appropriate class. The classes that will be created are all injected in to the IServiceCollection
at the application start up and have other services injected into their constructors.
If we assume access to a list of types constructed from a variety calls to Assembly.GetExportedTypes()
, I can get the Type
I need using something like
var type = myListOfServices.FirstOrDefault(_=>_.FullName == "My.Qualified.ClassName");
And I can get an instance of the service using
var instance = serviceProvider.GetService(type);
The problem is that instance
is of type object
and I have no idea how to cast it to the actual type so that I can access its properties and methods.
UPDATE
The classes I want to create instances of have methods that I need to be able to call and, therefore, I need to be able to cast the object
returned by GetService()
to the specific type so I can access the methods.
Whilst I have the Type in a variable (see my example above) you cannot use the variable to case the object...
var attempt1 = (type)instance; // won't compile
var attempt2 = instance as type; // won't compile
The point is that the instance I'm trying to get at could be one of any number of classes but all I know about it is the name (this is being supplied as a string message)
The suggestions of How to cast Object to its actual type? aren't appropriate because the answers there assume you know the Type, I'm having to work out what the Type at runtime