MemberInfo

MemberInfo also has a property called MemberType of type MemberTypes.

This is a flags enum with these values:

A MemberInfo object has a Name property and two Type properties:

DeclaringTypeReturns the Type that defines the member
ReflectedTypeReturns 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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.