如何在C#中创建动态属性?

时间:2009-06-03 21:03:29

标签: c#

我正在寻找一种创建具有一组静态属性的类的方法。在运行时,我希望能够从数据库向此对象添加其他动态属性。我还想为这些对象添加排序和过滤功能。

如何在C#中执行此操作?

12 个答案:

答案 0 :(得分:56)

你可以使用字典,比如说

Dictionary<string,object> properties;

我认为在大多数情况下,类似的事情都是这样做的。就像这样 在任何情况下,你都不会从使用set和get访问器创建“真实”属性中获得任何东西,因为它只会在运行时创建,而你不会在代码中使用它...

这是一个示例,显示了过滤和排序(无错误检查)的可能实现:

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication1 {

    class ObjectWithProperties {
        Dictionary<string, object> properties = new Dictionary<string,object>();

        public object this[string name] {
            get { 
                if (properties.ContainsKey(name)){
                    return properties[name];
                }
                return null;
            }
            set {
                properties[name] = value;
            }
        }

    }

    class Comparer<T> : IComparer<ObjectWithProperties> where T : IComparable {

        string m_attributeName;

        public Comparer(string attributeName){
            m_attributeName = attributeName;
        }

        public int Compare(ObjectWithProperties x, ObjectWithProperties y) {
            return ((T)x[m_attributeName]).CompareTo((T)y[m_attributeName]);
        }

    }

    class Program {

        static void Main(string[] args) {

            // create some objects and fill a list
            var obj1 = new ObjectWithProperties();
            obj1["test"] = 100;
            var obj2 = new ObjectWithProperties();
            obj2["test"] = 200;
            var obj3 = new ObjectWithProperties();
            obj3["test"] = 150;
            var objects = new List<ObjectWithProperties>(new ObjectWithProperties[]{ obj1, obj2, obj3 });

            // filtering:
            Console.WriteLine("Filtering:");
            var filtered = from obj in objects
                         where (int)obj["test"] >= 150
                         select obj;
            foreach (var obj in filtered){
                Console.WriteLine(obj["test"]);
            }

            // sorting:
            Console.WriteLine("Sorting:");
            Comparer<int> c = new Comparer<int>("test");
            objects.Sort(c);
            foreach (var obj in objects) {
                Console.WriteLine(obj["test"]);
            }
        }

    }
}

答案 1 :(得分:30)

如果您需要这个用于数据绑定目的,可以使用自定义描述符模型执行此操作...通过实施ICustomTypeDescriptorTypeDescriptionProvider和/或TypeCoverter,您可以创建运行时自己的PropertyDescriptor个实例。这就是DataGridViewPropertyGrid等用于显示属性的控件。

要绑定到列表,您需要ITypedListIList;用于基本排序:IBindingList;用于过滤和高级排序:IBindingListView;对于完整的“新行”支持(DataGridView):ICancelAddNew(p!)。

虽然工作是 很多 DataTable(虽然我讨厌它)是做同样事情的廉价方式。如果您不需要数据绑定,只需使用哈希表;-p

这是一个simple example - 但你可以做更多......

答案 2 :(得分:28)

像MVC 3中的ViewBag一样使用ExpandoObject

答案 3 :(得分:12)

创建名为“Properties”的Hashtable并将属性添加到其中。

答案 4 :(得分:12)

我不确定你真的想做你想说的事情,但我不能理解为什么!

在类被JIT后,您无法向类中添加属性。

最接近的是使用Reflection.Emit动态创建子类型并复制现有字段,但您必须自己更新对该对象的所有引用。

您也无法在编译时访问这些属性。

类似的东西:

public class Dynamic
{
    public Dynamic Add<T>(string key, T value)
    {
        AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("DynamicAssembly"), AssemblyBuilderAccess.Run);
        ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("Dynamic.dll");
        TypeBuilder typeBuilder = moduleBuilder.DefineType(Guid.NewGuid().ToString());
        typeBuilder.SetParent(this.GetType());
        PropertyBuilder propertyBuilder = typeBuilder.DefineProperty(key, PropertyAttributes.None, typeof(T), Type.EmptyTypes);

        MethodBuilder getMethodBuilder = typeBuilder.DefineMethod("get_" + key, MethodAttributes.Public, CallingConventions.HasThis, typeof(T), Type.EmptyTypes);
        ILGenerator getter = getMethodBuilder.GetILGenerator();
        getter.Emit(OpCodes.Ldarg_0);
        getter.Emit(OpCodes.Ldstr, key);
        getter.Emit(OpCodes.Callvirt, typeof(Dynamic).GetMethod("Get", BindingFlags.Instance | BindingFlags.NonPublic).MakeGenericMethod(typeof(T)));
        getter.Emit(OpCodes.Ret);
        propertyBuilder.SetGetMethod(getMethodBuilder);

