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/

  • 相关阅读:
    对话框隐藏的简单实现(转)
    CoInitializeSecurity 学习(转)
    VC++实现CD/DVD刻录(转)
    网络连接的保活机制(心跳机制转)
    tcp架构
    木马的隐藏及其启动方式 (转)
    vs2005 控件bug
    vs2005 虚拟调试配置(转)
    sprintf用法(转)
    要掌握真正的免杀必须懂汇编【汇编语言超浓缩教程】(转)
  • 原文地址:https://www.cnblogs.com/simhare/p/1520867.html
Copyright © 2011-2022 走看看