(論壇答疑點滴)如何動態(tài)設(shè)定類的屬性和字段?
2024-07-21 02:15:53
供稿:網(wǎng)友
 
正好有人問這個,代碼非常簡單,最基本的應(yīng)用,直接貼代碼 
using system; 
 
namespace test 
{ 
    /**//// <summary> 
    /// class1 的摘要說明。 
    /// </summary> 
    class class1 
    { 
        /**//// <summary> 
        /// 應(yīng)用程序的主入口點。 
        /// </summary> 
        [stathread] 
        static void main(string[] args) 
        { 
            // 
            // todo: 在此處添加代碼以啟動應(yīng)用程序 
            // 
            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則重新設(shè)定 
            //setfield2(ht1,dv)//如果hashtable中的key匹配類中的字段則重新設(shè)定,效果等同于setfield1 
            console.writeline(dv.fielda);//a 
            console.writeline(dv.fieldb);//bb 
            console.writeline(dv.fieldc);//c 
            system.collections.hashtable ht2=new system.collections.hashtable(); 
            ht2.add("propertyb","b"); 
            ht2.add("propertyc","c"); 
            setproperty1(ht2,dv);//如果類中的屬性匹配hashtable中的key則重新設(shè)定 
            //setproperty2(ht2,dv);//如果hashtable中的key匹配類中的屬性則重新設(shè)定,效果等同于setproperty1 
            console.writeline(dv.fielda);//a 
            console.writeline(dv.fieldb);//b 
            console.writeline(dv.fieldc);//c 
             
        } 
 
        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; 
            } 
        } 
    } 
 
} 
 
網(wǎng)站運營seo文章大全提供全面的站長運營經(jīng)驗及seo技術(shù)!