        Type type = typeBuilder.CreateType();

        Dynamic child = (Dynamic)Activator.CreateInstance(type);
        child.dictionary = this.dictionary;
        dictionary.Add(key, value);
        return child;
    }

    protected T Get<T>(string key)
    {
        return (T)dictionary[key];
    }

    private Dictionary<string, object> dictionary = new Dictionary<string,object>();
}

我没有在这台机器上安装VS,所以让我知道是否有任何大规模的错误(嗯......除了大量的性能问题,但我没有编写规范!)

现在你可以使用它了:

Dynamic d = new Dynamic();
d = d.Add("MyProperty", 42);
Console.WriteLine(d.GetType().GetProperty("MyProperty").GetValue(d, null));

您也可以像支持后期绑定的语言中的普通属性一样使用它(例如,VB.NET)

答案 5 :(得分:3)

我已经使用ICustomTypeDescriptor接口和Dictionary完成了这一步。

为动态属性实现ICustomTypeDescriptor:

我最近要求将网格视图绑定到记录对象,该记录对象可以具有可在运行时添加和删除的任意数量的属性。这是为了允许用户将新列添加到结果集以输入另一组数据。

这可以通过将每个数据“row”作为字典来实现,其中键是属性名称,值是字符串或可以存储指定行的属性值的类。当然,拥有Dictionary对象列表将无法绑定到网格。这是ICustomTypeDescriptor的用武之地。

通过为Dictionary创建一个包装类并使其符合ICustomTypeDescriptor接口,可以覆盖返回对象属性的行为。

看看下面数据'row'类的实现:

/// <summary>
/// Class to manage test result row data functions
/// </summary>
public class TestResultRowWrapper : Dictionary<string, TestResultValue>, ICustomTypeDescriptor
{
    //- METHODS -----------------------------------------------------------------------------------------------------------------

    #region Methods

    /// <summary>
    /// Gets the Attributes for the object
    /// </summary>
    AttributeCollection ICustomTypeDescriptor.GetAttributes()
    {
        return new AttributeCollection(null);
    }

    /// <summary>
    /// Gets the Class name
    /// </summary>
    string ICustomTypeDescriptor.GetClassName()
    {
        return null;
    }

    /// <summary>
    /// Gets the component Name
    /// </summary>
    string ICustomTypeDescriptor.GetComponentName()
    {
        return null;
    }

    /// <summary>
    /// Gets the Type Converter
    /// </summary>
    TypeConverter ICustomTypeDescriptor.GetConverter()
    {
        return null;
    }

    /// <summary>
    /// Gets the Default Event
    /// </summary>
    /// <returns></returns>
    EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
    {
        return null;
    }

    /// <summary>
    /// Gets the Default Property
    /// </summary>
    PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
    {
        return null;
    }

    /// <summary>
    /// Gets the Editor
    /// </summary>
    object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
    {
        return null;
    }

    /// <summary>
    /// Gets the Events
    /// </summary>
    EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
    {
        return new EventDescriptorCollection(null);
    }

    /// <summary>
    /// Gets the events
    /// </summary>
    EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
    {
        return new EventDescriptorCollection(null);
    }

    /// <summary>
    /// Gets the properties
    /// </summary>
    PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
    {
        List<propertydescriptor> properties = new List<propertydescriptor>();

        //Add property descriptors for each entry in the dictionary
        foreach (string key in this.Keys)
        {
            properties.Add(new TestResultPropertyDescriptor(key));
        }

        //Get properties also belonging to this class also
        PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(this.GetType(), attributes);

        foreach (PropertyDescriptor oPropertyDescriptor in pdc)
        {
            properties.Add(oPropertyDescriptor);
        }

        return new PropertyDescriptorCollection(properties.ToArray());
    }

    /// <summary>
    /// gets the Properties
    /// </summary>
    PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
    {
        return ((ICustomTypeDescriptor)this).GetProperties(null);
    }

    /// <summary>
    /// Gets the property owner
    /// </summary>
    object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
    {
        return this;
    }

    #endregion Methods

    //---------------------------------------------------------------------------------------------------------------------------
}

注意:在GetProperties方法中,我可以在读取性能时缓存PropertyDescriptors但是因为我在运行时添加和删除列,所以我总是希望它们重建

您还会在GetProperties方法中注意到为字典条目添加的属性描述符的类型为TestResultPropertyDescriptor。这是一个自定义属性描述符类,用于管理属性的设置和检索方式。看看下面的实现:

