How to determine the existence of a class at runtime in .NET?

Is it possible in a .NET application (C #) to conditionally determine if a class is defined at run time?

Implementation example - let's say you want to create a class object based on a configuration parameter?

+4
source share
3 answers

I did something like this , load the class from Config and create it. In this example, I needed to make sure that the class specified in the config was inherited from the NinjectModule class, but I think you get the idea.

protected override IKernel CreateKernel() { // The name of the class, eg 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 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 StandardKernel return new StandardKernel(module); } 
+2
source
 string className="SomeClass"; Type type=Type.GetType(className); if(type!=null) { //class with the given name exists } 

For the second part of your question: -

Implementation Example - Tell me, what do you want to create a class object based on a configuration option?

I do not know why you want to do this. However, if your classes implement the interface and you want to dynamically create objects of these classes based on configuration files, I think you can look at the Unity IoC container . Its really cool and very easy to use if it fits your scenario. An example of how to do this is here .

+3
source

Activator.CreateInstance may correspond to the score:

http://msdn.microsoft.com/en-us/library/system.activator.createinstance.aspx

Of course, this throws an exception if you cannot create an instance of the class, which is not exactly the same as the class "exists". But if you cannot create an instance, and you do not want to just name static members, it should do the trick for you.

You are probably looking for overload with string parameters, the first argument should be the name of the assembly, the second should be the name of the class (full namespace).

0
source

Source: https://habr.com/ru/post/1305288/


All Articles