Re: Does FieldInfo.GetValue return the actual field?

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Jon Skeet [C# MVP] (skeet_at_pobox.com)
Date: 02/15/05


Date: Tue, 15 Feb 2005 20:23:46 -0000

Joanna Carter (TeamB) <joannac@nospamforme.com> wrote:
> "Jon Skeet [C# MVP]" <skeet@pobox.com> a écrit dans le message de news:
> MPG.1c7c5a267745915e98bd38@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 - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Relevant Pages