/// <summary>
/// Property Descriptor for Test Result Row Wrapper
/// </summary>
public class TestResultPropertyDescriptor : PropertyDescriptor
{
    //- PROPERTIES --------------------------------------------------------------------------------------------------------------

    #region Properties

    /// <summary>
    /// Component Type
    /// </summary>
    public override Type ComponentType
    {
        get { return typeof(Dictionary<string, TestResultValue>); }
    }

    /// <summary>
    /// Gets whether its read only
    /// </summary>
    public override bool IsReadOnly
    {
        get { return false; }
    }

    /// <summary>
    /// Gets the Property Type
    /// </summary>
    public override Type PropertyType
    {
        get { return typeof(string); }
    }

    #endregion Properties

    //- CONSTRUCTOR -------------------------------------------------------------------------------------------------------------

    #region Constructor

    /// <summary>
    /// Constructor
    /// </summary>
    public TestResultPropertyDescriptor(string key)
        : base(key, null)
    {

    }

    #endregion Constructor

    //- METHODS -----------------------------------------------------------------------------------------------------------------

    #region Methods

    /// <summary>
    /// Can Reset Value
    /// </summary>
    public override bool CanResetValue(object component)
    {
        return true;
    }

    /// <summary>
    /// Gets the Value
    /// </summary>
    public override object GetValue(object component)
    {
          return ((Dictionary<string, TestResultValue>)component)[base.Name].Value;
    }

    /// <summary>
    /// Resets the Value
    /// </summary>
    public override void ResetValue(object component)
    {
        ((Dictionary<string, TestResultValue>)component)[base.Name].Value = string.Empty;
    }

    /// <summary>
    /// Sets the value
    /// </summary>
    public override void SetValue(object component, object value)
    {
        ((Dictionary<string, TestResultValue>)component)[base.Name].Value = value.ToString();
    }

    /// <summary>
    /// Gets whether the value should be serialized
    /// </summary>
    public override bool ShouldSerializeValue(object component)
    {
        return false;
    }

    #endregion Methods

    //---------------------------------------------------------------------------------------------------------------------------
}

要在此类上查看的主要属性是GetValue和SetValue。在这里,您可以看到组件被转换为字典,并且其中的键的值被设置或检索。重要的是,此类中的字典与Row包装类中的类型相同,否则强制转换将失败。创建描述符时,传入键(属性名称)并用于查询字典以获取正确的值。

取自我的博客:

ICustomTypeDescriptor Implementation for dynamic properties

答案 6 :(得分:1)

我不确定你的理由是什么,即使你可以用Reflection Emit以某种方式将其拉出来(我不确定你能不能),但这听起来并不是一个好主意。可能更好的想法是拥有某种类型的Dictionary,你可以通过类中的方法包装对字典的访问。这样,您可以将数据库中的数据存储在此字典中,然后使用这些方法检索它们。

答案 7 :(得分:1)

您应该查看WPF使用的DependencyObjects,它们遵循类似的模式,从而可以在运行时分配属性。如上所述,这最终指向使用哈希表。

另一个有用的事情是CSLA.Net。该代码是免费提供的,并使用了您所看到的一些原则\模式。

另外,如果你正在考虑排序和过滤,我猜你将使用某种网格。一个有用的实现接口是ICustomTypeDescriptor,这可以让你有效地覆盖对象被反射时发生的事情,这样你就可以将反射器指向对象自己的内部哈希表。

答案 8 :(得分:1)

作为一些orsogufo代码的替代品,因为我最近自己带了一本字典来解决同样的问题,这里是我的[]运算符:

public string this[string key]
{
    get { return properties.ContainsKey(key) ? properties[key] : null; }

    set
    {
        if (properties.ContainsKey(key))
        {
            properties[key] = value;
        }
        else
        {
            properties.Add(key, value);
        }
    }
}

通过此实现,当您使用[]=时,如果字典中尚不存在新的键值对,则setter将添加新的键值对。

另外,对我而言propertiesIDictionary,在构造函数中我将其初始化为new SortedDictionary<string, string>()

答案 9 :(得分:0)

为什么不使用具有属性名称的索引器作为传递给索引器的字符串值?

答案 10 :(得分:0)

难道你不能让你的类公开一个Dictionary对象吗?您可以在运行时将数据(带有一些标识符)简单地插入到字典中,而不是“将更多属性附加到对象”。

答案 11 :(得分:0)

如果是绑定,那么您可以从XAML

引用索引器
Text="{Binding [FullName]}"

这里引用了带有“FullName”键的类索引器

相关问题