Is it possible in a .NET app (C#), to conditionally detect if a class is defined at runtime?
Sample implementation - say you want to create a class object based on a configuration option?
Is it possible in a .NET app (C#), to conditionally detect if a class is defined at runtime?
Sample implementation - say you want to create a class object based on a configuration option?
For the second part of your question :-
I dont know why you want to do that. However, If your classes implement an interface and you want to dynamically create objects of those classes based on configuration files, I think you can look at Unity IoC container. Its really cool and very easy to use If it fits your scenario. An example on how to do that is here.
Activator.CreateInstance may fit the bill:
http://msdn.microsoft.com/en-us/library/system.activator.createinstance.aspx
Of course, it throws an exception if you can't instantiate the class, which isn't exactly the same thing as whether the class "exists". But if you can't instantiate it and you aren't looking to just call static members, it should do the trick for you.
You're probably looking for the overload that has string parameters, the first argument should be the name of the assembly, the second being the name of the class (fully namespace-qualified).
I've done something like that, load a class from the Config and instantiate it. In this example, I needed to make sure the class specified in the config inherited from a class called NinjectModule, but I think you get the idea.