Does FieldInfo.GetValue return the actual field?

  • Thread starter Thread starter Joanna Carter \(TeamB\)
  • Start date Start date
J

Joanna Carter \(TeamB\)

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 (TeamB) said:
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...
 
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 (TeamB) said:
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:

<snip>

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

public struct TestType<T>

((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?
 
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 \(TeamB\) said:
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/
 
Joanna Carter (TeamB) said:
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
 
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 (TeamB) said:
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.
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 :)
 
Back
Top