473,326 Members | 2,813 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,326 software developers and data experts.

GetFileld fails - ReflectionPermission problem?

dan
Hi all,
I have a reflection-problem I'm totally stuck with. Maybe someone has a
hint...
I want to get a fieldinformation of an event from the Control class, e.g.
"TextChanged".

FieldInfo fi = typeof(Control).GetField("TextChanged",
System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Public);

This should return the FieldInfo of the Control.TextChanged-event.But it
doesn't! It returns null.
If I do the same code on my self defined class, it works perfectly. The only
thing I can guess, is that
I should grant permission with ReflectionPermission. I have the default
settings for security and the
security tool tells me there is no security on the windows.forms-assembly.

Anybody can help me?
Thanks in advance

Dan

Full code below:
-------------------------

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Reflection;

namespace ReflectionTest
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Tester
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
//MyClass
EventInfo ei1 = typeof(MyClass).GetEvent("MyEvent");
FieldInfo fi1 = typeof(MyClass).GetField("MyEvent",
System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Public);

//Control
EventInfo ei2 = typeof(Control).GetEvent("TextChanged");
FieldInfo fi2 = typeof(Control).GetField("TextChanged",
System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Public);

//why is fi2 == null ?????
}

class MyClass
{
public event EventHandler MyEvent;
}
}
}

Nov 15 '05 #1
3 4371
dan <da*@dontsend.com> wrote:
I have a reflection-problem I'm totally stuck with. Maybe someone has a
hint...
I want to get a fieldinformation of an event from the Control class, e.g.
"TextChanged".

FieldInfo fi = typeof(Control).GetField("TextChanged",
System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Public);

This should return the FieldInfo of the Control.TextChanged-event.But it
doesn't! It returns null.


Events aren't necessarily stored as fields. In Control, for instance, I
believe that all the events are stored in the component's
EventHandlerList.

Think of it as being similar to properties - just because there's a
property named Foo doesn't mean there's necessarily a field backing it
called foo.

Indeed, it's not even necessary that your code will work on your own
class. The C# compiler is free to use whatever field name it wants. For
instance, the language specification has this example:

<quote>
Thus, an instance event declaration of the form:

class X {
public event D Ev;
}

could be compiled to something equivalent to:

class X {
private D __Ev; // field to hold the delegate
public event D Ev {
add {
lock(this) { __Ev = __Ev + value; }
}
remove {
lock(this) { __Ev = __Ev - value; }
}
}
}

Within the class X, references to Ev are compiled to reference the
hidden field __Ev instead. The name "__Ev" is arbitrary; the hidden
field could have any name or no name at all.
</quote>

It so happens that the MS C# compiler picks the same name for the field
as for the event, but that's not in the specification - and that's only
field-like events, let alone events which don't use a "field per
event" model at all.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
dan <da*@dontsend.com> wrote:
I have a reflection-problem I'm totally stuck with. Maybe someone has a
hint...


<snip>

In my previous post I told you why it didn't work. I neglected to give
you an alternative :)

I suggest you get the event itself using Type.GetEvent instead. You can
then add/remove handlers from it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3
dan
Hi Jon,
thanks a lot for this very fast and precise answer! You're completly right.
I checked it out with Anakrino.
That's the code it decompiles.

public void add_KeyPress(KeyPressEventHandler value)
{
this.Events.AddHandler(Control.EventKeyPress, value);
}

So Control adds its events effectively to the Events property of the
Component class.
I spent a few hours the find a solution. But I'm really happy to know why it
didn't work :-)

The purpose behind my question actually was to figure out, which handler are
attached at the event.
In a class, where the event is defined as a "real" event, I could do so by
writing the following code:

object val= fieldinfo.GetValue(anObject);
MulticastDelegate md = (MulticastDelegate)val;
Delegate[] list = md.GetInvocationList();

This would have been a general way to figure out which events are attached
to an event.
Obviously, this doesn't work on Control. Instead I could get the events
field, which is an EventHandlerList
and ask it for the contained handlers.

Anyway, it was a pleasure to get incredible fast such a competent answer!

Daniel Frey


"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m...
dan <da*@dontsend.com> wrote:
I have a reflection-problem I'm totally stuck with. Maybe someone has a
hint...
I want to get a fieldinformation of an event from the Control class, e.g. "TextChanged".

FieldInfo fi = typeof(Control).GetField("TextChanged",
System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Public);

This should return the FieldInfo of the Control.TextChanged-event.But it
doesn't! It returns null.


Events aren't necessarily stored as fields. In Control, for instance, I
believe that all the events are stored in the component's
EventHandlerList.

Think of it as being similar to properties - just because there's a
property named Foo doesn't mean there's necessarily a field backing it
called foo.

Indeed, it's not even necessary that your code will work on your own
class. The C# compiler is free to use whatever field name it wants. For
instance, the language specification has this example:

<quote>
Thus, an instance event declaration of the form:

class X {
public event D Ev;
}

could be compiled to something equivalent to:

class X {
private D __Ev; // field to hold the delegate
public event D Ev {
add {
lock(this) { __Ev = __Ev + value; }
}
remove {
lock(this) { __Ev = __Ev - value; }
}
}
}

Within the class X, references to Ev are compiled to reference the
hidden field __Ev instead. The name "__Ev" is arbitrary; the hidden
field could have any name or no name at all.
</quote>

It so happens that the MS C# compiler picks the same name for the field
as for the event, but that's not in the specification - and that's only
field-like events, let alone events which don't use a "field per
event" model at all.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #4

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

Similar topics

2
by: Vaap | last post by:
I did lot of googling to see if I can solve the SQL server not found problem while trying to run ASP.Net community starter kit from an XP machine to Windows 2003 server hosting SQL server 2000...
0
by: Etienne | last post by:
I'm using DotNetNuke 3.1 and it works well. I try to use Ajax with it. But if I add the Ajax specification in web.config : <add verb="GET,POST" path="vbwrapper/*.ashx"...
6
by: Ken Varn | last post by:
I have an ASP.NET application that is calling a custom class that is trying to parse all of the members of my Page object using Type.GetMembers(). The problem that I am having is that private...
1
by: MMJ | last post by:
I just wanted to know if someone can tell me how to use the ReflectionPermission Class. What I am trying to do is I am trying to list out all the Private as well as protected members of the class...
2
by: Budhi Saputra Prasetya | last post by:
Hi, I managed to create a Windows Form Control and put it on my ASP .NET page. I have done the suggestion that is provided by modifying the security settings. From the stack trace, I would...
0
by: Budhi Saputra Prasetya | last post by:
Hi, I still have the same problem with embedding Windows Control. I'll just requote what I posted last time: I managed to create a Windows Form Control and put it on my ASP .NET page. I...
1
by: Edwin | last post by:
I would like to dynamically create an object, using a non-public constructor and without ReflectionPermission. At the place where I want to do this, I could access the internal constructor...
3
by: CMorgan | last post by:
Hi everybody, I am experiencing an annoying problem with fork() and execv(). In my program I need to launch the "pppd" from a thread, so, I create a new process with fork and then in the child...
18
by: Eric | last post by:
Ok...this seems to be treading into some really esoteric areas of c++. I will do my best to explain this issue, but I don't fully understand what is happening myself. I am hoping that the problem...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.