.NETGURU
Reflection and AddEventHandler method
Messages   Related Types
This message was discovered on ASPFriends.com 'ngfx-reflection' list.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.

Ryan Trudelle-Schwarz (VIP)
-- Moved from [aspngfw] to [ngfx-reflection] by Charles M. Carroll <Click here to reveal e-mail address> --Relevant code: Control MyControl = Page.FindControl("myControl"); Type MyType = MyControl.GetType(); System.Delegate x = new EventHandler(this.myEventHandler); MyType.GetEvent("SomeEvent").AddEventHandler(MyControl, x); } public void myEventHandler(object sender, EventArgs e) { Trace.Write("event handler","Event occured"); }This is looking at a non pre-compiled user control.A few facts:1) x is, by every test I can think of, a valid Delegate.2) Both arguments are, by all tests, not null.3) I can do as many of these as I want: MyType.GetProperty("someProperty").SetValue(MyControl,"SomeValue",null);without any errors (for valid properties of course).Any ideas what's going wrong?Thanks, Ryan
Reply to this message...
 
    
Mitch Denny (VIP)
Ryan,

Without trying it, how about make x an EventHandler, it
might be possible that some detail is being lost when
you cast it down to the base delegate. Example:

    EventHandler x = new EventHandler(this.myEventHandler);

This is just a wild stab in the dark. Its odd that
the second parameter of that method takes any delegate
and isn't more specific.

----------------------------------------
- Mitch Denny
- Click here to reveal e-mail address
- +61 (414) 610-141
-

-----Original Message-----
From: Ryan Trudelle-Schwarz [mailto:Click here to reveal e-mail address]
Sent: Wednesday, 2 January 2002 19:22
To: ngfx-reflection
Subject: [ngfx-reflection] Reflection and AddEventHandler method

-- Moved from [aspngfw] to [ngfx-reflection] by Charles M. Carroll
<Click here to reveal e-mail address> --

Relevant code:

Control MyControl = Page.FindControl("myControl");
Type MyType = MyControl.GetType();
System.Delegate x = new EventHandler(this.myEventHandler);
MyType.GetEvent("SomeEvent").AddEventHandler(MyControl, x);
}

public void myEventHandler(object sender, EventArgs e)
{
Trace.Write("event handler","Event occured");
}

This is looking at a non pre-compiled user control.

A few facts:

1) x is, by every test I can think of, a valid Delegate.
2) Both arguments are, by all tests, not null.
3) I can do as many of these as I want:

MyType.GetProperty("someProperty").SetValue(MyControl,"SomeValue",null);
without any errors (for valid properties of course).

Any ideas what's going wrong?

Thanks, Ryan

| [ngfx-reflection] member Click here to reveal e-mail address = YOUR ID
| http://www.aspfriends.com/aspfriends/ngfx-reflection.asp = JOIN/QUIT

Reply to this message...
 
    
Ryan Trudelle-Schwarz (VIP)
Nope, tried that too and it didn't work.

I was shown a few hours after I sent the initial message that it was
incomplete.

The error I am getting is:
Object type cannot be converted to target type.

It is coming from the AddEventHandler() method call (I split it up on to
3 lines and it is definitely that call that's erroring).

I've tested all 3 objects to make sure they are not null and to verify
that they are the proper object type.

Any other ideas?

Ryan

-----Original Message-----
From: Mitch Denny [mailto:Click here to reveal e-mail address]

Ryan,

Without trying it, how about make x an EventHandler, it
might be possible that some detail is being lost when
you cast it down to the base delegate. Example:

    EventHandler x = new EventHandler(this.myEventHandler);

This is just a wild stab in the dark. Its odd that
the second parameter of that method takes any delegate
and isn't more specific.

----------------------------------------
- Mitch Denny

-----Original Message-----
From: Ryan Trudelle-Schwarz [mailto:Click here to reveal e-mail address]

Relevant code:

Control MyControl = Page.FindControl("myControl");
Type MyType = MyControl.GetType();
System.Delegate x = new EventHandler(this.myEventHandler);
MyType.GetEvent("SomeEvent").AddEventHandler(MyControl, x);
}

