zoukankan      html  css  js  c++  java
  • 反射的一种用法

    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, nullnull);      
                
    // 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://kb.cnblogs.com/page/42051/

    http://kb.cnblogs.com/page/42279/

  • 相关阅读:
    C# decimal保留指定的小数位数,不四舍五入
    C# :实现水印与图片合成,并利用Graphics 压缩图像质量 , (委托实现listBox的动态添加提示)
    手机游戏模拟器汇总 用于开发
    WinAPI 操作串口
    C#图片压缩算法
    SQL SERVER 2008 无法启动TSQL调试的解决方法
    C#放缩、截取、合并图片并生成高质量新图的类
    C#图片处理之: 另存为压缩质量可自己控制的JPEG
    URL及short URL短网址
    1的补码及2的补码
  • 原文地址:https://www.cnblogs.com/simhare/p/1520867.html
Copyright © 2011-2022 走看看