473,662 Members | 2,752 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Does FieldInfo.GetVa lue return the actual field?

Hi folks

I am trying to assign a Delegate to an Event using reflection.

I use FieldInfo.GetVa lue to supposedly get the field object, but casting the
result to the type of the field and assigning the Delegate does not catch
the event.

1. Does GetValue really return the *actual* object held in the field ?
2. Is there an easier way to assign a delegate in a containing class to an
event in the fields it contains ?

Joanna

--
Joanna Carter
Consultant Software Engineer
Nov 16 '05 #1
9 7123
Joanna Carter (TeamB) <jo*****@nospam forme.com> wrote:
I am trying to assign a Delegate to an Event using reflection.

I use FieldInfo.GetVa lue to supposedly get the field object, but casting the
result to the type of the field and assigning the Delegate does not catch
the event.

1. Does GetValue really return the *actual* object held in the field ?
What exactly do you mean by "actual object"?
2. Is there an easier way to assign a delegate in a containing class to an
event in the fields it contains ?


I'm afraid I don't understand exactly what you mean. Could you post a
short but complete program which demonstrates the problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

It sounds like what you want to do is going to break encapsulation
though...

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
"Jon Skeet [C# MVP]" <sk***@pobox.co m> a écrit dans le message de news:
MP************* ***********@msn ews.microsoft.c om...
What exactly do you mean by "actual object"?
I want to assign a delegate to an event on a value type inside a method of a
base containing class that does not know what fields are going to be added
in the sub-classes
I'm afraid I don't understand exactly what you mean. Could you post a
short but complete program which demonstrates the problem?


///////////////////////////////
public struct TestType<T>
{
public delegate void ValueTypeValida tionHandler(T oldValue, T newValue);

private T value;

public event ValueTypeValida tionHandler<T> valueChanged;

public ValueType(T value)
{
this.value = value;
valueChanged = null;
}

public T Value
{
get { return value; }
set
{
if(valueChanged != null)
valueChanged(th is.value, value);

this.value = value;
}
}
}
//////////////////////

I want to be able to add TestType<> fields to a containing class and have
the containing class assign a delegate to each of the TestType<> fields
without naming them specifically, in the constructor of the containing
class.

So far I have got this far but I want to know how to attach the delegates to
the events.

/////////////////////////
public ContainingType( )
{
FieldInfo[] fieldInfos = GetType().GetFi elds(BindingFla gs.Instance |
BindingFlags.No nPublic);

foreach(FieldIn fo fi in fieldInfos)
{
Type ft = fi.FieldType;

object fObj = fi.GetValue(thi s);

((TestType<ft>) fObj).valueChan ged += new
ValueTypeValida tionHandler<ft> (HandleValueCha nged<ft>);
}
////////////////////////////

Joanna

--
Joanna Carter
Consultant Software Engineer
Nov 16 '05 #3
Joanna Carter (TeamB) <jo*****@nospam forme.com> wrote:
"Jon Skeet [C# MVP]" <sk***@pobox.co m> a écrit dans le message de news:
MP************* ***********@msn ews.microsoft.c om...
What exactly do you mean by "actual object"?
I want to assign a delegate to an event on a value type inside a method of a
base containing class that does not know what fields are going to be added
in the sub-classes


Okay. I'm still not sure I entirely understand what you're doing, and I
suspect there's a better way (there often *is* a better way than
reflection) but anyway:
I'm afraid I don't understand exactly what you mean. Could you post a
short but complete program which demonstrates the problem?


<snip>

Well, that's not a complete program, but...

<snip>
public struct TestType<T>
<snip>
((TestType<ft>) fObj).valueChan ged += new
ValueTypeValida tionHandler<ft> (HandleValueCha nged<ft>);
}


The problem is that you've got a struct, not a class. So you're getting
the value, changing it, but never setting the value back into the
field. It's like doing:

int x = someObject.Some Variable;
x++;

and expecting the value in "someObject " to be updated. You'll need to
call fi.SetValue (this, fObj); afterwards to make the change stick. Or
turn your struct into a class, of course... do you really need it to be
a value type?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
"Jon Skeet [C# MVP]" <sk***@pobox.co m> a écrit dans le message de news:
MP************* ***********@msn ews.microsoft.c om...
Okay. I'm still not sure I entirely understand what you're doing, and I
suspect there's a better way (there often *is* a better way than
reflection) but anyway:
Yes, in Delphi we have RTTI which is a very poor reflection and certainly
not as comprehansive as .NET reflection :-)
The problem is that you've got a struct, not a class. So you're getting
the value, changing it, but never setting the value back into the
field. It's like doing:

