Determine if FieldInfo is compiler generated backingfield

Posted by Steffen on Stack Overflow See other posts from Stack Overflow or by Steffen
Published on 2010-04-14T15:52:12Z Indexed on 2010/04/14 15:53 UTC
Read the original article Hit count: 156

Filed under:
|

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)

© Stack Overflow or respective owner

Related posts about c#

Related posts about reflection