The title pretty much says it all, how do I know if I'm getting a compiler generated backingfield for a {get; set;} property ?
I'm running this code to get my FieldInfos:
Class MyType
{
private int foo;
public int bar {get; private set; }
}
Type type = TypeOf(MyType);
foreach (FieldInfo fi in type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.NonPublic))
{
// Gets both foo and bar, however bar is called <bar>k__backingfield.
}
so the question is, can I somehow detect that the FieldInfo is a backingfield, without relying on checking its name ? (Which is pretty undocumented, and could be broken in next version of the framework)
Check .IsDefined(typeof(CompilerGeneratedAttribute), false);
on them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With