C# FieldInfo DeclaringType

Description

FieldInfo DeclaringType Gets the class that declares this member.

Syntax

FieldInfo.DeclaringType has the following syntax.


public abstract Type DeclaringType { get; }

Example

Gets the class that declares this member.


using System;/*  w w w .j a v  a  2s.  c o  m*/
using System.IO;
using System.Reflection;

namespace MyNamespace1 
{
    interface i 
    {
        int MyVar() ;
    };
    // DeclaringType for MyVar is i. 

    class A : i 
    {
        public int MyVar() { return 0; }
    };
    // DeclaringType for MyVar is A. 

    class B : A 
    {
        new int MyVar() { return 0; }
    };
    // DeclaringType for MyVar is B. 

    class C : A 
    {
    };
    // DeclaringType for MyVar is A.

}

namespace MyNamespace2 
{
    class Mymemberinfo 
    { 

        public static void Main(string[] args) 
        { 
            Type MyType =Type.GetType("System.IO.BufferedStream");
            MemberInfo[] Mymemberinfoarray = MyType.GetMembers();
            Console.WriteLine("\nThere are {0} members in {1}.", Mymemberinfoarray.Length, MyType.FullName); 
            foreach (MemberInfo Mymemberinfo in Mymemberinfoarray) 
            {  
                Console.WriteLine("Declaring type of {0} is {1}.", Mymemberinfo.Name, Mymemberinfo.DeclaringType); 
            }
        }
    }
}

namespace MyNamespace3 
{
    class A 
    {
        virtual public void M () {}
    }
    class B: A 
    {
        override public void M () {}
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Reflection »




EventInfo
FieldInfo
MemberInfo
MethodInfo
ParameterInfo
TypeInfo