LCE Late Bind

G

Guest

Hi,

I have a Class1.cs and a Main executable file.

The main executable codes look like this:
Assembly asm = Assembly.LoadFile(@"C:\ClassLibrary1.dll");
Type typ = asm.GetType("INPTest.Class1");
MethodInfo mi = typ.GetMethod("Show");
mi.Invoke(asm, null);

The Class1.cs codes look like this:
public static void Show()
{
MessageBox.Show("Create Log");
}

If I remove the static from the Show method I will have the error message
displayed --> Object does not match target type.

Can anyone show some examples using the same concept without the static
keyword?

Thanks,
C# newbie
 
W

Wiktor Zychla

Can anyone show some examples using the same concept without the static

to call non-static methods you need an object instance and invoke the method
against the instance.

Assembly asm = Assembly.LoadFile(@"C:\ClassLibrary1.dll");
Type typ = asm.GetType("INPTest.Class1");

object o = Activator.CreateInstance( typ );
typ.InvokeMember( "Show", BindingFlags.InvokeMethod, null, o, null );

regards,
Wiktor Zychla
 
G

Guest

Thanks Wiktor

Wiktor Zychla said:
to call non-static methods you need an object instance and invoke the method
against the instance.

Assembly asm = Assembly.LoadFile(@"C:\ClassLibrary1.dll");
Type typ = asm.GetType("INPTest.Class1");

object o = Activator.CreateInstance( typ );
typ.InvokeMember( "Show", BindingFlags.InvokeMethod, null, o, null );

regards,
Wiktor Zychla
 

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