相关疑难解决方法(0)

在运行时添加属性

我有一个程序员可以用来动态添加新属性的类.为此,它实现了ICustomTypeDescriptor能够覆盖GetProperties()方法.

public class DynamicProperty
{
    public object Value { get; set; }

    public Type Type { get; set; }

    public string Name { get; set; }

    public Collection<Attribute> Attributes { get; set; }
}

public class DynamicClass : ICustomTypeDescriptor
{
    // Collection to code add dynamic properties
    public KeyedCollection<string, DynamicProperty> Properties
    {
        get;
        private set;
    }

    // ICustomTypeDescriptor implementation
    PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
    {
        // Properties founded within instance
        PropertyInfo[] instanceProps = this.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);

        // Fill property …
Run Code Online (Sandbox Code Playgroud)

c# runtime properties

15
推荐指数
1
解决办法
2万
查看次数

标签 统计

c# ×1

properties ×1

runtime ×1