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 eventHandlerTyp e)
{
Delegate eh = Delegate.Create Delegate(
eventHandlerTyp e,
this,
"Callback",
true );
return eh;
}
private void CallBack(object sender, EventArgs e)
{
Console.WriteLi ne("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
CancelEventHand ler:
Delegate d = _class1.Hook( typeof( CancelEventHand ler ) );
d.Method.Invoke (...);
This fails at runtime. The call to Delegate.Create Delegate 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 3 4476
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.co m
<re*******@hotm ail.com> wrote in message
news:11******** *************@f 14g2000cwb.goog legroups.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 eventHandlerTyp e) {
Delegate eh = Delegate.Create Delegate( eventHandlerTyp e, this, "Callback", true );
return eh;
}
private void CallBack(object sender, EventArgs e) { Console.WriteLi ne("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 CancelEventHand ler:
Delegate d = _class1.Hook( typeof( CancelEventHand ler ) ); d.Method.Invoke (...);
This fails at runtime. The call to Delegate.Create Delegate 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 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. 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 completed, for all declarations of that type, by
^^^
# declaring the same structure or union tag with its defining
# content ...
|
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");
i.AddEventHandler(t1,
Delegate.CreateDelegate(i.EventHandlerType,patate,ei.Function));
this isn't working, gets me the error
|
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
{
public Delegate Hook(Type eventHandlerType)
|
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 = Type.GetTypeFromProgID("Outlook.Application");
oApp = Activator.CreateInstance(oType);
oInspectors = oApp.GetType().GetProperty("Inspectors").GetValue(oApp, null);
|
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 is that the WSDL is
defined incorrectly and .NET cannot parse the types. Any help is greatly
appreciated!
CustDDGSvc ws = new CustDDGSvc();...
| |
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 Framework are said to have an element
type.
http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html
|
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 paper written on this
subject.
On the Expressive Power of Programming Languages, by Matthias
Felleisen, 1990....
|
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()
{
MethodInfo mi = typeof(MainForm).GetMethod("handlesAll",
|
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 MyClassDelegate(MyClass cls);
....
|
by: Klaudiusz Bryja |
last post by:
Hi,
I need Delegate.CreateDelegate method equivalent in compact
framework.
Best regards,
Klaudiusz
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |