PC Review


Reply
Thread Tools Rate Thread

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

 
 
Daniel
Guest
Posts: n/a
 
      17th Dec 2004
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();


 
Reply With Quote
 
 
 
 
Bennie Haelen
Guest
Posts: n/a
 
      17th Dec 2004
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
 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Specifying a late-bound DLL's location Stu Microsoft C# .NET 0 18th Feb 2009 07:32 PM
Early bound to Late bound Henk Microsoft Excel Programming 2 13th Feb 2009 01:01 AM
late bound form reference Galen Somerville Microsoft VB .NET 3 5th Feb 2006 05:01 PM
calling a delegate in a late bound C# dll Daniel Microsoft Dot NET Framework 2 18th Dec 2004 04:57 AM
late bound lee Microsoft Dot NET Framework 2 16th Feb 2004 10:25 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:56 PM.