• C#基础知识动态为类型添加属性


    一、概述

    通常情况下,我们是事先在类型中定义好属性的,但有时候,我们需要动态为一个类型添加某些属性,这个时候,我们就需要使用DynamicObject类型了。

    二、Demo

    复制代码
    using System;
    using System.Collections.Generic;
    using System.Dynamic;
    
    namespace ConsoleDynamicModel
    {
        public class DynamicModel : DynamicObject
        {
            private string propertyName;
            public string PropertyName
            {
                get { return propertyName; }
                set { propertyName = value; }
            }
    
            // The inner dictionary.
            Dictionary<string, object> dicProperty
                = new Dictionary<string, object>();
            public Dictionary<string, object> DicProperty
            {
                get
                {
                    return dicProperty;
                }
            }
    
    
            // This property returns the number of elements
            // in the inner dictionary.
            public int Count
            {
                get
                {
                    return dicProperty.Count;
                }
            }
    
            // If you try to get a value of a property 
            // not defined in the class, this method is called.
            public override bool TryGetMember(
                GetMemberBinder binder, out object result)
            {
                // Converting the property name to lowercase
                // so that property names become case-insensitive.
                string name = binder.Name;
    
                // If the property name is found in a dictionary,
                // set the result parameter to the property value and return true.
                // Otherwise, return false.
                return dicProperty.TryGetValue(name, out result);
            }
    
            // If you try to set a value of a property that is
            // not defined in the class, this method is called.
            public override bool TrySetMember(
                SetMemberBinder binder, object value)
            {
                // Converting the property name to lowercase
                // so that property names become case-insensitive.
                if (binder.Name == "Property")
                {
                    dicProperty[PropertyName] = value;
                }
                else
                {
                    dicProperty[binder.Name] = value;
                }
    
    
                // You can always add a value to a dictionary,
                // so this method always returns true.
                return true;
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("动态为类型添加属性");
                dynamic dynamicModel = new DynamicModel();
                List<string> myList = new List<string>();
                myList.Add("Name");
                myList.Add("Age");
                myList.Add("Hobby");
    
                List<string> myValueList = new List<string>();
                myValueList.Add("Mary");
                myValueList.Add("18");
                myValueList.Add("Dance");
    
                for (int i = 0; i < myList.Count; i++)
                {
                    string myAttr = myList[i];
                    dynamicModel.PropertyName = myAttr;
                    dynamicModel.Property = myValueList[i];
                }
    
                Console.WriteLine($"Name: {dynamicModel.Name}");
                Console.WriteLine($"Age: {dynamicModel.Age}");
                Console.WriteLine($"Hobby: {dynamicModel.Hobby}");
            }
        }
    }
  • 相关阅读:
    Oracle 恢复[rman全备份集+当期归档日志]
    将ping结果输出到txt文件
    诗经 硕鼠 注释
    DIV里Table的宽度设置为100%后页面出现滚动条的解决办法;DIV下移的解决办法 IE 和 FireFox 都通过
    2007春节上海南站买火车票实录
    GG和baidu网络广告真的那么好做吗菜鸟不要被人忽悠了。做站长两个月总结
    iframe 自适应高度 IE Firefox 通过
    飘云QQ宣布终止后续开发 称不懂游戏规则玩不起
    我的小站:诗词在线 http://www.chinapoesy.com 欢迎大家测试速度。特别是网通的。
    丑奴儿欣赏 辛弃疾 诗词在线
  • 原文地址:https://www.cnblogs.com/itjeff/p/16180519.html
Copyright © 2020-2023  润新知