October 13, 2010

Static Property Reflection Sample

This sample enumerates over the "TargetClass" looking for all static properties that return the specified "ReturnType"
public static IEnumerable 
EnumerateReturnTypeProperties()
{
    Type type = typeof(TargetClass);
    PropertyInfo[] props = type.GetProperties();
    foreach (PropertyInfo prop in props)
    {
        // null as 1st GetValue parameter implies
        // only static properties enumerated
        ReturnType xxx = prop.GetValue(null, null)
            as ReturnType;
        if (xxx == null)
            continue;
        yield return xxx;
    }
}

No comments: