- 精品下载 | 实用查询 | 词典查询 | 桌面壁纸 | 网址 | 笑话 | FLASH频道 | 天气文章资讯 | 站长工具 | 证件办理 | 闪字生成 | 广告代码 | 在线手册 | 有问必答
您现在的位置: 蓝派网 >> 文章中心 >> 网络编程 >> .NET >> c#(Csharp) >> 正文
站内文章搜索:           

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

作者:佚名    文章来源:CSDN    更新时间 :2007-5-30 15:40:00
http://www.cnblogs.com/Laser_Lu/archive/2004/08/01/29171.html

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 the string before applying SetValue to the field.
            Console.WriteLine( " The field value of myString is {0}.", myFieldInfo.GetValue(myObject));  
            // Display the SetValue signature used to set the value of a field.
            Console.WriteLine( "Applying SetValue(Object, Object).");     
            // Change the field value using the SetValue method.  
            myFieldInfo.SetValue(myObject, "New value");  
            // Display the string after applying SetValue to the field.
            Console.WriteLine( "The field value of mystring is {0}.", myFieldInfo.GetValue(myObject));
            // Set the field value to its old value.  
            myFieldInfo.SetValue( myObject , "Old value" );  
            myFieldInfo = myType.GetField("myString", BindingFlags.NonPublic | BindingFlags.Instance);  
            // Display the string before applying SetValue to the field.
            Console.Write( " The field value of mystring is {0}. ", myFieldInfo.GetValue(myObject));  
            // Display the SetValue signature used to set the value of a field.
            Console.WriteLine("Applying SetValue(Object, Object, BindingFlags, Binder, CultureInfo).");  
            // Change the field value using the SetValue method.  
            myFieldInfo.SetValue(myObject, "New value", BindingFlags.Public, null, null);      
            // Display the string after applying SetValue to the field.
            Console.WriteLine( "The field value of mystring is {0}.", myFieldInfo.GetValue(myObject));
        }
        catch( Exception e )
        {
            // Any exception generated is displayed.  
            Console.WriteLine( "Exception: {0}", e.Message );
        }
    }
}

http://msdn.microsoft.com/library/chs/default.asp?url=/library/CHS/cpref/html/frlrfsystemtypeclasstopic.asp

using System;
using System.Reflection;
using System.Security;

public class MyFieldClassA
{
private string field = "A Field";
public string Field
{
  get
  {
   return field;
  }
  set
  {
   if(field!=value)
   {
    field=value;
   }
  }
}
}
public class MyFieldClassB
{
public string field = "B Field";
public string Field
{
  get
  {
   return field;
  }
  set
  {
   if(field!=value)
   {
    field=value;
   }
  }
}
}

public class MyFieldInfoClass
{
public static void Main()
{
  try
  {
   MyFieldClassB myFieldObjectB = new MyFieldClassB();
   MyFieldClassA myFieldObjectA = new MyFieldClassA();
   Type myTypeA = Type.GetType("MyFieldClassA");
   FieldInfo myFieldInfo = myTypeA.GetField("field",BindingFlags.NonPublic|BindingFlags.Instance);
   Type myTypeB = Type.GetType("MyFieldClassB");
   FieldInfo myFieldInfo1 = myTypeB.GetField("field", BindingFlags.Public | BindingFlags.Instance);
   Console.WriteLine("The value of the field is : {0} ", myFieldInfo.GetValue(myFieldObjectA));
   Console.WriteLine("The value of the field is : {0} ", myFieldInfo1.GetValue(myFieldObjectB));
  }
  catch(SecurityException e)
  {
   Console.WriteLine("Exception Raised!");
   Console.WriteLine("Message :"+e.Message);
  }
  catch(ArgumentNullException e)
  {
   Console.WriteLine("Exception Raised!");
   Console.WriteLine("Message :"+e.Message);
  }
  catch(Exception e)
  {
   Console.WriteLine("Exception Raised!");
   Console.WriteLine("Message :"+e.Message);
  }
}
}


发表评论】【打印此文】【关闭窗口】【点击数:
★好玩的休闲小游戏★
网友评论:
数据载入中,请稍后……