I have a foreach loop that cycles through a list of types and creates an instance of each one. However, when I build, it gives a CS0246 error ("The type or namespace could not be found ... "). Here's a simplified version of the code:
internal static class TypeManager
{
internal static void LoadTypes()
{
// Fill the list with types
// Create instances of each type
foreach (Type currType in Types)
{
Type aType = currType; // comiles fine
Object newObj = (currType)Activator.CreateInstance<currType>; // CS 0246
}
}
public static List<Type> Types;
}
Edit: Follow-up question
My foreach loop now looks like this:
foreach (Type currType in Types)
{
Types.Add((Type)Activator.CreateInstance(currType));
}
with Types List now being of type Object
This compiles fine, but when I run it, I get the following:
Object reference not set to an instance of an object.
If I break this up into two lines that first creates an object then adds it to the List, the first line is fine (the object is created successfully), but it gives me the same error message.
Edit: Update code sample
internal static LoadPlugins()
{
foreach (Type currType in pluginAssembly.GetTypes())
{
if (typeof(IPlugin).IsAssignableFrom(currType))
{
Assembly.LoadFrom(currFile.FullName);
Object pluginInstance = Activator.CreateInstance(currType); // Compiles and runs fine
Plugins.Add((IPlugin)pluginInstance); // NullReferenceException
break;
}
}
}
public static List<IPlugin> Plugins;