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 8796685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:40:46+00:00 2026-06-13T23:40:46+00:00

I’m trying to do some dynamic text to object parsing, however, I have run

  • 0

I’m trying to do some dynamic text to object parsing, however, I have run into a snag when creating and setting nested property values for structs.

If I have a property in an object that is a struct, whenever I use reflection to obtain the struct object and set any of it’s properties/fields, the value on the object is not changed. Take the object graph below.

public struct MyStruct
{
    public int MyIntProp {get;set;}
}

public class MyObj
{
    public MyStruct NestedStruct {get;set;}
}

PropertyInfo pInfo = (myObj.GetType()).GetProperty("NestedStruct");
object nestedStruct = pInfo.GetValue(myObj); // This is the nested struct however it is only a copy not the actual object
PropertyInfo intInfo = (pInfo.PropertyType).GetProperty("MyIntProp");
intInfo.SetValue(nestedStruct, 23); // this sets the MyIntProp on the nestedStruct, but it is not set on the myObj.NestedStruct.  My goal is to set object on that NestedStruct via reflection.

When I use reflection to obtain the NestedStruct property and then set the MyIntProp on that struct, the original MyObj.NestedStruct.MyIntProp doesn’t change. Naturally I attribute this to the fact that the struct is a value type and not a reference type.

So the real question is how I can use reflection to obtain the reference to a value type.

  • 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. Editorial Team
    Editorial Team
    2026-06-13T23:40:47+00:00Added an answer on June 13, 2026 at 11:40 pm

    If I have a property in an object that is a struct, whenever I use reflection to obtain the struct object and set any of it’s properties/fields, the value on the object is not changed.

    Well, no – because on this line:

    object nestedStruct = pInfo.GetValue(myObj);
    

    … you’re copying the value, as it’s a value type. You’re also boxing it – which is fine, because it means when you mutate it in the intInfo.SetValue call, it’s the boxed copy which is modified. (If you were boxing it at the point of calling intInfo.SetValue, that would really be hopeless.)

    After you’ve made the change within the boxed copy, you then need to set it back into the property of the object:

    pInfo.SetValue(myObject, nestedStruct);
    

    It would be instructive to try doing what you’re doing not using reflection:

    MyObj myObj = new MyObj();
    myObj.NestedStruct.MyIntProp = 23;
    

    The compiler will give you an error on the last line, explaining that it’s a silly thing to do:

    Test.cs(20,9): error CS1612: Cannot modify the return value of ‘MyObj.NestedStruct’ because it is not a variable

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

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I have a reasonable size flat file database of text documents mostly saved in

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.