反射可以通过FieldInfo.SetValue设置任何字段的值。

 

None.gif using  System;
None.gif
using  System.Reflection;
None.gif
using  System.Globalization;
None.gif
None.gif
public   class  MyClass
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
private string myString;
InBlock.gif    
public MyClass()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        myString 
= "Old value";
ExpandedSubBlockEnd.gif    }

InBlock.gif    
string GetField
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
get
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return myString;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
public   class  FieldInfo_SetValue
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
try
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            MyClass myObject 
= new MyClass();
InBlock.gif            Type myType 
= Type.GetType("MyClass");
InBlock.gif            FieldInfo myFieldInfo 
= myType.GetField("myString", BindingFlags.NonPublic | BindingFlags.Instance); 
InBlock.gif            
// Display the string before applying SetValue to the field.
InBlock.gif
            Console.WriteLine( " The field value of myString is {0}.", myFieldInfo.GetValue(myObject)); 
InBlock.gif            
// Display the SetValue signature used to set the value of a field.
InBlock.gif
            Console.WriteLine( "Applying SetValue(Object, Object).");    
InBlock.gif            
// Change the field value using the SetValue method. 
InBlock.gif
            myFieldInfo.SetValue(myObject, "New value"); 
InBlock.gif            
// Display the string after applying SetValue to the field.
InBlock.gif
            Console.WriteLine( "The field value of mystring is {0}.", myFieldInfo.GetValue(myObject));
InBlock.gif            
// Set the field value to its old value. 
InBlock.gif
            myFieldInfo.SetValue( myObject , "Old value" ); 
InBlock.gif            myFieldInfo 
= myType.GetField("myString", BindingFlags.NonPublic | BindingFlags.Instance); 
InBlock.gif            
// Display the string before applying SetValue to the field.
InBlock.gif
            Console.Write( " The field value of mystring is {0}. ", myFieldInfo.GetValue(myObject)); 
InBlock.gif            
// Display the SetValue signature used to set the value of a field.
InBlock.gif
            Console.WriteLine("Applying SetValue(Object, Object, BindingFlags, Binder, CultureInfo)."); 
InBlock.gif            
// Change the field value using the SetValue method. 
InBlock.gif
            myFieldInfo.SetValue(myObject, "New value", BindingFlags.Public, nullnull);     
InBlock.gif            
// Display the string after applying SetValue to the field.
InBlock.gif
            Console.WriteLine( "The field value of mystring is {0}.", myFieldInfo.GetValue(myObject));
ExpandedSubBlockEnd.gif        }

InBlock.gif        
catch( Exception e )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// Any exception generated is displayed. 
InBlock.gif
            Console.WriteLine( "Exception: {0}", e.Message );
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值