int x = someObject.Some Variable;
x++;

and expecting the value in "someObject " to be updated. You'll need to
call fi.SetValue (this, fObj); afterwards to make the change stick. Or
turn your struct into a class, of course... do you really need it to be
a value type?


This may be the cause of some of my misunderstandin g :-)

I am still learning about the implications of boxing and unboxing. Are you
saying that the GetValue() call returns an object which contains a copy of
the original field, and *not* the original field ?

So boxing creates an object with a copy of a ValueType ? and unboxing by
casting to the original type does not return the original ValueType ?

Or is this just what happens with FieldInfo.GetVa lue() ?

Joanna

--
Joanna Carter
Consultant Software Engineer
Nov 16 '05 #5
"Joanna Carter \(TeamB\)" <jo*****@nospam forme.com> wrote in news:#
$e************@ TK2MSFTNGP15.ph x.gbl:
saying that the GetValue() call returns an object which contains a copy of
the original field, and *not* the original field ?
AFAIK Yes.
So boxing creates an object with a copy of a ValueType ? and unboxing by
casting to the original type does not return the original ValueType ?


AFAIK yes too.


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programmin g is an art form that fights back"

Empower ASP.NET with IntraWeb
http://www.atozed.com/IntraWeb/
Nov 16 '05 #6
"Chad Z. Hower aka Kudzu" <cp**@hower.org > a écrit dans le message de news:
Xn************* *****@127.0.0.1 ...

Hey Chad, fancy meeting you here ! :-))
AFAIK Yes. AFAIK yes too.


Aahh, the light dawns :-)

Joanna

--
Joanna Carter
Consultant Software Engineer
Nov 16 '05 #7
Joanna Carter (TeamB) <jo*****@nospam forme.com> wrote:
and expecting the value in "someObject " to be updated. You'll need to
call fi.SetValue (this, fObj); afterwards to make the change stick. Or
turn your struct into a class, of course... do you really need it to be
a value type?
This may be the cause of some of my misunderstandin g :-)

I am still learning about the implications of boxing and unboxing. Are you
saying that the GetValue() call returns an object which contains a copy of
the original field, and *not* the original field ?


It returns the value of the original field. It can't return the field
itself, as that's a variable rather than a value.
So boxing creates an object with a copy of a ValueType ? and unboxing by
casting to the original type does not return the original ValueType ?

Or is this just what happens with FieldInfo.GetVa lue() ?


It's not really got anything to do with boxing. It's the way value
types work - with a value type, the value of the expression *is* the
whole data, rather than a reference to an actual object.

Read the following articles and see if they help you:

http://www.pobox.com/~skeet/csharp/parameters.html
http://www.pobox.com/~skeet/csharp/memory.html

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8
"Jon Skeet [C# MVP]" <sk***@pobox.co m> a écrit dans le message de news:
MP************* ***********@msn ews.microsoft.c om...
It returns the value of the original field. It can't return the field
itself, as that's a variable rather than a value.
So, if I want to do anything with what GetValue() returns, I then have to
pass my 'copy' to SetValue() to update the underlying field ? That sounds
reasonable to me :-)
It's not really got anything to do with boxing. It's the way value
types work - with a value type, the value of the expression *is* the
whole data, rather than a reference to an actual object.


Right, the value type semantics are the same in Delphi; it was just the
GetValue() thing that threw me; I didn't realise that a value type function
result would be a copy. That's what comes with having used Delphi value
types which are not object based and trying to discern where the
'objectiness' starts and ends in .NET :-)

Can I just follow on by asking if I can cast an object to an unbound generic
type and obtain an event from the resulting object ?

e.g.

I want to cast the result of GetValue() to TestType<T> which has an event
ValueChanged.

Then I want to assign a delegate to that event.

This won't compile :

TestType<> t = (TestType<>)fie ldInfo.GetValue (this);

TIA

Joanna

