473,385 Members | 1,400 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,385 software developers and data experts.

CreateDelegate for and type of EventArg

Hi,

Is is possible to create an event handler method that can handle any
type of event no matter what the delegate signature is.

Here is the code I have so far:

public class class1
{
public Delegate Hook(Type eventHandlerType)
{

Delegate eh = Delegate.CreateDelegate(
eventHandlerType,
this,
"Callback",
true );

return eh;

}

private void CallBack(object sender, EventArgs e)
{
Console.WriteLine("callback");
}
}
With the code above I can do:

Delegate d = _class1.Hook( typeof( EventHandler ) );
d.Method.Invoke(...);
But I cannot do it with other type of delegates, such as
CancelEventHandler:

Delegate d = _class1.Hook( typeof( CancelEventHandler ) );
d.Method.Invoke(...);

This fails at runtime. The call to Delegate.CreateDelegate fails with
an argument exception. Error: "Error binding to target method."

Is there a way to this work? After all CancelEventArg derives from
EventArg.

Thanks for your help.

Sylvain

Nov 17 '05 #1
3 4465
Unfortunately, in .NET 1.1 and before, there is not. However, in .NET
2.0 and beyond, yes, you can do this, because there is covariance with
delegate parameters (meaning that because your method handler can accept
EventArgs, and that is the base class for the parameters required by the
delegate).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<re*******@hotmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
Hi,

Is is possible to create an event handler method that can handle any
type of event no matter what the delegate signature is.

Here is the code I have so far:

public class class1
{
public Delegate Hook(Type eventHandlerType)
{

Delegate eh = Delegate.CreateDelegate(
eventHandlerType,
this,
"Callback",
true );

return eh;

}

private void CallBack(object sender, EventArgs e)
{
Console.WriteLine("callback");
}
}
With the code above I can do:

Delegate d = _class1.Hook( typeof( EventHandler ) );
d.Method.Invoke(...);
But I cannot do it with other type of delegates, such as
CancelEventHandler:

Delegate d = _class1.Hook( typeof( CancelEventHandler ) );
d.Method.Invoke(...);

This fails at runtime. The call to Delegate.CreateDelegate fails with
an argument exception. Error: "Error binding to target method."

Is there a way to this work? After all CancelEventArg derives from
EventArg.

Thanks for your help.

Sylvain

Nov 17 '05 #2
Is there a way to this work? After all CancelEventArg derives from
EventArg.


You can generate dynamic thunk methods with Reflection Emit that
matches the delegate signature and forwards the call to a generic
handler. Pretty messy but doable.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #3
Is there a way to this work? After all CancelEventArg derives from
EventArg.


You can generate dynamic thunk methods with Reflection Emit that
matches the delegate signature and forwards the call to a generic
handler. Pretty messy but doable.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: S.Tobias | last post by:
I'm trying to understand how structure type completion works. # A structure or union type of unknown # content (as described in 6.7.2.3) is an incomplete type. It # is ...
0
by: Nivek Eroom | last post by:
Can anyone tells me how can i add a delegate to an object remotly created as this : object patate = Activator.GetObject(tt,"http://localhost:8080/DynamicTemplateServer.soap"); ...
1
by: redhotsly | last post by:
Hi, Is is possible to create an event handler method that can handle any type of event no matter what the delegate signature is. Here is the code I have so far: public class class1 {...
7
by: Max | last post by:
I'm using late binding to automate to Outlook and I'm getting an ArgumentException when I create a delegate. The arguments I pass seem valid. This is my code: Type oType =...
0
by: Chris Fink | last post by:
When I am consuming a webservice, an object has an undefined value (inq3Type.Call3Data). I do not completely understand why this is happening and apologize for the vague question. My assumption...
1
by: Rob Griffiths | last post by:
Can anyone explain to me the difference between an element type and a component type? In the java literature, arrays are said to have component types, whereas collections from the Collections...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Languageâ€, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
1
by: snibril | last post by:
Hi, I'm trying to delegate all events of a command button to a single method, handlesAll. I wrote the following code in VS.NET 2005, and it worked fine: public void setUpDelegates() { ...
1
by: tesvit | last post by:
Hi there, I am working on a class library that should incpect a given class, make a delegate to a get Property and use it later on. Here is the code snippet: public delegate int...
9
by: Klaudiusz Bryja | last post by:
Hi, I need Delegate.CreateDelegate method equivalent in compact framework. Best regards, Klaudiusz
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
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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.