zoukankan      html  css  js  c++  java
  • [转]按照HashTable动态设定类的属性和字段

    using System; 
     
    namespace Test 

        
    /**//// <summary> 
        
    /// Class1 的摘要说明。 
        
    /// </summary> 
        class Class1 
        { 
            
    /**//// <summary> 
            
    /// 应用程序的主入口点。 
            
    /// </summary> 
            [STAThread] 
            
    static void Main(string[] args) 
            { 
                
    // 
                
    // TODO: 在此处添加代码以启动应用程序 
                
    // 
                MyFieldClass dv=new MyFieldClass(); 
                System.Collections.Hashtable ht1
    =new System.Collections.Hashtable(); 
                ht1.Add(
    "FieldA","A"); 
                ht1.Add(
    "FieldC","C"); 
                SetField1(ht1,dv);
    //如果类中的字段匹配Hashtable中的Key则重新设定 
                
    //SetField2(ht1,dv)//如果Hashtable中的Key匹配类中的字段则重新设定,效果等同于SetField1 
                Console.WriteLine(dv.FieldA);//
                Console.WriteLine(dv.FieldB);//bb 
                Console.WriteLine(dv.FieldC);//
                System.Collections.Hashtable ht2=new System.Collections.Hashtable(); 
                ht2.Add(
    "PropertyB","b"); 
                ht2.Add(
    "PropertyC","c"); 
                SetProperty1(ht2,dv);
    //如果类中的属性匹配Hashtable中的Key则重新设定 
                
    //SetProperty2(ht2,dv);//如果Hashtable中的Key匹配类中的属性则重新设定,效果等同于SetProperty1 
                Console.WriteLine(dv.FieldA);//
                Console.WriteLine(dv.FieldB);//
                Console.WriteLine(dv.FieldC);//
                 
            } 
     
            
    public static void SetProperty1(System.Collections.Hashtable ht1,MyFieldClass dv) 
            { 
                
    foreach(System.Collections.DictionaryEntry de in ht1) 
                { 
                    System.Reflection.PropertyInfo pi
    =dv.GetType().GetProperty(de.Key.ToString()); 
                    
    if(pi!=null)pi.SetValue(dv,de.Value.ToString(),null); 
                } 
            } 
     
            
    public static void SetProperty2(System.Collections.Hashtable ht1,MyFieldClass dv) 
            { 
                
    foreach(System.Reflection.PropertyInfo pi in dv.GetType().GetProperties()) 
                { 
                    
    if(ht1.Contains(pi.Name))pi.SetValue(dv,ht1[pi.Name],null); 
                } 
            } 
     
            
    public static void SetField1(System.Collections.Hashtable ht2,MyFieldClass dv) 
            { 
                
    foreach(System.Collections.DictionaryEntry de in ht2) 
                { 
                    System.Reflection.FieldInfo fi
    =dv.GetType().GetField(de.Key.ToString()); 
                    
    if(fi!=null)fi.SetValue(dv,de.Value.ToString()); 
                } 
            } 
     
            
    public static void SetField2(System.Collections.Hashtable ht2,MyFieldClass dv) 
            { 
                
    foreach(System.Reflection.FieldInfo fi in dv.GetType().GetFields()) 
                { 
                    
    if(ht2.Contains(fi.Name))fi.SetValue(dv,ht2[fi.Name]); 
                } 
            } 
        } 
     
        
    public class MyFieldClass 
        { 
            
    public string FieldA="aa"
            
    public string FieldB="bb"
            
    public string FieldC="cc"
     
            
    public string PropertyA 
            { 
                
    get 
                { 
                    
    return FieldA; 
                } 
                
    set 
                { 
                    FieldA
    =value; 
                } 
            } 
     
            
    public string PropertyB 
            { 
                
    get 
                { 
                    
    return FieldB; 
                } 
                
    set 
                { 
                    FieldB
    =value; 
                } 
            } 
     
            
    public string PropertyC 
            { 
                
    get 
                { 
                    
    return FieldC; 
                } 
                
    set 
                { 
                    FieldC
    =value; 
                } 
            } 
        } 
     


    原文 http://www.cnblogs.com/lovecherry/archive/2005/04/27/146461.html
  • 相关阅读:
    找一个数组的最大和的连续子数组(时间复杂度 O(n))
    Web版需求征集系统所得2,servlet中request.getParameter获值乱码问题解决
    Web版需求征集系统所得1,servlet中获取checkbox复选框的值
    人月神话读后感(三)
    Web版记账本开发记录(三)开发过程遇到的问题小结2
    人月神话读后感(二)
    团队开发项目--校园知网 nabcd 需求分析
    软件工程--第六周学习进度
    软件工程--第五周学习进度
    人月神话阅读笔记03
  • 原文地址:https://www.cnblogs.com/Bruce_H21/p/546940.html
Copyright © 2011-2022 走看看