public void myEventHandler(object sender, EventArgs e)
{
Trace.Write("event handler","Event occured");
}

This is looking at a non pre-compiled user control.

A few facts:

1) x is, by every test I can think of, a valid Delegate.
2) Both arguments are, by all tests, not null.
3) I can do as many of these as I want:

MyType.GetProperty("someProperty").SetValue(MyControl,"SomeValue",null);
without any errors (for valid properties of course).

Any ideas what's going wrong?

Thanks, Ryan

Reply to this message...
 
    
Mitch Denny (VIP)
Ryan,

Hrmmm. The following code worked for me - in the control
code I inserted a button and raised the SomeEvent from
its Click event.

    Control control = null;
    Type controlType = null;
    EventInfo eventInfo = null;

    Control control = this.FindControl("MyControl");

    Type controlType = control.GetType();
    
    eventInfo = controlType.GetEvent("SomeEvent");

    eventInfo.AddEventHandler(control, new
EventHandler(this.MyControl_SomeEvent));

Is it at all possible that the event isn't getting
fired in the control? Also, as a test, you might
try doing this:

    BindingFlags flags = BindingFlags.Instance |
BindingFlags.Public;

    foreach (EventInfo eventInfo in controlType.GetEvents(flags)) {

        this.Response.Write(eventInfo.Name + @"<br/>");

    }

Just to make sure that your event is getting seen via
reflection - it should, this is just a check.

----------------------------------------
- Mitch Denny
- Click here to reveal e-mail address
- +61 (414) 610-141
-

-----Original Message-----
From: Ryan Trudelle-Schwarz [mailto:Click here to reveal e-mail address]
Sent: Thursday, 3 January 2002 16:17
To: ngfx-reflection
Subject: [ngfx-reflection] RE: Reflection and AddEventHandler method

Nope, tried that too and it didn't work.

I was shown a few hours after I sent the initial message that it was
incomplete.

The error I am getting is:
Object type cannot be converted to target type.

It is coming from the AddEventHandler() method call (I split it up on to
3 lines and it is definitely that call that's erroring).

I've tested all 3 objects to make sure they are not null and to verify
that they are the proper object type.

Any other ideas?

Ryan

-----Original Message-----
From: Mitch Denny [mailto:Click here to reveal e-mail address]

Ryan,

Without trying it, how about make x an EventHandler, it
might be possible that some detail is being lost when
you cast it down to the base delegate. Example:

    EventHandler x = new EventHandler(this.myEventHandler);

This is just a wild stab in the dark. Its odd that
the second parameter of that method takes any delegate
and isn't more specific.

----------------------------------------
- Mitch Denny

-----Original Message-----
From: Ryan Trudelle-Schwarz [mailto:Click here to reveal e-mail address]

Relevant code:

Control MyControl = Page.FindControl("myControl");
Type MyType = MyControl.GetType();
System.Delegate x = new EventHandler(this.myEventHandler);
MyType.GetEvent("SomeEvent").AddEventHandler(MyControl, x);
}

public void myEventHandler(object sender, EventArgs e)
{
Trace.Write("event handler","Event occured");
}

This is looking at a non pre-compiled user control.

A few facts:

1) x is, by every test I can think of, a valid Delegate.
2) Both arguments are, by all tests, not null.
3) I can do as many of these as I want:

MyType.GetProperty("someProperty").SetValue(MyControl,"SomeValue",null);
without any errors (for valid properties of course).

Any ideas what's going wrong?

Thanks, Ryan

| [ngfx-reflection] member Click here to reveal e-mail address = YOUR ID
| http://www.aspfriends.com/aspfriends/ngfx-reflection.asp = JOIN/QUIT

Reply to this message...
 
 
System.Delegate
System.Diagnostics.Trace
System.EventArgs
System.EventHandler
System.Reflection.BindingFlags
System.Reflection.EventInfo
System.Web.UI.Page




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification