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

Does FieldInfo.GetValue return the actual field?

Hi folks

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

I use FieldInfo.GetValue 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 7101
Joanna Carter (TeamB) <jo*****@nospamforme.com> wrote:
I am trying to assign a Delegate to an Event using reflection.

I use FieldInfo.GetValue 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.com>
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.com> a écrit dans le message de news:
MP************************@msnews.microsoft.com...
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 ValueTypeValidationHandler(T oldValue, T newValue);

private T value;

public event ValueTypeValidationHandler<T> valueChanged;

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

public T Value
{
get { return value; }
set
{
if(valueChanged != null)
valueChanged(this.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().GetFields(BindingFlags.Instance |
BindingFlags.NonPublic);

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

object fObj = fi.GetValue(this);

((TestType<ft>) fObj).valueChanged += new
ValueTypeValidationHandler<ft>(HandleValueChanged< ft>);
}
////////////////////////////

Joanna

--
Joanna Carter
Consultant Software Engineer
Nov 16 '05 #3
Joanna Carter (TeamB) <jo*****@nospamforme.com> wrote:
"Jon Skeet [C# MVP]" <sk***@pobox.com> a écrit dans le message de news:
MP************************@msnews.microsoft.com...
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).valueChanged += new
ValueTypeValidationHandler<ft>(HandleValueChanged< 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.SomeVariable;
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.com>
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.com> a écrit dans le message de news:
MP************************@msnews.microsoft.com...
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.SomeVariable;
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 misunderstanding :-)

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.GetValue() ?

Joanna

--
Joanna Carter
Consultant Software Engineer
Nov 16 '05 #5
"Joanna Carter \(TeamB\)" <jo*****@nospamforme.com> wrote in news:#
$e************@TK2MSFTNGP15.phx.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/
"Programming 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*****@nospamforme.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 misunderstanding :-)

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.GetValue() ?


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.com>
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.com> a écrit dans le message de news:
MP************************@msnews.microsoft.com...
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<>)fieldInfo.GetValue(this);

TIA

Joanna

--
Joanna Carter
Consultant Software Engineer
Nov 16 '05 #9
Joanna Carter (TeamB) <jo*****@nospamforme.com> wrote:
"Jon Skeet [C# MVP]" <sk***@pobox.com> a écrit dans le message de news:
MP************************@msnews.microsoft.com...
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.com>
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
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...
5
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...
2
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)...
0
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) {...
3
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...
2
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...
1
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...
7
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,...
1
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...
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
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...
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.