Mega Code Archive

 
Categories / C# Book / 09 Reflection
 

0625 MemberInfo

MemberInfo also has a property called MemberType of type MemberTypes. This is a flags enum with these values: All Custom Field NestedType TypeInfo Constructor Event Method Property A MemberInfo object has a Name property and two Type properties: DeclaringType Returns the Type that defines the member ReflectedType Returns the Type upon which GetMembers was called The two differ when called on a member that's defined in a base type: Declaring Type returns the base type, whereas ReflectedType returns the subtype. The following example highlights this: using System; using System.Reflection; using System.Collections.Generic; class Program { static void Main() { MethodInfo test = typeof(Program).GetMethod("ToString"); MethodInfo obj = typeof(object).GetMethod("ToString"); Console.WriteLine(test.DeclaringType); // System.Object Console.WriteLine(obj.DeclaringType); // System.Object Console.WriteLine(test.ReflectedType); // Program Console.WriteLine(obj.ReflectedType); // System.Object Console.WriteLine(test == obj); // False } } The output: System.Object System.Object Program System.Object False