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

  • Home
  • SEARCH
  • 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 7656223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:42:44+00:00 2026-05-31T12:42:44+00:00

I have been reading here at stackoverflow how to write to a class var

  • 0

I have been reading here at stackoverflow how to write to a class var member using reflection. I use something like:

typeof(MyClass).GetField("myvar", BindingFlags.Public | BindingFlags.Instance).SetValue(instancie, 10);

This works for classes but if I do the same for a Struct instead of a class when reading myvar I allways get 0 (default construction value for int). This is the code I use:

struct MyStruct
{
    public int myvar;
}

MyStruct instance=new MyStruct();

typeof(MyStruct).GetField("myvar", BindingFlags.Public | BindingFlags. BindingFlags.Instance).SetValue(instance, 10);

Do anybody know why could this be hapenning?

  • 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-05-31T12:42:45+00:00Added an answer on May 31, 2026 at 12:42 pm

    When you pass in “instance”, that is a box – which is a wrapped clone of the data, that you later discard.

    To use reflection here:

    object obj = instance; // box
    blah.SetValue(obj, value); // mutate inside box
    instance = (YourType)obj; // unbox
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hi every1 i have been reading lots of posts here about how to use
I have been reading hundreds of posts on ASP.NET blogs and questions on StackOverflow
I have been reading up on File's Owner here on Stack Overflow and a
I have been reading here and there about multiple JQuery selectors but I couldn't
I have been reading through various questions on here, as I am learning VHDL
I have been reading up on using Shibboleth 2.0 as a Single Sign On
I have been reading this page here from MSDN regarding the XOR operator and
I've only just really started android developing and have been reading about services here
I have been reading lots of articles and also questions here on SO but
I have been reading through the Java tutorial on RMI. I like the approach

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.