473,326 Members | 2,196 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,326 software developers and data experts.

FieldInfo.SetValue()

FieldInfo.SetValue() doesn't appear to work when trying to set the values of fields of a struct. Why? Because structs are passed by value and not reference?
--
Thanks,
Glen
Nov 16 '05 #1
3 14359
Yeah that's interesting actually.
If you box the struct first it works, eg.

public struct test
{
public string name;
}

static void Main()
{
test t = new test();
FieldInfo fi = t.GetType().GetField("name");
object p = (object)t;
fi.SetValue(p, "blah");
t = (test)p;
Console.WriteLine(t.name);
Console.ReadLine();
}
"Glen" <Gl**@discussions.microsoft.com> wrote in message
news:BD**********************************@microsof t.com...
FieldInfo.SetValue() doesn't appear to work when trying to set the values of fields of a struct. Why? Because structs are passed by value and not
reference? --
Thanks,
Glen

Nov 16 '05 #2
Glen <Gl**@discussions.microsoft.com> wrote:
FieldInfo.SetValue() doesn't appear to work when trying to set the
values of fields of a struct. Why?
You need to take boxing into account. Here's an example:

using System;
using System.Reflection;

struct Foo
{
public int x;

public void Show()
{
Console.WriteLine(x);
}
}

public class Test
{
static void Main()
{
Foo foo = new Foo();
foo.x = 3;
foo.Show();

FieldInfo fi = typeof(Foo).GetField("x");

object boxed = foo;
fi.SetValue (boxed, 5);
foo = (Foo) boxed;

foo.Show();
}
}
Because structs are passed by value and not reference?


Everything is passed by value by default in C#.
See http://www.pobox.com/~skeet/csharp/parameters.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 #3
"Glen" <Gl**@discussions.microsoft.com> wrote in
news:BD**********************************@microsof t.com...
FieldInfo.SetValue() doesn't appear to work when trying to set the values

of fields of a struct. Why? Because structs are passed by value and not
reference?

If you box a struct, it gets copied: the original version lives in the
stack, while the boxed version is in the heap, so after boxing, you actually
got 2 structures. If the box'ed version is modified, this doesn't touch the
original version. As FieldInfo.SetValue takes an 'object' parameter, the
struct gets boxed when you pass it, and the stack-version never gets
changed. It's a bit weird, but I think the behaviour is correct.

Every boxing/unboxing creates a copy of the data. Consider the following:
In this sample, neither the boxed, nor the stack version of "foo" is
changed!

using System;
using System.Reflection;

struct Foo
{
public int x;

public void SetX(int i) { x = i; }

public void Show() { Console.WriteLine(x); }
}

public class Test
{
static void Main()
{
Foo foo = new Foo();
foo.x = 3;
foo.Show();

object boxed = foo;
((Foo)boxed).SetX(5); // the cast unboxes "boxed", and only the
member of the temporary unboxed version is modified, so this is really a
no-op
((Foo)boxed).Show();
foo = (Foo) boxed;

foo.Show();

Console.ReadLine();
}
}

Niki
Nov 16 '05 #4

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

Similar topics

0
by: James Hadwen | last post by:
Hi, I'm trying to do something with reflection that's a little wierd. The idea is that I call for a field to be updated on a base class. Using reflection it first updates the value, and then...
6
by: ORC | last post by:
I will use FieldInfo to import values from a XML file into the fields of an objects. But how to convert to the proper type like in this: public void Load(object MyClass) { foreach( FieldInfo...
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...
5
by: Dennis | last post by:
I have an array "t" where each element is a structure of type "testing". I want to use the FieldInfo class to set the values of the array element fields. I can read the field properties and...
1
by: jw56578 | last post by:
Im trying to use FieldInfo.SetValue to set the values of fields in a class, the class is using reflection on itself to popluate its own fields. but i keep getting this error. Object type cannot...
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...
0
by: kernell | last post by:
Hi everyone, i'm trying to initialize all control of a UserControl to is appropriate class. The value is set but the event won't raise anymore. See the example: 'Not working 'Normally...
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.