0
public class UserDetails
    {
        public string UserID { get; set; }

        public string UserName { get; set; }
    }

在这里我想动态添加属性。类型属性名称会随着我想要创建属性的值动态变化。

4

3 回答 3

1

这似乎可行,但需要强制转换才能获得“灵活”属性。

UserDetails班级_

public class UserDetails
{
    private dynamic _internal;

    public static implicit operator System.Dynamic.ExpandoObject(UserDetails details)
    {
        return details._internal;
    }

    public UserDetails()
    {
        _internal = new System.Dynamic.ExpandoObject();
    }

    public string UserID 
    { 
        get
        {
            return _internal.UserID;
        }
        set
        {
            _internal.UserID = value;
        }
    }

    public string UserName
    { 
        get
        {
            return _internal.UserName;
        }
        set
        {
            _internal.UserName = value;
        }
    }
}

并使用类

UserDetails user = new UserDetails();
user.UserName = "bill";
user.UserID = "1";

dynamic dynamicUser = (System.Dynamic.ExpandoObject)user;
dynamicUser.newMember = "check this out!";

Console.WriteLine(user.UserName);
Console.WriteLine(user.UserID);
Console.WriteLine(dynamicUser.UserName);
Console.WriteLine(dynamicUser.UserID);
Console.WriteLine(dynamicUser.newMember);
于 2013-02-01T12:10:02.207 回答
0

似乎您真正需要的只是一个“Property Bag”,即一个无序的容器,您可以在其中插入名称/值对,其中名称是字符串,值是任何类型的对象。

网上有很多 PropertyBag 的实现;这是我拼凑在一起的一个快速而肮脏的例子:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;

namespace Demo
{
    public static class Program
    {
        private static void Main(string[] args)
        {
            var properties = new PropertyBag();

            properties["Colour"]   = Color.Red;
            properties["π"]        = Math.PI;
            properties["UserId"]   = "My User ID";
            properties["UserName"] = "Matthew";

            // Enumerate all properties.

            foreach (var property in properties)
            {
                Console.WriteLine(property.Key + " = " + property.Value);
            }

            // Check if property exists:

            if (properties["UserName"] != null)
            {
                Console.WriteLine("[UserName] exists.");
            }

            // Get a property:

            double π = (double)properties["π"];
            Console.WriteLine("Pi = " + π);
        }
    }

    public sealed class PropertyBag: IEnumerable<KeyValuePair<string, object>>
    {
        public object this[string propertyName]
        {
            get
            {
                if (propertyName == null)
                {
                    throw new ArgumentNullException("propertyName");
                }

                if (_dict.ContainsKey(propertyName))
                {
                    return _dict[propertyName];
                }
                else
                {
                    return null;
                }
            }

            set
            {
                if (propertyName == null)
                {
                    throw new ArgumentNullException("propertyName");
                }

                _dict[propertyName] = value;
            }
        }

        public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
        {
            return _dict.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        private readonly Dictionary<string, object> _dict = new Dictionary<string, object>();
    }
}
于 2013-02-01T11:57:18.027 回答
0

是的,但它很复杂。检查实施ICustomTypeDescriptor。如果你让你的基类实现它,你将能够动态地添加属性。网上有教程,在网上搜索界面。

第二件事可以是使用ExpandoObject
这样你就不能从基类继承,但实现起来要简单得多。

于 2013-02-01T11:38:48.547 回答