469,917 Members | 1,764 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,917 developers. It's quick & easy.

can a late bound delegate be called in a late bound C# dll?

How do I call a delegate in late bound C# dll? there some way to do this w/
a sharedinterface file? any examples? i tried this but it doesnt work:

(oType.GetMethod("IOCTLJOB").Invoke(pObj, new object[] {
pClass1.m_job } )).ToString();

and it returns the error:

Additional information: Object type cannot be converted to target type.

I have the delegate defined in both the late bound dll and the host assembly
like this:
public delegate void Job();
Nov 16 '05 #1
1 1846
Daniel wrote:
How do I call a delegate in late bound C# dll? there some way to do this w/
a sharedinterface file? any examples? i tried this but it doesnt work:

(oType.GetMethod("IOCTLJOB").Invoke(pObj, new object[] {
pClass1.m_job } )).ToString();

and it returns the error:

Additional information: Object type cannot be converted to target type.

I have the delegate defined in both the late bound dll and the host assembly
like this:
public delegate void Job();

Hi Daniel,

When you look at your assembly with ILDASM.exe, you will notice that
delegates are implemented as inner classes of your type. The name of
this inner class will be the name of your delegate type.
This inner class has a constructor, which takes:
- The outer instance
- A function pointer to the method that should be called

The delegate class also has an "Invoke" method, together with a
BeginInvoke and EndInvoke pair (if you want asynchronous activation).

Say, for example that you have this class:

public class MyClass
{
public MyClass()
{
}

public void Foo()
{
Console.WriteLine("test");
}

public delegate void Job();
}

In the above example, the "Foo" method is a method that can be invoked
through the delegate of type "Job".

Below is the code to do this invokation ( the comments should be self
explanatory)

//
// Load the type's assembly, Get the type, and create an instance
//
Assembly assembly =
AppDomain.CurrentDomain.Load("TestDelegateAssembly ");
Type classType = assembly.GetType("TestDelegateAssembly.MyClass");
bject instance = Activator.CreateInstance(classType);

//
// Now, get the delegate (implemented as an inner class), and get
// a reference to a method ("Foo"), which has a signature
// that corresponds to the delegate
//
Type delegateType =
assembly.GetType("TestDelegateAssembly.MyClass+Job ");
MethodInfo fooRef = classType.GetMethod("Foo");

//
// We need to pass in a pointer to the function, so go ahead
// and get that one
//
IntPtr fooRefPtr = fooRef.MethodHandle.GetFunctionPointer();

//
// Create an instance of the delegate inner class. We need
// to pass in a reference to the containing instance AND
// a reference to the method
//
object delegateInstance =
Activator.CreateInstance(
delegateType,
new object[] { instance, fooRefPtr });

//
// All delegates expose the "Invoke" Method
//
MethodInfo invokeMethod = delegateType.GetMethod("Invoke");

//
// Now we can "Invoke", the "Invoke" method ;-)
//
invokeMethod.Invoke(delegateInstance, null);
Hope this helps,

Bennie
Nov 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Olaf Meding | last post: by
9 posts views Thread by John Smith | last post: by
7 posts views Thread by Ant | last post: by
7 posts views Thread by Entwickler | last post: by
5 posts views Thread by needin4mation | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.