--
Joanna Carter
Consultant Software Engineer
Nov 16 '05 #9
Joanna Carter (TeamB) <jo*****@nospam forme.com> wrote:
"Jon Skeet [C# MVP]" <sk***@pobox.co m> a écrit dans le message de news:
MP************* ***********@msn ews.microsoft.c om...
It returns the value of the original field. It can't return the field
itself, as that's a variable rather than a value.
So, if I want to do anything with what GetValue() returns, I then have to
pass my 'copy' to SetValue() to update the underlying field ? That sounds
reasonable to me :-)


You have to do that if you want to change the value of the field
itself. If the value is a reference, you could change the contents of
the object without changing the field's value, and the change would
still be visible.
It's not really got anything to do with boxing. It's the way value
types work - with a value type, the value of the expression *is* the
whole data, rather than a reference to an actual object.


Right, the value type semantics are the same in Delphi; it was just the
GetValue() thing that threw me; I didn't realise that a value type function
result would be a copy. That's what comes with having used Delphi value
types which are not object based and trying to discern where the
'objectiness' starts and ends in .NET :-)


Right.
Can I just follow on by asking if I can cast an object to an unbound generic
type and obtain an event from the resulting object ?


I'm afraid I don't know enough about generics to answer that properly -
I'd start a new thread with an appropriate subject line to get those
who know about generics interested :)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #10

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

Similar topics

2
1417
by: dotnetguy | last post by:
Hello, having a FieldInfo object I'd like to get a reference to the corresponding field object. I know the type of the field object (so I can do a cast if it is required) but I don't know if FieldInfo indeed provides a reference to the underlying object and how to get it. Can anyone help out??? Thx.
5
13272
by: Cezar | last post by:
Hi, I'm trying to get the value of a field in a class using Reflection. First of all, I can't access private fields. I know MSDN says you can access them if the code is fully trusted, but what that means? I added at the end some code I used. Then, if the field is static everything works fine, but it doesn't work for
2
1511
by: Nicolas | last post by:
What shuld I do on my code to make it work on both vb and csharp(ok). This code are in a dll It seems that withevents cause a problem on fieldinfo private Control getObject(string ControlName) { //return an handle of the control try {
0
860
by: Nicolas | last post by:
How can I fixe my code which is working fine for csarp control but give me trouble on vb control withevents This code is compil in a separate dll private Control getObject(string ControlName) { //return an handle of the control try { if(ControlName != FormOwner.Name)
3
3101
by: Laurie | last post by:
Hi, I need to be able to manipulate field values within a structure using FieldInfo.GetValue and FieldInfo.SetValue, in VB.Nt 2003. The GetValue is working fine and makes it really easy for me to maintain my code. Unfortunately, SetValue doesn't return any errors but doesn't actually set the value. I have read through various newsgroup postings, and think the problem might have something to do with "boxing", and passing of structures...
2
4989
by: Brent | last post by:
I have variables in a structure loaded into a list box. I thought I could use FieldInfo.SetValue to update the items value when the user clicks on it, but it is not working. .. .. .. Dim fi As Reflection.FieldInfo fi = tp.GetField(Item) MsgBox(fi.GetValue(InItems)) 'returns the original value
1
2195
by: David Gouge | last post by:
Posting on behalf of a colleague (no, really! ;) )... Hi, I was wondering if there is any equivalent of FieldInfo.SetValue(Object, Object) that is strongly typed or uses generics? I have been looking at the source for FieldInfo to see if I could implement my own generic version of this method but unfortunately the code seems to be hidden, is there anyway I can implement my own method if there is no equivalent?
7
8711
by: Wilson | last post by:
Hi, How do get the Dictioanry object from FiedlInfo ? my code : fieldInfo = this.GetType().GetField("dictioanry1"); ??Dictionary<string, string> dicTemp1 = (Dictionary<string, string>)fieldInfo; Thanks Wilson
1
4863
by: damiensawyer | last post by:
Hi, I need to invoke .getvalue and .setvalue on fieldinfo and propetyinfo objects. Even though they're both derived from MemberInfo, they don't share those two methods. I've finding myself writing the following types of structure over and over. Is there something I'm missing? Or some 'funky' 3.5 delegate/lambda expression way of doing this? Thanks very much in advance,
0
8435
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8345
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8857
Oralloy
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8768
jinu1996
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8547
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6186
isladogs
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4181
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1999
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1754
bsmnconsultancy
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.