My first issue was I didn't now that when you reflect a type, you can access all the inherited public scoped methods and properties and fields, but you can't access any inherited private methods, properties, and fields. Once I had that figured out, I just had to figure out what the name of the field was.
I downloaded Red Gate's .Net Reflector (an awesome tool) and dissambled my sample test program. I then looked at the property generated by the compiler,
// Fields
[CompilerGenerated]
private long <CustId>k__BackingField;
// Properties
public long CustId
[CompilerGenerated]
get
{
return this.<CustId>k__BackingField;
}
private [CompilerGenerated]
set
{
this.<CustId>k__BackingField = value;
}
And as you can quickly see, the name of the backing field for an automatic property is as follows,
WinPart parent = new WinPart(_mainForm);
parent.GetType().GetField("<CustId>k__BackingField", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).SetValue(this, newCustId);
No comments:
Post a Comment