public sealed class FieldBuilder : FieldInfo
|
Attributes | Read-only Overridden: Indicates the attributes of this field. This property is read-only. |
DeclaringType | Read-only Overridden: Indicates a reference to the Type object for the type that declares this field. This property is read-only. |
FieldHandle | Read-only Overridden: Indicates the internal metadata handle for this field. This property is read-only. |
FieldType | Read-only Overridden: Indicates the Type object that represents the type of this field. This property is read-only. |
IsAssembly (inherited from System.Reflection.FieldInfo) |
Read-only See base class member description: System.Reflection.FieldInfo.IsAssembly Gets a value indicating whether this field has Assembly level visibility. |
IsFamily (inherited from System.Reflection.FieldInfo) |
Read-only See base class member description: System.Reflection.FieldInfo.IsFamily Gets a value indicating whether this field has Family level visibility. |
IsFamilyAndAssembly (inherited from System.Reflection.FieldInfo) |
Read-only See base class member description: System.Reflection.FieldInfo.IsFamilyAndAssembly Gets a value indicating whether this field has FamilyAndAssembly level visibility. |
IsFamilyOrAssembly (inherited from System.Reflection.FieldInfo) |
Read-only See base class member description: System.Reflection.FieldInfo.IsFamilyOrAssembly Gets a value indicating whether this field has FamilyOrAssembly level visibility. |
IsInitOnly (inherited from System.Reflection.FieldInfo) |
Read-only See base class member description: System.Reflection.FieldInfo.IsInitOnly Gets a value indicating whether the field can only be set in the body of the constructor. |
IsLiteral (inherited from System.Reflection.FieldInfo) |
Read-only See base class member description: System.Reflection.FieldInfo.IsLiteral Gets a value indicating whether the value is written at compile time and cannot be changed. |
IsNotSerialized (inherited from System.Reflection.FieldInfo) |
Read-only See base class member description: System.Reflection.FieldInfo.IsNotSerialized Gets a value indicating whether this field has the NotSerialized attribute. |
IsPinvokeImpl (inherited from System.Reflection.FieldInfo) |
Read-only See base class member description: System.Reflection.FieldInfo.IsPinvokeImpl Gets a value indicating whether the corresponding PinvokeImpl attribute is set in FieldAttributes. |
IsPrivate (inherited from System.Reflection.FieldInfo) |
Read-only See base class member description: System.Reflection.FieldInfo.IsPrivate Gets a value indicating whether the field is private. |
IsPublic (inherited from System.Reflection.FieldInfo) |
Read-only See base class member description: System.Reflection.FieldInfo.IsPublic Gets a value indicating whether the field is public. |
IsSpecialName (inherited from System.Reflection.FieldInfo) |
Read-only See base class member description: System.Reflection.FieldInfo.IsSpecialName Gets a value indicating whether the corresponding SpecialName attribute is set in the FieldAttributes enumerator. |
IsStatic (inherited from System.Reflection.FieldInfo) |
Read-only See base class member description: System.Reflection.FieldInfo.IsStatic Gets a value indicating whether the field is static. |
MemberType (inherited from System.Reflection.FieldInfo) |
Read-only See base class member description: System.Reflection.FieldInfo.MemberType Gets the Type of property reflected by this FieldInfo object. The retrieved value indicates that this member is a field. |
Name | Read-only Overridden: Indicates the name of this field. This property is read-only. |
ReflectedType | Read-only Overridden: Indicates the reference to the Type object from which this object was obtained. This property is read-only. |
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) Overridden: Returns all the custom attributes defined for this field. |
GetCustomAttributes | Overloaded:GetCustomAttributes(Type attributeType, bool inherit) Overridden: Returns all the custom attributes defined for this field identified by the given 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. |
GetToken | Returns the token representing this field. |
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 | Overridden: Retrieves the value of the field supported by the given object. |
GetValueDirect (inherited from System.Reflection.FieldInfo) |
See base class member description: System.Reflection.FieldInfo.GetValueDirect Returns the value of a field supported by a given object. |
IsDefined | Overridden: Indicates whether an attribute having the specified type is defined on a field. |
SetConstant | Sets the default value of this field. |
SetCustomAttribute | Overloaded:SetCustomAttribute(CustomAttributeBuilder customBuilder) Sets a custom attribute using a custom attribute builder. |
SetCustomAttribute | Overloaded:SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) Sets a custom attribute using a specified custom attribute blob. |
SetMarshal | Describes the native marshaling of the field. |
SetOffset | Specifies the field layout. |
SetValue (inherited from System.Reflection.FieldInfo) |
Overloaded:SetValue(object obj, object value) See base class member description: System.Reflection.FieldInfo.SetValueSets the value of the field supported by the given object. |
SetValue | Overloaded:SetValue(object obj, object val, BindingFlags invokeAttr, Binder binder, CultureInfo culture) Overridden: Sets the value of the field supported by the given object. |
SetValueDirect (inherited from System.Reflection.FieldInfo) |
See base class member description: System.Reflection.FieldInfo.SetValueDirect Sets the value of the field supported by the given object. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
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:
public override FieldAttributes Attributes {get;}
|
public override Type DeclaringType {get;}
|
public override RuntimeFieldHandle FieldHandle {get;}
|
Exception Type | Condition |
---|---|
NotSupportedException | This method is not supported. |
public override Type FieldType {get;}
|
public bool IsAssembly {get;}
|
The IsAssembly property is set when the FieldAttributes.Assembly attribute is set. In C#, you can declare the field as internal to set this property to limit the access of this field to this project.
//Make two fields. public class Myfielda { private string field = "A private field"; public string Field{ get{return field;} set{if(field!=value){field=value;}} } } public class Myfieldb { internal string field = "B public field"; public string Field{ get{return field;} set{if(field!=value){field=value;}} } } public class Myfieldinfo { public static int Main() { Console.WriteLine("\nReflection.FieldInfo"); Myfielda Myfielda = new Myfielda(); Myfieldb Myfieldb = new Myfieldb(); //Get the Type and FieldInfo. Type MyTypea = Type.GetType("Myfielda"); FieldInfo Myfieldinfoa = MyTypea.GetField("field", BindingFlags.NonPublic|BindingFlags.Instance); Type MyTypeb = Type.GetType("Myfieldb"); FieldInfo Myfieldinfob = MyTypeb.GetField("field", BindingFlags.NonPublic|BindingFlags.Instance); //For the first field, get and display the name, field, and IsAssembly. Console.Write("\n{0} - ", MyTypea.FullName); Console.Write("{0};", Myfieldinfoa.GetValue(Myfielda)); Console.Write(" IsAssembly = {0}; ", Myfieldinfoa.IsAssembly ); if (Myfieldinfoa.IsAssembly) Console.Write("Field has limited accessibility"); //For the second field, get and display the name, field, and IsAssembly. Console.Write("\n{0} - ", MyTypeb.FullName); Console.Write("{0}; ", Myfieldinfob.GetValue(Myfieldb)); Console.Write(" IsAssembly = {0}; ", Myfieldinfob.IsAssembly ); if (Myfieldinfob.IsAssembly) Console.Write("Field has limited accessibility"); return 0; } }This code produces the following output:
Reflection.FieldInfo Myfielda - A private field; IsAssembly = False; Myfieldb - B public field; IsAssembly = True; Field has limited accessibility
public bool IsFamily {get;}
|
To get the IsFamily property, first get the class Type. From the Type, get the FieldInfo. From the FieldInfo, get the IsFamily value.
The IsFamily property is set when the FieldAttributes.Family attribute is set.
//Make two fields. using System; using System.Reflection; public class Myfielda { private string field = "A private field"; public string Field{ get{return field;} set{if(field!=value){field=value;}} } } public class Myfieldb { protected string field = "B public field"; public string Field{ get{return field;} set{if(field!=value){field=value;}} } } public class Myfieldinfo { public static int Main() { Console.WriteLine("\nReflection.FieldInfo"); Myfielda Myfielda = new Myfielda(); Myfieldb Myfieldb = new Myfieldb(); //Get the Type and FieldInfo. Type MyTypea = Type.GetType("Myfielda"); FieldInfo Myfieldinfoa = MyTypea.GetField("field", BindingFlags.NonPublic|BindingFlags.Instance); Type MyTypeb = Type.GetType("Myfieldb"); FieldInfo Myfieldinfob = MyTypeb.GetField("field", BindingFlags.NonPublic|BindingFlags.Instance); //For the first field, get and display the Name, field, and IsFamily. Console.Write("\n{0} - ", MyTypea.FullName); Console.Write("{0} - ", Myfieldinfoa.GetValue(Myfielda)); Console.Write("\n IsFamily - {0}", Myfieldinfoa.IsFamily); Console.Write("\n FieldAttributes - {0}", Myfieldinfoa.Attributes.ToString()); //For the second field, get and Display the Name, field, and IsFamily. Console.Write("\n{0} - ", MyTypeb.FullName); Console.Write("{0} - ", Myfieldinfob.GetValue(Myfieldb)); Console.Write("\n IsFamily - {0}", Myfieldinfob.IsFamily); FieldAttributes Myfieldattributesb = Myfieldinfob.Attributes; Console.Write("\n FieldAttributes - {0}", Myfieldinfob.Attributes.ToString()); return 0; } }
This code produces the following output:
Reflection.FieldInfo Myfielda - A private field - IsFamily - False FieldAttributes - Private Myfieldb - B public field - IsFamily - True FieldAttributes - Family
public bool IsFamilyAndAssembly {get;}
|
The IsFamilyAndAssembly property is set when the FieldAttributes.FamANDAssem attribute is set.
using System; using System.Reflection; public class Fieldinfo_IsFamilyAndAssembly { protected internal string myField = "A protected internal field"; public static void Main() { Fieldinfo_IsFamilyAndAssembly myObject = new Fieldinfo_IsFamilyAndAssembly(); //Get the Type and FieldInfo. Type myType1 = typeof(Fieldinfo_IsFamilyAndAssembly); FieldInfo myFieldInfo = myType1.GetField("myField", BindingFlags.NonPublic|BindingFlags.Instance); // Display the name, field and the FamilyAndAssembly attribute for the field. Console.Write("\n Name of Class: {0}", myType1.FullName); Console.Write("\n Value of Field: {0}", myFieldInfo.GetValue(myObject)); Console.Write("\n IsFamilyAndAssembly: {0}", myFieldInfo.IsFamilyAndAssembly ); } }
public bool IsFamilyOrAssembly {get;}
|
using System; using System.Reflection; //Make two fields. public class Myfielda //Note that if the private line below is uncommented //and the protected internal line below is commented out, //this will not compile as Myfielda.field is inaccessible. { //private string field = "A private field"; protected internal string field = "A private field"; public string Field{ get{return field;} set{if(field!=value) {field=value;}} } } //Myfieldb is derived from Myfielda. //The protected internal string field allows //the IsFamilyOrAssembly flag to be set and allows //the field to be visible from a derived class. public class Myfieldb:Myfielda { new public string Field{ get{return field;} set{if(field!=value){field=value;}} } } public class Myfieldinfo { public static int Main() { Console.WriteLine("\nReflection.FieldInfo"); Myfielda Myfielda = new Myfielda(); Myfieldb Myfieldb = new Myfieldb(); //Get the Type and FieldInfo. Type MyTypea = Type.GetType("Myfielda"); FieldInfo Myfieldinfoa = MyTypea.GetField("field", BindingFlags.NonPublic|BindingFlags.Instance); Type MyTypeb = Type.GetType("Myfieldb"); FieldInfo Myfieldinfob = MyTypeb.GetField("field", BindingFlags.NonPublic|BindingFlags.Instance); //For the first field, get and display the Name, field, and //IsFamilyOrAssembly. Console.Write("\n{0} - ", MyTypea.FullName); Console.Write("{0};", Myfieldinfoa.GetValue(Myfielda)); Console.Write("\n IsFamilyOrAssembly = {0};", Myfieldinfoa.IsFamilyOrAssembly); if (Myfieldinfoa.IsFamilyOrAssembly ) Console.Write("Field has limited accessibility"); //For the second field, get and display the name and field. Console.Write("\n{0} - ", MyTypeb.FullName); Console.Write("{0}; ", Myfieldinfob.GetValue(Myfieldb)); return 0; } }
This code produces the following output:
Reflection.FieldInfo Myfielda - A private field; IsFamilyOrAssembly = True; Field has limited accessibility Myfieldb - A private field;
public bool IsInitOnly {get;}
|
To get the IsInitOnly property, first get the class Type. From the Type, get the FieldInfo. From the FieldInfo, get the IsInitOnly property. To access a non-public field, set the BindingFlags to NonPublic and choose either Static or InstanceBindingFlags in the GetField method.
The IsInitOnly property is set when the FieldAttributes.InitOnly attribute is set.
using System; using System.Reflection; //Make two fields, one public and one read-only. public class Myfielda { public string field = "A - public field"; public string Field{ get{return field;} set{if(field!=value){field=value;}} } } public class Myfieldb { readonly string field = "B - readonly field"; public string Field{ get{return field;} } } public class Myfieldinfo { public static int Main() { Console.WriteLine("\nReflection.FieldInfo"); Myfielda Myfielda = new Myfielda(); Myfieldb Myfieldb = new Myfieldb(); //Get the Type and FieldInfo. Type MyTypea = Type.GetType("Myfielda"); FieldInfo Myfieldinfoa = MyTypea.GetField("field", BindingFlags.Public|BindingFlags.Instance); Type MyTypeb = Type.GetType("Myfieldb"); FieldInfo Myfieldinfob = MyTypeb.GetField("field", BindingFlags.NonPublic|BindingFlags.Instance); //Modify the fields. //Note that Myfieldb is not modified, as it is //read-only (IsInitOnly is True). Myfielda.field = "A- modified"; //Myfieldb.field = "B- modified"; //For the first field, get and display the name, field, and IsInitOnly. Console.Write("\n{0} - {1}, IsInitOnly = {2} ", MyTypea.FullName, Myfieldinfoa.GetValue(Myfielda), Myfieldinfoa.IsInitOnly); //For the second field get and display the name, field, and IsInitOnly. Console.Write("\n{0} - {1}, IsInitOnly = {2} ", MyTypeb.FullName, Myfieldinfob.GetValue(Myfieldb), Myfieldinfob.IsInitOnly); return 0; } }
This code produces the following output:
Reflection.FieldInfo Myfielda - A- modified, IsInitOnly = False Myfieldb - B readonly field, IsInitOnly = True
public bool IsLiteral {get;}
|
public bool IsNotSerialized {get;}
|
using System; using System.Reflection; using System.Runtime.Serialization; public class MyClass { public short myShort; // The following field will not be serialized. [NonSerialized()] public int myInt; } public class Type_IsNotSerializable { public static void Main() { // Get the type of 'MyClass'. Type myType = typeof(MyClass); // Get the fields of 'MyClass'. FieldInfo[] myFields = myType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static); Console.WriteLine("\nDisplaying if field is serializable\n"); // Display whether the field is serializable or not. for(int i = 0; i < myFields.Length; i++) if(myFields[i].IsNotSerialized) Console.WriteLine("'{0}' field is not serializable", myFields[i]); else Console.WriteLine("'{0}' field is serializable", myFields[i]); } }
public bool IsPinvokeImpl {get;}
|
using System; using System.Reflection; public class Fieldinfo_IsPinvoke { public string myField = "A public field"; public static void Main() { Fieldinfo_IsPinvoke myObject = new Fieldinfo_IsPinvoke(); //Get the Type and FieldInfo. Type myType1 = typeof(Fieldinfo_IsPinvoke); FieldInfo myFieldInfo = myType1.GetField("myField", BindingFlags.Public|BindingFlags.Instance); // Display the name, field and the PInvokeImpl attribute for the field. Console.Write("\n Name of Class: {0} ", myType1.FullName); Console.Write("\n Value of Field: {0}", myFieldInfo.GetValue(myObject)); Console.Write("\n IsPinvokeImpl: {0} ", myFieldInfo.IsPinvokeImpl ); } }
public bool IsPrivate {get;}
|
The IsPrivate property is set when the FieldAttributes.Private attribute is set.
To get the IsPrivate property, first get the class Type. From the Type, get the FieldInfo. From the FieldInfo, get the IsPrivate property. To access a non-public field, set the BindingFlags to NonPublic, and either Static or Instance in the GetField method.
using System; using System.Reflection; class MyClass { private string myField; public string[] myArray = new string[] {"New York", "New Jersey"}; MyClass() { myField = "Microsoft"; } string GetField { get { return myField; } } } class FieldInfo_IsPrivate { public static void Main() { try { // Gets the type of 'MyClass'. Type myType = typeof(MyClass); // Gets the field information of 'MyClass'. FieldInfo[] myFields = myType.GetFields(BindingFlags.NonPublic |BindingFlags.Public |BindingFlags.Instance); Console.WriteLine("\nDisplaying whether the fields of '{0}' are private or not:\n",myType); for(int i = 0; i < myFields.Length; i++) { // Check whether the field is private or not. if(myFields[i].IsPrivate) Console.WriteLine("'{0}' is a private field", myFields[i].Name); else Console.WriteLine("'{0}' is not a private field", myFields[i].Name); } } catch(Exception e) { Console.WriteLine("Exception : {0} " , e.Message); } } }
public bool IsPublic {get;}
|
The IsPublic property is set when the FieldAttributes.Public attribute is set.
To get the IsPublic property, first get the class Type. From the Type, get the FieldInfo. From the FieldInfo, get the IsPublic property. If the field is other than public, it is protected and cannot be readily accessed. To access a not public field, set the BindingFlags to NonPublic, specify either BindingFlags.Instance or BindingFlags.Static, and use this for the GetField method.
using System; using System.Reflection; //Make two fields. public class Myfielda // private { private string SomeField = "private field"; public string Field{ get{return SomeField;} } } public class Myfieldb // public { public string SomeField = "public field"; public string Field{ get{return SomeField;} } } public class Myfieldinfo { public static int Main() { Console.WriteLine("\nReflection.FieldInfo"); Myfielda Myfielda = new Myfielda(); Myfieldb Myfieldb = new Myfieldb(); //Get the Type and FieldInfo. Type MyTypea = Type.GetType("Myfielda"); FieldInfo Myfieldinfoa = MyTypea.GetField("SomeField", BindingFlags.NonPublic|BindingFlags.Instance); Type MyTypeb = Type.GetType("Myfieldb"); FieldInfo Myfieldinfob = MyTypeb.GetField("SomeField"); //Get and Display the IsPublic and IsPrivate. Console.Write("\n{0}.", MyTypea.FullName); Console.Write("{0} - ", Myfieldinfoa.Name); Console.Write("{0};", Myfielda.Field); Console.Write("\n IsPublic = {0}; ",Myfieldinfoa.IsPublic); Console.Write("\n IsPrivate = {0}; ",Myfieldinfoa.IsPrivate); Console.Write("\n{0}.", MyTypeb.FullName); Console.Write("{0} - ", Myfieldinfob.Name); Console.Write("{0};", Myfieldb.Field); Console.Write("\n IsPublic = {0}; ", Myfieldinfob.IsPublic); Console.Write("\n IsPrivate = {0}; ",Myfieldinfob.IsPrivate); return 0; } }This code produces the following output:
Reflection.FieldInfo
Myfielda.SomeField - private field;
IsPublic = False;
IsPrivate = True;
Myfieldb.SomeField - public field;
IsPublic = True;
IsPrivate = False;
public bool IsSpecialName {get;}
|
using System; using System.Reflection; using System.ComponentModel.Design; class FieldInfo_IsSpecialName { public static void Main() { try { // Get the type handle of the 'ViewTechnology' class. Type myType = typeof(ViewTechnology); // Get the fields of the class 'ViewTechnology'. FieldInfo[] myField = myType.GetFields(); Console.WriteLine("\nDisplaying fields that have 'SpecialName' attributes\n"); for(int i = 0; i < myField.Length; i++) { // Check whether each field is a special name or not. if(myField[i].IsSpecialName) { Console.WriteLine("The field '{0}' has SpecialName attribute", myField[i].Name); } } } catch(Exception e) { Console.WriteLine("Exception : {0} " , e.Message); } } }
public bool IsStatic {get;}
|
The IsStatic property is set when the FieldAttributes.Static attribute is set.
To get the IsStatic property, first get the class Type. From the Type, get the FieldInfo. From the FieldInfo, get the IsStatic property. To access a non-public field, set the BindingFlags to NonPublic in the GetField method and set the accessibility to Instance or Static.
using System; using System.Reflection; //Make two fields. public class Myfielda { private string field = "A private field"; public string Field{ get{return field;} set{if(field!=value){field=value;}} } } public class Myfieldb { static string field = "B static field"; public string Field { get{return field;} set{if(field!=value){field=value;}} } } public class Myfieldinfo { public static int Main() { Console.WriteLine("\nReflection.FieldInfo"); Myfielda Myfielda = new Myfielda(); Myfieldb Myfieldb = new Myfieldb(); //Get the Type and FieldInfo. Type MyTypea = Type.GetType("Myfielda"); FieldInfo Myfieldinfoa = MyTypea.GetField("field", BindingFlags.NonPublic|BindingFlags.Instance); Type MyTypeb = Type.GetType("Myfieldb"); FieldInfo Myfieldinfob = MyTypeb.GetField("field", BindingFlags.NonPublic|BindingFlags.Static); //For the first field, get and display the name, field, and IsStatic. Console.Write("\n{0} - ", MyTypea.FullName); Console.Write("{0}; ", Myfieldinfoa.GetValue(Myfielda)); Console.Write("IsStatic - {0}", Myfieldinfoa.IsStatic); //For the second field get and display the name, field, and IsStatic. Console.Write("\n{0} - ", MyTypeb.FullName); Console.Write("{0}; ", Myfieldinfob.GetValue(Myfieldb)); Console.Write("IsStatic - {0}", Myfieldinfob.IsStatic); return 0; } }This code produces the following output:
Reflection.FieldInfo
Myfielda - A private field; IsStatic - False
Myfieldb - B static field; IsStatic - True
public override MemberTypes MemberType {get;}
|
using System; using System.Reflection; //Make a field public class Myfield { private string field = "a private field"; public string Field{ get{return field;} } } public class Myfieldinfo { public static int Main() { Console.WriteLine ("\nReflection.FieldInfo"); Myfield Myfield = new Myfield(); //Get the Type and FieldInfo Type MyType = Type.GetType("Myfield"); FieldInfo Myfieldinfo = MyType.GetField("field", BindingFlags.NonPublic|BindingFlags.Instance); //Get and Display the MemberType Console.Write ("\n{0}.", MyType.FullName); Console.Write ("{0} - ", Myfieldinfo.Name); Console.Write ("{0};", Myfield.Field); MemberTypes Mymembertypes = Myfieldinfo.MemberType; Console.Write(" MemberType is a {0}", Mymembertypes.ToString()); return 0; } }This code produces the following output:
Reflection.FieldInfo
Myfield.field - a private field; MemberType is a Field
public override string Name {get;}
|
public override Type ReflectedType {get;}
|
~FieldBuilder(); |
inherit
Exception Type | Condition |
---|---|
NotSupportedException | This method is not supported. |
attributeType
inherit
Exception Type | Condition |
---|---|
NotSupportedException | This method is not supported. |
public virtual int GetHashCode(); |
public FieldToken GetToken(); |
public Type GetType(); |
obj
Exception Type | Condition |
---|---|
NotSupportedException | This method is not supported. |
The return type of FieldBuilder.GetValue is Object. For example, if the field holds a Boolean primitive value, an instance of Object with the appropriate Boolean value is returned. Before returning the value, FieldBuilder.GetValue checks to see if the user has access permission.
Access restrictions are ignored for fully-trusted code.Private constructors, methods, fields, and properties can be accessed and invoked using Reflection whenever the code is fully-trusted.
[CLSCompliant(false)] |
obj
Exception Type | Condition |
---|---|
NotSupportedException | The caller requires the CLS alternative, but called this method instead. |
attributeType
inherit
Exception Type | Condition |
---|---|
NotSupportedException | This method is not currently supported. Retrieve the field using Type.GetField and call MemberInfo.IsDefined on the returned FieldInfo. |
protected object MemberwiseClone(); |
public void SetConstant( |
defaultValue
Exception Type | Condition |
---|---|
InvalidOperationException | The containing type has been created using TypeBuilder.CreateType. |
public void SetCustomAttribute( |
customBuilder
Exception Type | Condition |
---|---|
ArgumentNullException | con is null. |
InvalidOperationException | The parent type of this field is complete. |
public void SetCustomAttribute( |
con
binaryAttribute
Exception Type | Condition |
---|---|
ArgumentNullException | con or binaryAttribute is null. |
InvalidOperationException | The parent type of this field is complete. |
public void SetMarshal( |
unmanagedMarshal
Exception Type | Condition |
---|---|
ArgumentNullException | unmanagedMarshal is null. |
InvalidOperationException | The containing type has been created using TypeBuilder.CreateType. |
public void SetOffset( |
iOffset
Exception Type | Condition |
---|---|
InvalidOperationException | The containing type has been created using TypeBuilder.CreateType. |
obj
value
Exception Type | Condition |
---|---|
FieldAccessException | The caller does not have permission to access this field. |
TargetException | The obj parameter is null and the field is an instance field. |
ArgumentException | The field does not exist on the object. -or- The value parameter cannot be converted and stored in the field. |
using System; using System.Reflection; using System.Globalization; public class MyClass { private string myString; public MyClass() { myString = "Old value"; } string GetField { get { return myString; } } } public class FieldInfo_SetValue { public static void Main() { try { MyClass myObject = new MyClass(); Type myType = Type.GetType("MyClass"); FieldInfo myFieldInfo = myType.GetField("myString", BindingFlags.NonPublic | BindingFlags.Instance); // Display string before applying 'Set Value' on the field. Console.WriteLine( "\nField value of 'myString': {0}", myFieldInfo.GetValue( myObject ) ); // Display 'SetValue' signature used to set the value of a field. Console.WriteLine( "Applying 'SetValue( Object,Object )' method"); // Change field value using 'SetValue' method. myFieldInfo.SetValue( myObject , "New value"); // Display string after applying 'Set Value' on the field. Console.WriteLine( "Field value of 'mystring' : {0}", myFieldInfo.GetValue( myObject ) ); // Set field value using 'SetValue' method to its old value. myFieldInfo.SetValue( myObject , "Old value" ); myFieldInfo = myType.GetField("myString", BindingFlags.NonPublic | BindingFlags.Instance); // Display string before applying 'SetValue' on the field. Console.Write( "\nField value of 'mystring' : {0}\n", myFieldInfo.GetValue( myObject ) ); // Display 'SetValue' signature used to set value of a field. Console.WriteLine( "Applying 'SetValue( Object,Object,BindingFlags,Binder,CultureInfo )' method"); // Change field value using 'SetValue' method. myFieldInfo.SetValue( myObject, "New value", BindingFlags.Default, null , null ); // Display string after applying 'SetValue' on the field. Console.WriteLine( "Field value of 'mystring' : {0}", myFieldInfo.GetValue( myObject ) ); } catch( Exception e ) { // Any exception generated is displayed. Console.WriteLine( "Exception: {0}", e.Message ); } } }
public override void SetValue( |
obj
val
invokeAttr
binder
culture
Exception Type | Condition |
---|---|
NotSupportedException | This method is not supported. |
The new value is passed as an Object. For example, if the field's type is Boolean, an instance of Object with the appropriate Boolean value is passed. Before setting the value, FieldBuilder.SetValue checks to see if the user has access permission.
Access restrictions are ignored for fully-trusted code.Private constructors, methods, fields, and properties can be accessed and invoked using Reflection whenever the code is fully-trusted.
This method is currently not supported. As a workaround, retrieve the FieldInfo by reflecting on the finished type and call FieldInfo.SetValue to set the value of the field.
[CLSCompliant(false)] |
obj
value
Exception Type | Condition |
---|---|
NotSupportedException | The caller requires the CLS alternative, but called this method instead. |
public virtual string ToString(); |