Reputation: 4526
The title says all: Is there a way to set C# readonly auto-implemented Propeties through reflection?
typeof(Change)
.GetProperty("ChangeType", BindingFlags.Instance | BindingFlags.Public)
.SetValue(myChange, change.ChangeType.Transform(),null);
This line gives me an error : System.ArgumentException - {"Property set method not found."}. The thing is I can't use GetField because there are no fields.
Before you ask, I'm doing this because I need to "complement" an already finished library and I have no access to its code.
Upvotes: 1
Views: 706
Reputation: 10211
the obvious conclusion would be that Change.ChangeType
does not have public instance setter.
Upvotes: 1
Reputation: 241641
This should work, so there is something that you are not telling us. Are you sure that it's an auto-implemented property? An explanation consistent with what you are seeing is that the property is not auto-implemented and does not have a setter.
That is,
public class Foo { public int Bar { get; set; } }
typeof(Foo).GetProperty("Bar").SetValue(foo, 42);
will succeed but
public class Foo { public int Bar { get { return 42; } } }
typeof(Foo).GetProperty("Bar").SetValue(foo, 42);
will not and it will produce the exception with the message that you are seeing.
Upvotes: 4