By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,903 Members | 1,677 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,903 IT Pros & Developers. It's quick & easy.

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

P: n/a
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:\\SimpleSol utions\\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);
}

}
}

Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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

"Daniel" <so*******************@yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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:\\SimpleSol utions\\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);
}

}
}

Nov 16 '05 #2

P: n/a
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...
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:\\SimpleSol utions\\asmload\\asm
toload
\\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);
}
}
}

Nov 16 '05 #3

P: n/a
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.Create Instance(type), null);

"Daniel" wrote:
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:\\SimpleSol utions\\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);
}

}
}

Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.