Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 138415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:15:38+00:00 2026-05-11T07:15:38+00:00

Code snippet: Dim target As Object ‘ target gets properly set to something of

  • 0

Code snippet:

Dim target As Object ' target gets properly set to something of the desired type Dim field As FieldInfo = target.GetType.GetField('fieldName', _   BindingFlags.Instance Or BindingFlags.Public Or BindingFlags.NonPublic) field.SetValue(target,newValue) 

This snippet works perfectly IF target is set to an instance of a CLASS.

However, if target is set to an instance of a STRUCTURE, the code does not actually change the value of the field. No error, but the value remains unchanged.

And, oddly, if I’m stepping through code, watch the SetValue fail to do anything, and immediately go to the Immediate window and type exactly the same SetValue operation, that works.

Any suggestions on what’s going on and how to actually change the field IN CODE?

Edit:

Per request from Jon Skeet, actual code:

Private Shared Function XmlDeserializeObject(ByVal objectType As Type, _         ByVal deserializedID As String) As Object     Dim result As Object     result = CreateObject(objectType)      mXmlR.ReadStartElement()     Do While mXmlR.IsStartElement _     AndAlso mXmlR.Name <> elementItem         Dim field As FieldInfo = result.GetType.GetField(FullName, _             BindingFlags.Instance Or BindingFlags.Public Or BindingFlags.NonPublic)         field.SetValue(result, XmlDeserialize(field.FieldType))     Loop      Return result End Function 

External variables and called routines:
* mXmlR is an XmlTextReader, and is properly initialized and positioned (else this would not work on classes)
* CreateObject works (ditto)
* XmlDeserialize mostly works, and at the point in question is handling an integer just fine. The only known problem is with structures.

As for how I’m checking the value, I’m mostly looking at the Locals window, but I’ve also used print statements in the Immediate window, and I’m running an NUnit test which is failing because of this problem – while the equivalent test with a class, rather than a structure, passes.

Here’s the test.

<Serializable()> Private Structure SimpleStructure     Public MemberOne As Integer End Structure  <Test()> Sub A016_SimpleStructure()     Dim input As New SimpleStructure     input.MemberOne = 3     Dim st As String = Serialize(input)     Debug.Print(st)     Dim retObject As Object = Deserialize(st)     Assert.IsNotNull(retObject)     Assert.IsInstanceOfType(GetType(SimpleStructure), retObject)     Assert.AreEqual(input.MemberOne, DirectCast(retObject, SimpleStructure).MemberOne) End Sub 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T07:15:38+00:00Added an answer on May 11, 2026 at 7:15 am

    Working with your original sample, I agree that it works in C# but not in VB! If you use Reflector or ILDasm you will see that the call to Field.SetValue(target, …) is actually compiled (in VB) as:

    field.SetValue(RuntimeHelpers.GetObjectValue(target), ...) 

    GetObjectValue ‘Returns a boxed copy of obj if it is a value class; otherwise obj itself is returned.’ I.e. the value is being set on a copy of your struct!

    This link gives the explanation (such as it is). The workaround is to declare target as System.ValueType instead of Object. I’m not sure if that actually helps in your real-life code: you may need a messy type test to be able to handle value types separately from reference types.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have those code snippet that checks the fans table and gets everyone who
I have got a code snippet as follows: Dim fstream = new filestream(some file
I run the following code snippet. Public Shared Sub DisplayDnsConfiguration() Dim adapters As NetworkInterface()
Code Snippet: ShippingPeriod[] arrShippingPeriods; . . . List<ShippingPeriod> shippingPeriods = ShippingPeriodList.ToList<ShippingPeriod>(); The Last line
Code snippet (normal pointer) int *pi = new int; int i = 90; pi
Code snippet.. if (regionalApprover == null) { throw new Exception(string.Format(The regional approver for {0}
The code snippet: C2DMessaging.java public class C2DMessaging { public static final String EXTRA_SENDER =
This code snippet is from C# in Depth static bool AreReferencesEqual<T>(T first, T second)
Which code snippet will give better performance? The below code segments were written in
Relevant Code Snippet: public class MyPreference extends Preference { public MyPreference(Context context, AttributeSet attrs)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.