Introduction
This code drop is part of a smash and grab series. If you're in a rush, you can just grab this code and insert it into your application, no understanding is required. When you have some time (yeah, right), you may want to review the source code.
Background
I needed to load an arbitrary class and execute some methods in it at runtime. I wouldn't know ahead of time what the class or member name was, so I couldn't use a reference in the project. Here is how I did it. Just copy/paste from the code listing below, and away you go.
The code
public class DynaInvoke
{
public static Object InvokeMethodSlow(string AssemblyName,
string ClassName, string MethodName, Object[] args)
{
Assembly assembly = Assembly.LoadFrom(AssemblyName);
foreach (Type type in assembly.GetTypes())
{
if (type.IsClass == true)
{
if (type.FullName.EndsWith("." + ClassName))
{
object ClassObj = Activator.CreateInstance(type);
object Result = type.InvokeMember(MethodName,
BindingFlags.Default | BindingFlags.InvokeMethod,
null,
ClassObj,
args);
return (Result);
}
}
}
throw (new System.Exception("could not invoke method"));
}
public class DynaClassInfo
{
public Type type;
public Object ClassObject;
public DynaClassInfo()
{
}
public DynaClassInfo(Type t, Object c)
{
type = t;
ClassObject = c;
}
}
public static Hashtable AssemblyReferences = new Hashtable();
public static Hashtable ClassReferences = new Hashtable();
public static DynaClassInfo
GetClassReference(string AssemblyName, string ClassName)
{
if (ClassReferences.ContainsKey(AssemblyName) == false)
{
Assembly assembly;
if (AssemblyReferences.ContainsKey(AssemblyName) == false)
{
AssemblyReferences.Add(AssemblyName,
assembly = Assembly.LoadFrom(AssemblyName));
}
else
assembly = (Assembly)AssemblyReferences[AssemblyName];
foreach (Type type in assembly.GetTypes())
{
if (type.IsClass == true)
{
if (type.FullName.EndsWith("." + ClassName))
{
DynaClassInfo ci = new DynaClassInfo(type,
Activator.CreateInstance(type));
ClassReferences.Add(AssemblyName, ci);
return (ci);
}
}
}
throw (new System.Exception("could not instantiate class"));
}
return ((DynaClassInfo)ClassReferences[AssemblyName]);
}
public static Object InvokeMethod(DynaClassInfo ci,
string MethodName, Object[] args)
{
Object Result = ci.type.InvokeMember(MethodName,
BindingFlags.Default | BindingFlags.InvokeMethod,
null,
ci.ClassObject,
args);
return (Result);
}
public static Object InvokeMethod(string AssemblyName,
string ClassName, string MethodName, Object[] args)
{
DynaClassInfo ci = GetClassReference(AssemblyName, ClassName);
return (InvokeMethod(ci, MethodName, args));
}
}
Notes
This is how you use it:
Object [] args = {1, "2", 3.0};
Object Result = DynaInvoke("c:\FullPathToDll.DLL",
"ClassName", "MethodName", args);
SmashGrab / Redux series
I have recently started two series of articles here at CodeProject. Smash&Grab is intended as a series of short articles on one specific code technique. Redux is intended as a series of longer articles which attempt to reduce a complicated topic (like GridView
) into its basic constituent parts and show that once you have all the information, it wasn't really that hard after all. To find Smash&Grab articles, search for the keyword SmashGrab. To find Redux articles, search for the keyword Redux. I welcome any contributions to either series, but please follow the guidelines when submitting articles to either.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.