[Serializable] |
Several methods in this class assume that the getter and setter methods of a property have certain formats. The signatures of the get and set methods must match the following convention:
If this format is not followed, the behavior of the GetValue and SetValue methods is undefined.
Calling ICustomAttributeProvider.GetCustomAttributes on PropertyInfo when the inherit parameter of GetCustomAttributes is true does not walk the type hierarchy. Use Attribute to inherit custom attributes.
Attributes | Read-only Gets the attributes for this property. |
CanRead | Read-only Gets a value indicating whether the property can be read. |
CanWrite | Read-only Gets a value indicating whether the property can be written to. |
DeclaringType (inherited from System.Reflection.MemberInfo) |
Read-only See base class member description: System.Reflection.MemberInfo.DeclaringType Gets the class that declares this member. |
IsSpecialName | Read-only Gets a value indicating whether the property is the special name. |
MemberType | Read-only Overridden: Gets the Type of property reflected by this PropertyInfo object. |
Name (inherited from System.Reflection.MemberInfo) |
Read-only See base class member description: System.Reflection.MemberInfo.Name Gets the name of this member. |
PropertyType | Read-only Gets the type of the field of this property. |
ReflectedType (inherited from System.Reflection.MemberInfo) |
Read-only See base class member description: System.Reflection.MemberInfo.ReflectedType 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. |
GetAccessors | Overloaded:GetAccessors() Returns an array of the public get and set accessors on this property, and any method associated with this property. |
GetAccessors | Overloaded:GetAccessors(bool nonPublic) Returns an array of the public and/or non-public get and set accessors on this property. |
GetCustomAttributes (inherited from System.Reflection.MemberInfo) |
Overloaded:GetCustomAttributes(bool inherit) See base class member description: System.Reflection.MemberInfo.GetCustomAttributesWhen overridden in a derived class, returns an array of all of the custom attributes. |
GetCustomAttributes (inherited from System.Reflection.MemberInfo) |
Overloaded:GetCustomAttributes(Type attributeType, bool inherit) See base class member description: System.Reflection.MemberInfo.GetCustomAttributesWhen overridden in a derived class, returns an array of custom attributes identified by Type. |
GetGetMethod | Overloaded:GetGetMethod() Returns the public get accessor for this property. |
GetGetMethod | Overloaded:GetGetMethod(bool nonPublic) When overridden in a derived class, returns the public or non-public get accessor for this property. |
GetHashCode (inherited from System.Object) |
See base class member description: System.Object.GetHashCode Derived from System.Object, the primary base class for all objects. |
GetIndexParameters | When overridden in a derived class, returns an array of all the index parameters for the property. |
GetSetMethod | Overloaded:GetSetMethod() Returns the public set accessor for this property. |
GetSetMethod | Overloaded:GetSetMethod(bool nonPublic) When overridden in a derived class, returns the set accessor for this property. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
GetValue | Overloaded:GetValue(object obj, object[] index) Returns the value of the property with optional index values for indexed properties. |
GetValue | Overloaded:GetValue(object obj, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture) When overridden in a derived class, returns the value of a property having the specified binding, index, and CultureInfo. |
IsDefined (inherited from System.Reflection.MemberInfo) |
See base class member description: System.Reflection.MemberInfo.IsDefined When overridden in a derived class, indicates whether one or more instance of attributeType is defined on this member. |
SetValue | Overloaded:SetValue(object obj, object value, object[] index) Sets the value of the property with optional index values for index properties. |
SetValue | Overloaded:SetValue(object obj, object value, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture) When overridden in a derived class, sets the property value for the given object to the given value. |
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 PropertyInfo 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 PropertyInfo(); |
public abstract PropertyAttributes Attributes {get;}
|
To get the Attributes property, first get the class type. From the type, get the PropertyInfo. From the PropertyInfo, get the attributes.
//Make a property, then display the PropertyInfo using System; using System.Reflection; public class Myproperty { private string caption = "Default caption"; public string Caption{ get{return caption;} set {if(caption!=value) {caption = value;} } } } class Mypropertyinfo { public static int Main(string[] args) { Console.WriteLine("\nReflection.PropertyInfo"); //Build a property Myproperty Myproperty = new Myproperty(); Console.Write("\nMyproperty.Caption = " + Myproperty.Caption); //Get the type and PropertyInfo Type MyType = Type.GetType("Myproperty"); PropertyInfo Mypropertyinfo = MyType.GetProperty("Caption"); //Get and display the attributes property PropertyAttributes Myattributes = Mypropertyinfo.Attributes; Console.Write("\nPropertyAttributes - " + Myattributes.ToString()); return 0; } } /* Produces the following output Reflection.PropertyInfo Myproperty.Caption = Default caption PropertyAttributes - None */
public abstract bool CanRead {get;}
|
To get the CanRead property, first get the class Type. From the Type, get the PropertyInfo. From the PropertyInfo, get the CanRead value.
using System; using System.Reflection; //Make two properties, one readable and on not readable public class Mypropertya { private string caption = "A Default caption"; public string Caption{ get{return caption;} set {if(caption!=value) {caption = value;} } } } public class Mypropertyb { private string caption = "B Default caption"; public string Caption{ set{if(caption!=value) {caption = value;} } } } class Mypropertyinfo { public static int Main() { Console.WriteLine("\nReflection.PropertyInfo"); //Build two properties Mypropertya Mypropertya = new Mypropertya(); Mypropertyb Mypropertyb = new Mypropertyb(); Console.Write("\nMypropertya.Caption = " + Mypropertya.Caption); //Note: Mypropertyb.Caption cannot be read as // there is no get accessor //Get the type and PropertyInfo Type MyTypea = Type.GetType("Mypropertya"); PropertyInfo Mypropertyinfoa = MyTypea.GetProperty("Caption"); Type MyTypeb = Type.GetType("Mypropertyb"); PropertyInfo Mypropertyinfob = MyTypeb.GetProperty("Caption"); //Get and display the CanRead property Console.Write("\nCanRead a - " + Mypropertyinfoa.CanRead); Console.Write("\nCanRead b - " + Mypropertyinfob.CanRead); return 0; } } /* Produces the following output Reflection.PropertyInfo Mypropertya.Caption = A Default caption CanRead a - True CanRead b - False */
public abstract bool CanWrite {get;}
|
To get the CanWrite property, first get the class Type. From the Type, get the PropertyInfo. From the PropertyInfo, get the CanWrite value.
using System; using System.Reflection; //Make two properties, one writable and one not writable public class Mypropertya { private string caption = "A Default caption"; public string Caption{ get{return caption;} set {if(caption!=value) {caption = value;} } } } public class Mypropertyb { private string caption = "B Default caption"; public string Caption{ get{return caption;} } } class Mypropertyinfo { public static int Main() { Console.WriteLine("\nReflection.PropertyInfo"); //Build two properties Mypropertya Mypropertya = new Mypropertya(); Mypropertyb Mypropertyb = new Mypropertyb(); //Read and display the property Console.Write("\nMypropertya.Caption = " + Mypropertya.Caption); Console.Write("\nMypropertyb.Caption = " + Mypropertyb.Caption); //Write to the property Mypropertya.Caption = "A- I have been changed"; //Note: Mypropertyb.Caption cannot be written as // there is no set accessor //Read and display the property Console.Write("\nMypropertya.Caption = " + Mypropertya.Caption); Console.Write ("\nMypropertyb.Caption = " + Mypropertyb.Caption); //Get the type and PropertyInfo Type MyTypea = Type.GetType("Mypropertya"); PropertyInfo Mypropertyinfoa = MyTypea.GetProperty("Caption"); Type MyTypeb = Type.GetType("Mypropertyb"); PropertyInfo Mypropertyinfob = MyTypeb.GetProperty("Caption"); //Get and display the CanWrite property Console.Write("\nCanWrite a - " + Mypropertyinfoa.CanWrite); Console.Write("\nCanWrite b - " + Mypropertyinfob.CanWrite); return 0; } } /* This code produces the following output: Reflection.PropertyInfo Mypropertya.Caption = A Default caption Mypropertyb.Caption = B Default caption Mypropertya.Caption = A- I have been changed Mypropertyb.Caption = B Default caption CanWrite a - True CanWrite b - False */
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 bool IsSpecialName {get;}
|
To get the IsSpecialName property, first get the class Type. From the Type, get the PropertyInfo. From the PropertyInfo, get the IsSpecialName value.
public override MemberTypes MemberType {get;}
|
To get the MemberType property, first get the class Type. From the Type, get the PropertyInfo. From the PropertyInfo, get the MemberType value.
using System; using System.Reflection; class Mypropertyinfo { public static int Main() { Console.WriteLine("\nReflection.PropertyInfo"); //Get the type and PropertyInfo Type MyType = Type.GetType("System.Reflection.MemberInfo"); PropertyInfo Mypropertyinfo = MyType.GetProperty("Name"); //Read and display the MemberType property Console.Write("\nMemberType = " + Mypropertyinfo.MemberType.ToString()); return 0; } } /* Produces the following output Reflection.PropertyInfo MemberType = 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 PropertyType {get;}
|
To get the PropertyType property, first get the class Type. From the Type, get the PropertyInfo. From the PropertyInfo, get the PropertyType value.
using System; using System.Reflection; class Mypropertyinfo { public static int Main() { Console.WriteLine("\nReflection.PropertyInfo"); //Get the type and PropertyInfo Type MyTypea = Type.GetType("System.Reflection.MemberInfo"); PropertyInfo Mypropertyinfoa = MyTypea.GetProperty("Name"); Type MyTypeb = Type.GetType("System.Reflection.MethodBase"); PropertyInfo Mypropertyinfob = MyTypeb.GetProperty("IsFinal"); //Read and display the PropertyType property Console.Write ("\n" + MyTypea.FullName + "." + Mypropertyinfoa.Name + " has a PropertyType of " + Mypropertyinfoa.PropertyType); Console.Write("\n" + MyTypeb.FullName + "." + Mypropertyinfob.Name + " has a PropertyType of " + Mypropertyinfob.PropertyType); return 0; } } /* Produces the following output Reflection.PropertyInfo System.Reflection.MemberInfo.Name has a PropertyType of System.String System.Reflection.MethodBase.IsFinal has a PropertyType of Boolean */
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
~PropertyInfo(); |
public MethodInfo[] GetAccessors(); |
public abstract MethodInfo[] GetAccessors( |
nonPublic
using System; using System.Reflection; //Make a property public class Myproperty { private string caption = "A Default caption"; public string Caption{ get{return caption;} set {if(caption!=value) {caption = value;} } } } class Mypropertyinfo { public static int Main() { Console.WriteLine ("\nReflection.PropertyInfo"); //Get the type and PropertyInfo Type MyType = Type.GetType("Myproperty"); PropertyInfo Mypropertyinfo = MyType.GetProperty("Caption"); //Get the public GetAccessors Method MethodInfo[] Mymethodinfoarray = Mypropertyinfo.GetAccessors(true); Console.Write ("\nThere are " + Mymethodinfoarray.Length + "accessors (public)"); return 0; } } /* Produces the following output Reflection.PropertyInfo There are 2 accessors (public) */
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 MethodInfo GetGetMethod(); |
To use the GetGetMethod method, first get the class Type. From the Type, get the PropertyInfo. From the PropertyInfo, use the GetGetMethod method.
public abstract MethodInfo GetGetMethod( |
nonPublic
Exception Type | Condition |
---|---|
SecurityException | The requested method is not public and the caller does not have ReflectionPermission to reflect on methods that are not public. |
To use the GetGetMethod method, first get the class Type. From the Type, get the PropertyInfo. From the PropertyInfo, use the GetGetMethod method.
using System; using System.Reflection; //Make a property public class Myproperty { private string caption = "A Default caption"; public string Caption{ get{return caption;} set {if(caption!=value) {caption = value;} } } } class Mypropertyinfo { public static int Main() { Console.WriteLine ("\nReflection.PropertyInfo"); //Get the type and PropertyInfo for two separate properties Type MyTypea = Type.GetType("Myproperty"); PropertyInfo Mypropertyinfoa = MyTypea.GetProperty("Caption"); Type MyTypeb = Type.GetType("System.Reflection.MethodInfo"); PropertyInfo Mypropertyinfob = MyTypeb.GetProperty("MemberType"); //Get and display the GetGetMethod Method for each property MethodInfo Mygetmethodinfoa = Mypropertyinfoa.GetGetMethod(); Console.Write ("\nGetAccessor for " + Mypropertyinfoa.Name + " returns a " + Mygetmethodinfoa.ReturnType); MethodInfo Mygetmethodinfob = Mypropertyinfob.GetGetMethod(); Console.Write ("\nGetAccessor for " + Mypropertyinfob.Name + " returns a " + Mygetmethodinfob.ReturnType); //Display the GetGetMethod without using the MethodInfo Console.Write ("\n" + MyTypea.FullName + "." + Mypropertyinfoa.Name + " GetGetMethod - " + Mypropertyinfoa.GetGetMethod()); Console.Write ("\n" + MyTypeb.FullName + "." + Mypropertyinfob.Name + " GetGetMethod - " + Mypropertyinfob.GetGetMethod()); return 0; } } /* Produces the following output Reflection.PropertyInfo GetAccessor for Caption returns a System.String GetAccessor for MemberType returns a System.Reflection.MemberTypes Myproperty.Caption GetGetMethod - System.String get_Caption() System.Reflection.MethodInfo.MemberType GetGetMethod - System.Reflection.MemberTypes get_MemberType() */
public virtual int GetHashCode(); |
public abstract ParameterInfo[] GetIndexParameters(); |
Exception Type | Condition |
---|---|
SecurityException | The property itself is accessible, but the get or set accessor is not. |
To use the GetIndexParameters method, first get the class Type. From the Type, get the PropertyInfo. From the PropertyInfo, use the GetIndexParameters method.
using System; using System.Reflection; //Make a property public class Myproperty { private string caption = "A Default caption"; public string Caption{ get{return caption;} set {if(caption!=value) {caption = value;} } } } class Mypropertyinfo { public static int Main() { Console.WriteLine ("\nReflection.PropertyInfo"); //Get the type and PropertyInfo Type MyType = Type.GetType("Myproperty"); PropertyInfo Mypropertyinfo = MyType.GetProperty("Caption"); //Get the public GetIndexParameters Method ParameterInfo[] Myparameterinfoarray = Mypropertyinfo.GetIndexParameters(); Console.Write ("\n" + MyType.FullName + "." + Mypropertyinfo.Name + " has " + Myparameterinfoarray.GetLength(0) + " parameters"); return 0; } } /* Produces the following output Reflection.PropertyInfo Myproperty.Caption has 0 parameters */
public MethodInfo GetSetMethod(); |
To use the GetSetMethod method, first get the class Type. From the Type, get the PropertyInfo. From the PropertyInfo, use the GetSetMethod method.
public abstract MethodInfo GetSetMethod( |
nonPublic
Value | Condition |
---|---|
A MethodInfo object representing the Set method for this property. | The set accessor is public.nonPublic is true and non-public methods can be returned. |
null | nonPublic is true, but the property is read-only. -or- nonPublic is false and the set accessor is non-public. -or- There is no set accessor. |
Exception Type | Condition |
---|---|
SecurityException | The requested method is not public and the caller does not have ReflectionPermission to reflect on methods that are not public. |
using System; using System.Reflection; //Make a property public class Myproperty { private string caption = "A Default caption"; public string Caption{ get{return caption;} set {if(caption!=value) {caption = value;} } } } class Mypropertyinfo { public static int Main() { Console.WriteLine ("\nReflection.PropertyInfo"); //Get the type and PropertyInfo for two separate properties Type MyTypea = Type.GetType("Myproperty"); PropertyInfo Mypropertyinfoa = MyTypea.GetProperty("Caption"); Type MyTypeb = Type.GetType("System.Text.StringBuilder"); PropertyInfo Mypropertyinfob = MyTypeb.GetProperty("Length"); //Get and display the GetSetMethod Method for each property MethodInfo Mygetmethodinfoa = Mypropertyinfoa.GetSetMethod(); Console.Write ("\nSetAccessor for " + Mypropertyinfoa.Name + " returns a " + Mygetmethodinfoa.ReturnType); MethodInfo Mygetmethodinfob = Mypropertyinfob.GetSetMethod(); Console.Write ("\nSetAccessor for " + Mypropertyinfob.Name + " returns a " + Mygetmethodinfob.ReturnType); //Display the GetSetMethod without using the MethodInfo Console.Write ("\n\n" + MyTypea.FullName + "." + Mypropertyinfoa.Name + " GetSetMethod - " + Mypropertyinfoa.GetSetMethod()); Console.Write ("\n" + MyTypeb.FullName + "." + Mypropertyinfob.Name + " GetSetMethod - " + Mypropertyinfob.GetSetMethod()); return 0; } } /* Produces the following output Reflection.PropertyInfo SetAccessor for Caption returns a System.Void SetAccessor for Length returns a System.Void Myproperty.Caption GetSetMethod - Void set_Caption(System.String) System.Text.StringBuilder.Length GetSetMethod - Void set_Length(Int32) */
public Type GetType(); |
obj
index
Exception Type | Condition |
---|---|
ArgumentException | The index array does not contain the type of arguments needed. -or- The property's Get method is not found. |
TargetException | The object does not match the target type, or a property is an instance property but obj is null. |
TargetParameterCountException | The number of parameters in index does not match the number of parameters the indexed property takes. |
MethodAccessException | There was an illegal attempt to access a private or protected method inside a class. |
Because static properties belong to the type, not individual objects, get static properties by passing null as the object argument. For example, use the following code to get the static CurrentCulture property of CultureInfo:
PropertyInfo CurCultProp =
(typeof(CultureInfo)).GetProperty("CurrentCulture");
Console.WriteLine("CurrCult: " +
CurCultProp.GetValue(null,null));
To use the GetValue method, first get the class Type. From the Type, get the PropertyInfo. From the PropertyInfo, use the GetValue method.
public abstract object GetValue( |
obj
invokeAttr
binder
index
culture
Exception Type | Condition |
---|---|
ArgumentException | The index array does not contain the type of arguments needed. -or- The property's Get method is not found. |
TargetException | The object does not match the target type, or a property is an instance property but obj is null. |
TargetParameterCountException | The number of parameters in index does not match the number of parameters the indexed property takes. |
MethodAccessException | There was an illegal attempt to access a private or protected method inside a class. |
PropertyInfo CurCultProp =
(typeof(CultureInfo)).GetProperty("CurrentCulture");
Console.WriteLine("CurrCult: " +
CurCultProp.GetValue(null,null));
To use the GetValue method, first get the class Type. From the Type, get the PropertyInfo. From the PropertyInfo, use the GetValue method.
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(); |
obj
value
index
Exception Type | Condition |
---|---|
ArgumentException | The index array does not contain the type of arguments needed. -or- The property's Get method is not found. |
TargetException | The object does not match the target type, or a property is an instance property but obj is null. |
TargetParameterCountException | The number of parameters in index does not match the number of parameters the indexed property takes. |
MethodAccessException | There was an illegal attempt to access a private or protected method inside a class. |
To use the SetValue method, first get the class Type. From the Type, get the PropertyInfo. From the PropertyInfo, use the SetValue method.
public abstract void SetValue( |
obj
value
invokeAttr
binder
index
culture
Exception Type | Condition |
---|---|
ArgumentException | The index array does not contain the type of arguments needed. -or- The property's Get method is not found. |
TargetException | The object does not match the target type, or a property is an instance property but obj is null. |
TargetParameterCountException | The number of parameters in index does not match the number of parameters the indexed property takes. |
MethodAccessException | There was an illegal attempt to access a private or protected method inside a class. |
To use the SetValue method, first get the class Type. From the Type, get the PropertyInfo. From the PropertyInfo, use the SetValue method.
using System; using System.Reflection; //Make a property public class Myproperty { private string caption = "A Default caption"; public string Caption{ get{return caption;} set {if(caption!=value) {caption = value;} } } } class Mypropertyinfo { public static int Main() { Console.WriteLine ("\nReflection.PropertyInfo"); Myproperty Myproperty = new Myproperty(); //Get the type and PropertyInfo Type MyType = Type.GetType("Myproperty"); PropertyInfo Mypropertyinfo = MyType.GetProperty("Caption"); //Get and display the GetValue Method Console.Write ("\nGetValue - " + Mypropertyinfo.GetValue (Myproperty, null)); //Use the SetValue Method to change the caption Mypropertyinfo.SetValue( Myproperty, "This caption has been changed", null); //Get the caption again and display it Console.Write ("\nGetValue - " + Mypropertyinfo.GetValue (Myproperty, null)); return 0; } } /* Produces the following output Reflection.PropertyInfo GetValue - A Default caption GetValue - This caption has been changed */
public virtual string ToString(); |