Need help with Activator.CreateInstance()

G

Guest

I'm trying to dynamically create an instance of a class, but it's always
failing. I'm using the Activator.CreateInstance( Type, Object[] ) overload.

The class I'm trying to instaniate and call a method from is a public class
from another project in the solution.

The Assembly is loaded fine, but whenever I try to instaniate the class, I
get a MissingMethodException exception, stating that, "Constructor on type
'...' not found". The class is found in the list of classes, and the method
is found in the list of methods.

On step-through debugging, the parameters I pass to the CreateInstance()
method are correct -- that is, the first parameter is the correct class type
and the second parameter is an array of objects that match the only public
constructor of the class I'm trying to instantiate.

Does anyone have any idea why, when everything seems correct, I'm still
getting this exception?
 
G

Guest

Here's an example of what I'm doing: It's not the real code, but it's a
parallel, simplified version. Everything is part of the same solution.

//In Project 1:
public class Info
{
// ...
}

public class AssemblyLoader
{
//...
public bool LoadAssembly(
string assemblyName,
string assemblyPath,
string className,
object[] constructorParams,
string methodName,
object[] methodParams) {

bool success = false;
BindingFlags flags = (BindingFlags.NonPublic |
BindingFlags.Public |
BindingFlags.Static | BindingFlags.Instance |
BindingFlags.DeclaredOnly);

if ((AssemblyName == "") ||
(ClassName == "") ||
(MethodName == ""))
{
return success;
}

try
{
assemblyToLoad = Assembly.LoadFrom(assemblyPath +
assemblyName + ".dll");
success = true;
}
catch (Exception ex)
{
// Log error
//...

success = false;
}

if (!success)
{
return success;
}

// Now find the proper class and method
Type classTypeToInstantiate = null;
MethodInfo methodInfoToInstantiate = null;
success = false;

foreach (Type classType in assemblyToLoad.GetTypes())
{
// Does the current class have the proper name?
if (classType.Name.ToString().ToUpper() !=
ClassName.ToUpper())
{
continue;
}

// Found a good class to use
classTypeToInstantiate = classType;

// Check all the methods in the class for a matching one
foreach (MethodInfo mi in classType.GetMethods(flags))
{
// Does the method have the proper name?
if (mi.Name.ToString().ToUpper() == MethodName.ToUpper())
{
methodInfoToInstantiate = mi;
break;
}
}
}

// Did we find a class and method to call
if (classTypeToInstantiate == null || methodInfoToInstantiate ==
null)
{
return false;
}

// OK, now try to call the method
try
{
//Found the class, now instantiate it
classInstance = Activator.CreateInstance(
classTypeToInstantiate, // Class type to instantiate
classConstructorParameters); // Arguments

// If class has been instantiated
if (classInstance != null)
{
object retObj =
methodInfoToInstantiate.Invoke(classInstance, methodParameters);

// Got this far, then ok
success = true;
}
else
{
success = false;
}
}
catch (Exception ex)
{
success = false;
}

return success;
}



//In Project 2:
public class MyAssembly
{
private MyAssembly() {}

public MyAssembly(AssemblyLoader loader, Info info)
{
//...
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top