自定义属性

时间:2011-09-12 13:58:50

标签: c# properties

我正在使用windowsapps,我需要在这里做一些不同的事情。我使用ICustomTypeDescriptor创建了一个对象,其中包含一些使用我在互联网上找到的样本的自定义属性。

这是我的代码:

class ObjectWithProperties : ICustomTypeDescriptor
{
    **public int MyProperty { get; set; }**

    class MyDescriptor : PropertyDescriptor
    {
        public MyDescriptor(string name, Attribute[] attributes) :
            base(name, attributes)
        {

        }

        public override bool CanResetValue(object component)
        {
            return true;
        }

        public override Type ComponentType
        {
            get { return typeof(ObjectWithProperties); }
        }

        public override object GetValue(object component)
        {
            return (component as ObjectWithProperties)[Name];
        }

        public override bool IsReadOnly
        {
            get { return false; }
        }

        public override Type PropertyType
        {
            get { return typeof(object); }
        }

        public override void ResetValue(object component)
        {
            (component as ObjectWithProperties).properties.Remove(Name);
        }

        public override void SetValue(object component, object value)
        {
            (component as ObjectWithProperties)[Name] = value;
        }

        public override bool ShouldSerializeValue(object component)
        {
            return (component as ObjectWithProperties).properties.ContainsKey(Name);
        }
    }

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

    Dictionary<string, List<Attribute>> attributes = new Dictionary<string, List<Attribute>>();
    public void SetAttribute(string property, Attribute attribute)
    {
        if (attributes.ContainsKey(property))
            attributes[property].Add(attribute);
        else
            attributes[property] = new List<Attribute> { attribute };
    }


    #region ICustomTypeDescriptor Membres

    public AttributeCollection GetAttributes()
    {
        return TypeDescriptor.GetAttributes(this, true);
    }

    public string GetClassName()
    {
        return null;
    }

    public string GetComponentName()
    {
        return null;
    }

    public TypeConverter GetConverter()
    {
        return null;
    }

    public EventDescriptor GetDefaultEvent()
    {
        return null;
    }

    public PropertyDescriptor GetDefaultProperty()
    {
        return null;
    }

    public object GetEditor(Type editorBaseType)
    {
        return null;
    }

    public EventDescriptorCollection GetEvents(Attribute[] attributes)
    {
        return EventDescriptorCollection.Empty;
    }

    public EventDescriptorCollection GetEvents()
    {
        return EventDescriptorCollection.Empty;
    }

    public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
    {
        return new PropertyDescriptorCollection(properties.Keys.Select(key => this.attributes.ContainsKey(key)
                ? new MyDescriptor(key, this.attributes[key].ToArray()) : new MyDescriptor(key, null)).ToArray());
    }

    public PropertyDescriptorCollection GetProperties()
    {
        return new PropertyDescriptorCollection(properties.Keys.Select(key => new MyDescriptor(key, null)).ToArray());
    }

    public object GetPropertyOwner(PropertyDescriptor pd)
    {
        return this;
    }

    //public  GetCustomAttribute(string name)
    //{
    //    var bar = TypeDescriptor.GetProperties(this, new Atributos.AtributosdasPropriedades[]{})[0]; 
    //    foreach (Attribute attrib in bar.Attributes) 
    //    {
    //    } 
    //}

    #endregion
}

private void button1_Click(object sender, EventArgs e)
    {
        var obj1 = new ObjectWithProperties();
        obj1["test"] = 100;
        var obj2 = new ObjectWithProperties();
        obj2["test"] = 200;
        var obj3 = new ObjectWithProperties();
        obj3["test"] = 150;

        **var obj4 = new ObjectWithProperties();
        obj4.MyProperty = 100;**

        List<ObjectWithProperties> objects = new List<ObjectWithProperties>(new ObjectWithProperties[] { obj1, obj2, obj3, obj4 });
        dataGridView1.DataSource = objects;
    }

如你所见,完全正常。但我需要在gridview上显示属性“MyProperty”,其他的......这可能吗?你能举一些展示样品的样品吗?

非常感谢

2 个答案:

答案 0 :(得分:0)

请看看这个 http://www.4guysfromrolla.com/articles/012308-1.aspx Web控制网格视图的示例,但应指导您正确的方向。

答案 1 :(得分:0)