Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine property types using reflection?

How would I test a property of a type to see if it is a specified type?

EDIT: My goal is to examine an assembly to see if any of the types in that assembly contain properties that are MyType (or inherited from MyType).

Here is the track I've gone down...

AssemblyName n = new AssemblyName(); n.CodeBase = "file://" + dllName; Assembly a = AppDomain.CurrentDomain.Load(n);  foreach (Type t in a.GetTypes())     foreach (PropertyInfo pi in t.GetProperties())         if ( pi.PropertyType is MyType ) // warning CS0184             Console.WriteLine("Found a property that is MyType"); 

This compiles with warning CS0184: The given expression is never of the provided ('MyType') type

like image 289
Ed Guiness Avatar asked Feb 06 '09 14:02

Ed Guiness


People also ask

How do I get a property type from PropertyInfo?

Use PropertyInfo. PropertyType to get the type of the property.

How do you check if property is of type is a class?

Examples. The following example creates an instance of a type and indicates whether the type is a class. type MyDemoClass = class end try let myType = typeof<MyDemoClass> // Get and display the 'IsClass' property of the 'MyDemoClass' instance. printfn $"\nIs the specified type a class? {myType.

Is GetType reflection?

Or: typeof(MyType); To get the Type , then when one queries the info about the type e.g. getting properties, fields, attributes etc. they are certainly performing reflection.


2 Answers

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.

like image 195
Jon Skeet Avatar answered Sep 21 '22 18:09

Jon Skeet


Ok, maybe I'm missing something stupid, but shouldn't it be:

if ( pi.PropertyType == typeof(MyType )) 

???

like image 26
BFree Avatar answered Sep 20 '22 18:09

BFree