how do i access methods when the .net assembly was loaded with late biding?

  • Thread starter Thread starter Daniel
  • Start date Start date
D

Daniel

how do i access methods when the .net assembly was loaded with late biding?

class Class1
{
public static void Main()
{
System.Reflection.Assembly SampleAssembly;
SampleAssembly =
System.Reflection.Assembly.LoadFrom("C:\\SimpleSolutions\\asmload\\asmtoload
\\bin\\Debug\\asmtoload.dll");
System.Type[] Types = SampleAssembly.GetTypes();
foreach (System.Type oType in Types)
{
System.Console.WriteLine(oType.Name.ToString());
string strName = oType.Name.ToString();
System.Object pObj = SampleAssembly.CreateInstance("asmtoload" + "." +
strName);
//System.Console.WriteLine(pObj.IOCTL("foo")); THIS IS A BUILD ERROR
BECAUSE IOCTL method is not accessible, how to access method if type info of
dynamicaly loaded dll?
System.Threading.Thread.Sleep(5000);
}

}
}
 
Almost there,

try;
System.Object pObj = System.Activator.CreateInstance(oType);
System.Reflection.MethodInfo[] vMethods =
oType.GetMethods(BindingFlags.Public | BindingFlags.Instance);
for(int vIdx=0; vIdx< vMethods.Length; vIdx++)
{
string vName = vMethods[vIdx].Name;
System.Console.WriteLine("Invoke method " + vName + " with 2 string
arguements");
object[] vArgs = new object[2];
vArgs[0] = "My string arguement #1";
vArgs[1] = "My string arguement #2";
oType.InvokeMember(vName, BindingFlags.InvokeMethod, null, pObj, vArgs);
}

You will get some errors if you run this "as is", due to the fact that all
the methods will not take 2 string arguements - but this should get you
heading in the right direction (for all you cross posts!)

- Colin
 
Hello Daniel,

Your best bet for late binding is to use VB .NET. It's way too much work
to program late binding code in C# using Reflection...
 
try out this code:
It loads a form from a dll calling the show method.
I post it in another thread as well.

string assembly = "ClassLibrary1.dll";
string method = "Show";
string type = "Form1";
Assembly assemblyInstance = Assembly.LoadFrom(assembly);
assemblyInstance.GetType(type).InvokeMember(method, BindingFlags.Public |
BindingFlags.InvokeMethod |
BindingFlags.Instance,null,assemblyInstance.CreateInstance(type), null);
 
Back
Top