Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / February 2008

Tip: Looking for answers? Try searching our database.

setting Struct-s via Reflection

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Edgile - 27 Feb 2008 16:22 GMT
Hello guys,

Due to special requirements, I must create my own
serialization/deserialization solution using .Net2.0. The idea looks straight
forward: I use Reflection to navigate through the objects and get/set their
fields (and also add/retrieve my own special stuff). However,
FieldInfo.Set(object obj, object value) does not work on Structs (see sample
code below). If I understand the things right, the underlying reason is that
structs are value types and passed by value. So in the body of FieldInfo.Set,
a copy of the input struct is set, while the original one outside the method
remains intact. Do you have any idea, how to overcome the issue? How does
BinarySerializer do that?

Here is an example:

System.Drawing.Point p = new System.Drawing.Point(10, 20);
FieldInfo xField = p.GetType().GetField("x", BindingFlags.Instance |
                                                             
BindingFlags.NonPublic);
xField.SetValue(p, 100);
Console.WriteLine(p.X); // Still returns 10

Thanx in advance!
Jon Skeet [C# MVP] - 27 Feb 2008 16:32 GMT
> Due to special requirements, I must create my own
> serialization/deserialization solution using .Net2.0. The idea looks straight
[quoted text clipped - 15 lines]
> xField.SetValue(p, 100);
> Console.WriteLine(p.X); // Still returns 10

Try this (untested):

System.Drawing.Point p = new System.Drawing.Point(10, 20);
FieldInfo xField = p.GetType().GetField("x", BindingFlags.Instance |
                                                             
BindingFlags.NonPublic);

object[] boxed = new object[] {p};

xField.SetValue(boxed[0], 100);

p = (Point)boxed[0];
Console.WriteLine(p.X); // Still returns 10

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.