What type are you interested in? The return type of the method/property/event etc?
If so, I don't think there's anything in MemberInfo
to let you get at it directly - you'll need to cast and use MethodInfo.ReturnType
, PropertyInfo.PropertyType
, FieldInfo.FieldType
, EventInfo.EventHandlerType
and any others I've forgotten. (Remember that types themselves can be members. Not sure what you'll want to do with them!)
EDIT: If you're interested in whether a specific Type represents either MyType or some subclass, then use Type.IsAssignableFrom:
if (typeof(MyType).IsAssignableFrom(type))
EDIT: Now that we know you want properties, it's easy - use GetProperties instead of GetMembers. I like doing reflection with LINQ:
var query = from type in assembly.GetTypes()
from property in type.GetProperties()
where typeof(MyType).IsAssignableFrom(property.PropertyType)
select new { Type=type, Property=property };
foreach (var entry in query)
{
Console.WriteLine(entry);
}
If you're not a fan of LINQ:
foreach (Type t in a.GetTypes())
foreach (PropertyInfo pi in t.GetProperties())
if (typeof(MyType).IsAssignableFrom(pi.PropertyType))
Console.WriteLine("Found a property that is MyType");
Note that you might want to specify binding flags to get non-public properties etc.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…