1. 程式人生 > 其它 >C#基礎知識---動態為型別新增屬性

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}");
        }
    }
}