How to set value of property where there is no setter

I do not suggest doing this on your application but for testing purpose it may be usefull… Assuming you have: public class MyClass { public int MyNumber {get;} } You could do this if its for test purpose, I would not suggest to use this in your runtime code: var field = typeof(MyClass).GetField(“<MyNumber>k__BackingField”, BindingFlags.Instance | … Read more

Automatically implemented property in struct can not be assigned

From the C# Specification: 10.7.3 Automatically implemented properties When a property is specified as an automatically implemented property, a hidden backing field is automatically available for the property, and the accessors are implemented to read from and write to that backing field. [Deleted] Because the backing field is inaccessible, it can be read and written … Read more

How to find out if a property is an auto-implemented property with reflection?

You could check to see if the get or set method is marked with the CompilerGenerated attribute. You could then combine that with looking for a private field that is marked with the CompilerGenerated attribute containing the name of the property and the string “BackingField”. Perhaps: public static bool MightBeCouldBeMaybeAutoGeneratedInstanceProperty( this PropertyInfo info ) { … Read more

What are Automatic Properties in C# and what is their purpose?

Automatic Properties are used when no additional logic is required in the property accessors. The declaration would look something like this: public int SomeProperty { get; set; } They are just syntactic sugar so you won’t need to write the following more lengthy code: private int _someField; public int SomeProperty { get { return _someField;} … Read more