国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

(論壇答疑點滴)如何動態(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ù)!
  • 發(fā)表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發(fā)表
    主站蜘蛛池模板: 江津市| 永平县| 吉隆县| 延安市| 阿瓦提县| 芮城县| 施甸县| 伊川县| 剑阁县| 泾源县| 武宁县| 静乐县| 阿鲁科尔沁旗| 台北县| 扶余县| 合江县| 万全县| 临城县| 阿拉尔市| 三门县| 神农架林区| 莱芜市| 新疆| 潜山县| 尼勒克县| 竹北市| 石屏县| 昌黎县| 上杭县| 光泽县| 东港市| 邯郸县| 丹巴县| 郧西县| 翁牛特旗| 哈巴河县| 新乐市| 香格里拉县| 深圳市| 云阳县| 团风县|