views:

89

answers:

3

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?

A: 

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).

richardtallent
MSDN states that Actiator.CreateInstance() is one of the "costly function" - http://msdn.microsoft.com/en-us/magazine/cc163759.aspx.Also, I am not sure why you would want to throw an exception for this as throwing an exception may also be costly?
ydobonmai
"Costly" is relative. If you only have co do this a few times, it's not that expensive. If you're doing it millions of times, you'll want to find a better solution.
Gabe
+1  A: 
string className="SomeClass";
Type type=Type.GetType(className);
if(type!=null)
{
//class with the given name exists
}

For the second part of your question :-

Sample implementation - say you want to create a class object based on a configuration option?

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.

ydobonmai
@Ashish: creating an instance from configuration is one of the things that Unity does.
John Saunders
@John, right. I am aware that It is mainly used for dependency injection. I was just suggesting that he just have a look at it. May not be an answer to the question. Thanks, anyway.
ydobonmai
+1  A: 

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.

protected override IKernel CreateKernel()
{
    // The name of the class, e.g. retrieved from a config
    string moduleName = "MyApp.MyAppTestNinjectModule";

    // Type.GetType takes a string and tries to find a Type with
    // the *fully qualified name* - which includes the Namespace
    // and possibly also the Assembly if it's in another assembly
    Type moduleType = Type.GetType(moduleName);

    // If Type.GetType can't find the type, it returns Null
    NinjectModule module;
    if (moduleType != null)
    {
        // Activator.CreateInstance calls the parameterless constructor
        // of the given Type to create an instace. As this returns object
        // you need to cast it to the desired type, NinjectModule
        module = Activator.CreateInstance(moduleType) as NinjectModule;
    }
    else
    {
        // If the Type was not found, you need to handle that. You could instead
        // initialize Module through some default type, for example
        // module = new MyAppDefaultNinjectModule();
        // or error out - whatever suits your needs
        throw new MyAppConfigException(
             string.Format("Could not find Type: '{0}'", moduleName),
             "injectModule");
    }

    // As module is an instance of a NinjectModule (or derived) class, we
    // can use it to create Ninject's StandardKernel
    return new StandardKernel(module);
}
Michael Stum