[Serializable] |
This class introduces the basic functionality that all members provide.
DeclaringType | Read-only Gets the class that declares this member. |
MemberType | Read-only Gets the type of this member, such as field, method, and so on. |
Name | Read-only Gets the name of this member. |
ReflectedType | Read-only Gets the class object that was used to obtain this instance of MemberInfo. |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
GetCustomAttributes | Overloaded:GetCustomAttributes(bool inherit) When overridden in a derived class, returns an array of all of the custom attributes. |
GetCustomAttributes | Overloaded:GetCustomAttributes(Type attributeType, bool inherit) When overridden in a derived class, returns an array of custom attributes identified by Type. |
GetHashCode (inherited from System.Object) |
See base class member description: System.Object.GetHashCode Derived from System.Object, the primary base class for all objects. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
IsDefined | When overridden in a derived class, indicates whether one or more instance of attributeType is defined on this member. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
ctor #1 | Default constructor. This constructor is called by derived class constructors to initialize state in this type. Initializes a new instance of the MemberInfo class. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
protected MemberInfo(); |
public abstract Type DeclaringType {get;}
|
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.
The following example uses DeclaringType to retrieve the member names of the System.IO.BufferedStream class, along with the class in which those members are declared.
using System; using System.IO; using System.Reflection; class Mymemberinfo { public static void Main(string[] args) { Console.WriteLine ("\nReflection.MemberInfo"); //Get the Type and MemberInfo. Type MyType =Type.GetType("System.IO.BufferedStream"); MemberInfo[] Mymemberinfoarray = MyType.GetMembers(); //Get and display the DeclaringType method. Console.Write("\nThere are {0} members in ", Mymemberinfoarray.Length); Console.Write("{0}.", MyType.FullName); foreach (MemberInfo Mymemberinfo in Mymemberinfoarray) { Console.Write("\n" + Mymemberinfo.Name + " declaring type - " + Mymemberinfo.DeclaringType); } } }
This code produces the following output:
Reflection.MemberInfo
There are 31 members in System.IO.BufferedStream.
WriteByte declaring type - System.IO.BufferedStream
Write declaring type - System.IO.BufferedStream
ReadByte declaring type - System.IO.BufferedStream
Read declaring type - System.IO.BufferedStream
SetLength declaring type - System.IO.BufferedStream
Seek declaring type - System.IO.BufferedStream
EndWrite declaring type - System.IO.Stream
BeginWrite declaring type - System.IO.Stream
EndRead declaring type - System.IO.Stream
BeginRead declaring type - System.IO.Stream
Flush declaring type - System.IO.BufferedStream
Close declaring type - System.IO.BufferedStream
set_Position declaring type - System.IO.BufferedStream
get_Position declaring type - System.IO.BufferedStream
get_Length declaring type - System.IO.BufferedStream
get_CanWrite declaring type - System.IO.BufferedStream
get_CanSeek declaring type - System.IO.BufferedStream
get_CanRead declaring type - System.IO.BufferedStream
InitializeLifetimeService declaring type - System.MarshalByRefObject
GetHashCode declaring type - System.Object
Equals declaring type - System.Object
ToString declaring type - System.Object
GetLifetimeService declaring type - System.MarshalByRefObject
GetType declaring type - System.Object
.ctor declaring type - System.IO.BufferedStream
.ctor declaring type - System.IO.BufferedStream
CanRead declaring type - System.IO.BufferedStream
CanWrite declaring type - System.IO.BufferedStream
CanSeek declaring type - System.IO.BufferedStream
Length declaring type - System.IO.BufferedStream
Position declaring type - System.IO.BufferedStream
In the following code example, when B overrides virtual method M from A, it essentially redefines (or redeclares) this method. Therefore, B.M's MethodInfo reports the declaring type as B rather than A, even though A is where this method was originally declared.
class A { virtual public void M () {} } class B: A { override public void M () {} }
public abstract MemberTypes MemberType {get;}
|
using System; using System.Reflection; class Mymemberinfo { public static int Main() { Console.WriteLine ("\nReflection.MemberInfo"); //Get the Type and MemberInfo. Type MyType = Type.GetType("System.Reflection.PropertyInfo"); MemberInfo[] Mymemberinfoarray = MyType.GetMembers(); //Get the MemberType method and display the elements. Console.Write("\nThere are {0} members in ", Mymemberinfoarray.GetLength(0)); Console.Write("{0}.", MyType.FullName); for (int counter = 0; counter < Mymemberinfoarray.Length; counter++) { Console.Write("\n" + counter + ". " + Mymemberinfoarray[counter].Name + " Member type - " + Mymemberinfoarray[counter].MemberType.ToString()); } return 0; } } /* This code produces the following output: Reflection.MemberInfo There are 52 members in System.Reflection.PropertyInfo. 37. GetCanRead Member type - Method 38. GetCanWrite Member type - Method 39. MemberType Member type - Property 40. PropertyType Member type - Property */
public abstract string Name {get;}
|
To get the Name property, get the class Type. From the Type, get the MemberInfo array. From a MemberInfo element of the array, obtain the Name property.
using System; using System.Reflection; class Mymemberinfo { public static int Main() { Console.WriteLine ("\nReflection.MemberInfo"); //Get the Type and MemberInfo. Type MyType = Type.GetType("System.Empty"); MemberInfo[] Mymemberinfoarray = MyType.GetMembers(); //Get and display the DeclaringType method. Console.Write("\nThere are {0} members in ", Mymemberinfoarray.GetLength(0)); Console.Write("{0}.", MyType.FullName); foreach (MemberInfo Mymemberinfo in Mymemberinfoarray) { Console.Write("\n" + Mymemberinfo.Name + " declaring type - " + Mymemberinfo.DeclaringType); } return 0; } } /* This code produces the following output: Reflection.MemberInfo There are 6 members in System.Empty. Value declaring type - System.Empty GetObjectData declaring type - System.Empty GetHashCode declaring type - System.Object Equals declaring type - System.Object ToString declaring type - System.Empty GetType declaring type - System.Object */
public abstract Type ReflectedType {get;}
|
In order to obtain a MethodInfo object:
using System; using System.IO; using System.Reflection; class Mymemberinfo { public static void Main(string[] args) { Console.WriteLine ("\nReflection.MemberInfo"); //Get the Type and MemberInfo Type MyType =Type.GetType("System.IO.BufferedStream"); MemberInfo[] Mymemberinfoarray = MyType.GetMembers(); //Get and display the DeclaringType method Console.Write("\nThere are {0} members in ", Mymemberinfoarray.Length); Console.Write("{0}.", MyType.FullName); foreach (MemberInfo Mymemberinfo in Mymemberinfoarray) { Console.Write("\n" + Mymemberinfo.Name + " reflected type - " + Mymemberinfo.ReflectedType); } } }This code produces the following output:
Reflection.MemberInfo
There are 31 members in System.IO.BufferedStream.
WriteByte reflected type - System.IO.BufferedStream
~MemberInfo(); |
inherit
using System; using System.Reflection; // Define a custom attribute with one named parameter. [AttributeUsage(AttributeTargets.All)] public class MyAttribute : Attribute { private string myName; public MyAttribute(string name) { myName = name; } public string Name { get { return myName; } } } // Define a class which has the custom attribute associated with one of its members. public class MyClass1 { [MyAttribute("This is an example attribute")] public void MyMethod(int i) { return; } } public class MemberInfo_GetCustomAttributes { public static void Main() { try { // Get the type of the class 'MyClass1'. Type myType = typeof(MyClass1); // Get the members associated with the class 'MyClass1'. MemberInfo[] myMembers = myType.GetMembers(); // Display the attributes for each of the members of the class 'MyClass1'. for(int i = 0; i < myMembers.Length; i++) { Object[] myAttributes = myMembers[i].GetCustomAttributes(false); if(myAttributes.Length > 0) { Console.WriteLine("\nThe attributes for the member {0} are : \n", myMembers[i]); for(int j = 0; j < myAttributes.Length; j++) Console.WriteLine("The type of the attribute is : {0}", myAttributes[j]); } } } catch(Exception e) { Console.WriteLine("Exception Caught! "+e.Message); } } }
attributeType
inherit
Exception Type | Condition |
---|---|
TypeLoadException | If the custom attribute type can not be loaded. |
public virtual int GetHashCode(); |
public Type GetType(); |
attributeType
inherit
using System; using System.Reflection; // Define a custom attribute with one named parameter. [AttributeUsage(AttributeTargets.All)] public class MyAttribute : Attribute { private string myName; public MyAttribute(string name) { myName = name; } public string Name { get { return myName; } } } // Define a class which has the custom attribute associated with one of its members. public class MyClass1 { [MyAttribute("This is an example attribute")] public void MyMethod(int i) { return; } } public class MemberInfo_GetCustomAttributes_IsDefined { public static void Main() { try { // Get the type of the class 'MyClass1'. Type myType = typeof(MyClass1); // Get the members associated with the class 'MyClass1'. MemberInfo[] myMembers = myType.GetMembers(); // Display the attributes for each of the members of the class 'MyClass1'. for(int i = 0; i < myMembers.Length; i++) { // Display the attribute if it is of type 'MyAttribute'. if(myMembers[i].IsDefined(typeof(MyAttribute), false)) { Object[] myAttributes = myMembers[i].GetCustomAttributes(typeof(MyAttribute), false); Console.WriteLine("\nThe attributes of type 'MyAttribute' for the member {0} are : \n", myMembers[i]); for(int j = 0; j < myAttributes.Length; j++) // Display the value associated with the attribute. Console.WriteLine("The value of the attribute is : \"{0}\"", ((MyAttribute)myAttributes[j]).Name); } } } catch(Exception e) { Console.WriteLine("Exception Caught! "+e.Message); } } }
protected object MemberwiseClone(); |
public virtual string ToString(); |