Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1.1K
Views
Set Property of Instance without Setter

Suppose I have a class and an interface that looks like this:

[Serializable]
public class MyClass : IClass
{
   public int Prop => 42;
}

public interface IClass
{
   int Prop { get; }
}

As you can see there's no Setter. But I was hoping to use reflection to change the Prop property but I can't seem to do it. Here's what I have so far:

var property = typeof(MyClass).GetProperty("Prop", BindingFlags.Instance | BindingFlags.Public);
property.SetValue(instaneOfClass, 31);

I keep getting this error: System.ArgumentException: Property set method not found.

Shouldn't this work? How can I set the property without using a setter?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Auto implemented properties have backing fields named <PropName>k__BackingField.

In your case you would access this backing field like this:

var property = typeof(MyClass).GetField("<Prop>k__BackingField", BindingFlags.Instance | BindingFlags.NonPublic);
property.SetValue(instanceOfClass, 31);

Note that I'm calling GetField() instead of GetProperty(), and using BindingFlags.NonPublic instead of BindingFlags.Public, since the backing field is private.

However, since this is an expression bodied member (lambda syntax), no backing field is generated. Instead of relying on another member, the getter simply returns the result of the given expression, and the compiler doesn't need to generate a backing field.

Implementing your property with an explicit getter will generate a backing field, and the above code will work.

public class MyClass : IClass
{
   public int Prop { get; } = 42;
}
over 4 years ago · Santiago Trujillo Report

0

If you specify a getter you'll have a compiler generated field that contains the property name. You can then modify the value of this field using reflection (GetFields, SetValue):

public class Foo        {
        public Foo() => Bar = 42;
        public int Bar { get; }        

        static void Main()  {
           var f = new Foo();
           Console.WriteLine(f.Bar); // ==> 42;
           f.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic).First(x => x.Name.Contains("Bar")).SetValue(f, 43);
           Console.WriteLine(f.Bar); // ==> 43;
  }
}

It does not work for a computed property as in your example.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!