tags:

views:

71

answers:

1

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)

+5  A: 

Check .IsDefined(typeof(CompilerGeneratedAttribute), false); on them.

Marc Gravell
Yup seems like the better choice :-)
Steffen
And it works like a charm - my test suite finally passed :-)
Steffen