I would like to be able to verify that a class exists within the current assembly before attempting to create it using Activator.CreateInstance.
Currently I build a fully qualified class name based on some meta-data retrieved from a 3rd party application I am extending. This class name is then created using Activator.CreateInstance and cast to a common base class.
I'm looking for something along the lines of:
string className = "MyNamespace." + "some_arbitary_class_name";
if (Assembly.ClassExists(className))
{
// Create the class.
}
Currently I have this:
string class_name = "MyNamespace.BaseClassImpl_" + meta_data;
MyBaseClass baseClass = null;
try
{
baseClass = (BaseClass)Activator.CreateInstance(null, class_name);
}
catch (Exception e) { }
// If base class in null, failed.
I know that I need to use System.Reflection to inspect the current assembly but I'm at a loss of the exact methods I need to use.
So my question is twofold:
- Could someone please direct me to the .NET api I need to use to accomplish this?
- Is it good practice to verify that a class exists before creating it? Or should I just let the
try-catchblock handle it?