PC Review


Reply
Thread Tools Rate Thread

Does FieldInfo.GetValue return the actual field?

 
 
Joanna Carter \(TeamB\)
Guest
Posts: n/a
 
      15th Feb 2005
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


 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      15th Feb 2005
Joanna Carter (TeamB) <(E-Mail Removed)> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Joanna Carter \(TeamB\)
Guest
Posts: n/a
 
      15th Feb 2005
"Jon Skeet [C# MVP]" <(E-Mail Removed)> a écrit dans le message de news:
(E-Mail Removed)...

> 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


 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      15th Feb 2005
Joanna Carter (TeamB) <(E-Mail Removed)> wrote:
> "Jon Skeet [C# MVP]" <(E-Mail Removed)> a écrit dans le message de news:
> (E-Mail Removed)...
>
> > 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Joanna Carter \(TeamB\)
Guest
Posts: n/a
 
      15th Feb 2005
"Jon Skeet [C# MVP]" <(E-Mail Removed)> a écrit dans le message de news:
(E-Mail Removed)...

> 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


 
Reply With Quote
 
Chad Z. Hower aka Kudzu
Guest
Posts: n/a
 
      15th Feb 2005
"Joanna Carter \(TeamB\)" <(E-Mail Removed)> wrote in news:#
$(E-Mail Removed):
> 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/
 
Reply With Quote
 
Joanna Carter \(TeamB\)
Guest
Posts: n/a
 
      15th Feb 2005
"Chad Z. Hower aka Kudzu" <(E-Mail Removed)> a écrit dans le message de news:
Xns95FEADB45286Ccpub@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


 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      15th Feb 2005
Joanna Carter (TeamB) <(E-Mail Removed)> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Joanna Carter \(TeamB\)
Guest
Posts: n/a
 
      15th Feb 2005
"Jon Skeet [C# MVP]" <(E-Mail Removed)> a écrit dans le message de news:
(E-Mail Removed)...

> 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


 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      16th Feb 2005
Joanna Carter (TeamB) <(E-Mail Removed)> wrote:
> "Jon Skeet [C# MVP]" <(E-Mail Removed)> a écrit dans le message de news:
> (E-Mail Removed)...
>
> > 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Return actual range not cell value in variable Richhall Microsoft Excel Worksheet Functions 1 18th Sep 2009 02:34 PM
VLOOKUP - return actual formula kvc Microsoft Excel Discussion 3 11th Dec 2007 08:31 PM
How to return actual cell contents? =?Utf-8?B?VG9tIEhheWFrYXdh?= Microsoft Excel Worksheet Functions 2 21st Oct 2004 07:25 PM
FieldInfo GetValue() Cezar Microsoft C# .NET 5 10th Nov 2003 10:26 PM
Return Actual Number of Working Days For a Specified Period Darren Franklin Microsoft Access Queries 1 30th Oct 2003 01:42 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:03 AM.