Setting or Getting a Field Dynamically


Reflection gives you the ability to do late binding. Late binding lets you set or get the value of a field dynamically at runtime based on the field's name, or to invoke a method based on the method's name and its parameters. You can always code to an interface, which is easier than using late binding, but if you don't have an interface and you really need to set or get the contents of a field this would be the way to do it.

The terms late binding and early binding have to do with the information that the compiler has at compile time. Early binding, the opposite of late binding, means invoking a method by writing C# code that the compiler can turn into a method invocation. Late binding is really about turning a method name in string form into a method invocation.

To set or get the value of the field through late binding, you have to have two things: an instance of the class for which you wish to set or get the field (unless the field is static in which case you don't need an instance of the class to set it or get it), and an instance of the class System.Reflection.FieldInfo .

To set or get the value of a field at runtime:

  1. Assuming that you have an object of type System.Type in a variable called currtype , type: System.Reflection.FieldInfo fi = currtype.GetField("fieldname"); where fieldname is the name of the field in the class you wish to set or get.

  2. to get the value of a static field, type fieldtype result = (fieldtype) fi.GetValue(null); where fieldtype is the type of the field, for example, int so that the line reads: int result = (int)fi.GetValue(null);

    or

    If the field isn't static, then type fieldtype result = (fieldtype) fi.GetValue(obj); where fieldtype is the type of the field and obj is the variable that holds an instance of the object containing the field.

  3. To set the value of a static field type fi.SetValue(null,val); where val is a variable or a literal dictating the value, for example: fi.SetValue(null, 50);

    or

    If the field is an instance field type fi.SetValue(obj, val); where obj is a variable containing an instance of the class with the field you wish to set and val is a variable or a literal dictating the value to which you wish to set the field, for example: fi.SetValue(obj,100); ( Figure 12.17 ).

    Figure 12.17 It isn't very difficult to set or get the value of a field using reflection. Notice that reflection is just a more generic way of making a method invocation. You get a pointer to a field from its string name, then you call the SetValue or GetValue functions.
     //get a reference to our assembly //that's where we have the definition for //the Address class Assembly ad = Assembly.GetExecutingAssembly(); object objAddress = ad.CreateInstance(                  "ordersystem.Address"); Type typeAddress = typeof(Address); FieldInfo fldStreet = typeAddress.  GetField("Street")  ; FieldInfo fldLastModified = typeAddress.  GetField("LastModified")  ; //set the Street field (instance field) fldStreet.  SetValue(objAddress,   "123 Elm St.");  //set the LastModified field //(static field) fldLastModified.  SetValue(null,   System.DateTime.Now);  //get the Street field string Street = (string)fldStreet.  GetValue(objAddress);  //get the LastModified field DateTime LastModified =          (DateTime)fldLastModified.  GetValue(null);  

graphics/tick.gif Tips

  • Unless an administrator uses code access security to block reflection permissions, you can set and read the value of private fields as well as non-private fields for any class. Without reflection permission you can only set and get the value of public fields.

  • Setting and getting properties is done in the same fashion as setting and getting fields. Information about the property is stored in an object of type System.Reflection.PropertyInfo instead of a FieldInfo object, and you use GetProperty() instead of GetField() to get information about the property. Other than that, you use GetValue() and SetValue() just like in fields ( Figure 12.18 ).

    Figure 12.18 For properties, you follow the same procedure as for fields except you call GetProperty and use a PropertyInfo object rather than FieldInfo. The SetValue and GetValue functions have an extra parameter for properties, because a property can be an indexer, and indexers can have arguments, whereas fields never have arguments.
     PropertyInfo propPhone = typeAddress.  GetProperty("Phone")  ; propPhone.SetValue(objAddress, "555-1212",null); string phone = (string)propPhone.GetValue(objAddress, null); 
  • If your class has an indexer, you can reach that indexer using GetProperty() and asking for the Item property ( Figure 12.19 ).

    Figure 12.19 An indexer is basically a property called Item that has arguments. In other languages, like VB.NET, you can create properties with any name that have arguments.
     //use Item to reach the indexer PropertyInfo propItem = typeAddress.  GetProperty("Item")  ; //the last parameter lets you set the //values for the indexer propItem.SetValue(objAddress,"Row5,Col3",  new object[] {5,3}  ); string val = (string)propItem.GetValue(objAddress,  new object[] {5,3}  ); 



C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net