473,378 Members | 1,470 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 1997
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Olaf Meding | last post by:
Is there a way to find out if I am using early or late binding given the reference ("excel" in the example below) returned by Dispatch()? >>> import win32com.client >>> excel =...
2
by: Steve Jorgensen | last post by:
I frequently find myself wanting to use class abstraction in VB/VBA code, and frankly, with the tacked-on class support in VB/VBA, there are some problems with trying to do that and have any...
0
by: Matt | last post by:
I'm trying to use late binding to automate Excel from C# so as to run with multiple versions of Excel, and am hoping to avoid referencing any Excel PIA in my project. But I need to add a C# event...
2
by: hazz | last post by:
how do I do this? I am certain my Visual Studio .NET solution allowed me to do this before but I don't know how the build/debug settings were configured. In order to get around an unavoidable...
9
by: John Smith | last post by:
Hey, I'm having a difficult time finding some good examples of late binding Outlook in C#. Anyone know of any good sites out there? I've googled and MSDN'ed, but have come up a bit empty. ...
17
by: David | last post by:
Hi all, I have the following problem: my program works fine, but when I add option strict at the top of the form, the following sub fails with an error that option strict does not allow late...
7
by: Ant | last post by:
Hello, Very simple question but one I need clarified. Which part of the statement below is considered the 'delegate'? Is it the 'new System.EventHandler' or the btnAccept_Click? or is it...
7
by: Entwickler | last post by:
hello, i have the following problem. i want to write a code that enables the user to call functions from a unmanaged code .dll at running time . so i have to use the late binding . i tried the...
5
by: needin4mation | last post by:
Learning about delegates (again, I admit), I think I finally get it, maybe. I can reference any method in any class in the same namespace as long as it has the same signature. Right? But how...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.