CategoryAttribute和PropertyInfo

时间:2013-10-20 01:19:46

标签: c#

我有一个包含数百个属性的类。每个属性都使用[CategoryAttribute(“My Category Name”)]属性子句声明,以便它可以很好地显示在PropertyGrid中。我想利用这个相同的CategoryAttribute属性来设置类中使用特定categoryAttribute catagories标记的所有属性的值。下面的代码编译并运行但它没有完成任务,因为att_coll不包含我期望的CategoryAttribute属性。有谁知道如何做到这一点?非常感谢。

    class my_class
    {
        [CategoryAttribute("Category One")]
        public int property_1
        {
             get { return _property_1; }
             set { _property_1 = value; }
        }

        [CategoryAttribute("Category One")]
        public int property_2
        {
             get { return _property_2; }
             set { _property_2 = value; }
        }
    }

    void ClearCatagory(string category_name)
    {
        CategoryAttribute target_attribute = new CategoryAttribute(category_name);

        Type my_class_type = my_class.GetType();
        PropertyInfo[] prop_info_array = my_class_type.GetProperties();

        foreach (PropertyInfo prop_info in prop_info_array)
        {
            AttributeCollection att_coll = TypeDescriptor.GetAttributes(prop_info);

            CategoryAttribute ca = (CategoryAttribute) att_col[typeof(CategoryAttribute)];

            if (ca.Equals(target_attribute))
            {
                prop_info.SetValue(my_class, 0, null);
            } 
        }
    }

3 个答案:

答案 0 :(得分:2)

使用MemberInfo.GetCustomAttributes实例方法代替TypeDescriptor.GetAttriburtes

电话会是object[] attributes = prop_info.GetCustomAttributes(typeof(CategoryAttriute), false)

或者你可以使用TypeDescriptor.GetProperties而不是Type.GetProperties你不应该在使用反射和TypeDescriptor之间切换。


此外,Category.Equals的{​​{3}}并不完全清楚,但它可能实现了引用相等(这是C#的默认值,除非类专门覆盖它)。这意味着Equals只有在被比较的实例完全相同时才会返回true,而不管Category的值是多少。如果是这种情况,则ca.Equals(target_attribute)将始终为false,因为引用是不同的对象。

尝试比较存储在Category值中的字符串值。字符串实现值相等,以便String.Equals比较存储在stings中的值。

所以替换

if (ca.Equals(target_attribute))

if (ca.Cateogry.Equals(category_name))

答案 1 :(得分:0)

非常感谢shf301解决这个问题。这是代码的工作版本:

    class my_class
    {
        [CategoryAttribute("Category One")]
        public int property_1
        {
                get { return _property_1; }
                set { _property_1 = value; }
        }
    }

    void ClearCatagory(string category_name)
    {
        Type my_class_type = my_class.GetType();
        PropertyInfo[] prop_info_array = my_class_type.GetProperties();

        foreach (PropertyInfo prop_info in prop_info_array)
        {
            CategoryAttribute[] attributes = (CategoryAttribute[]) prop_info.GetCustomAttributes(typeof(CategoryAttribute), false);
            foreach(CategoryAttribute ca in attributes)
            {
                if (ca.Category == category_name)
                {
                    prop_info.SetValue(my_class, 0, null);
                }
            }
        }
    }

答案 2 :(得分:0)

public class cls
{
        [Category("Default")]
        [DisplayName("Street")]
        public string Street { get; set; }
}

foreach (PropertyInfo propinf in cls.GetProperties())
{
  var category = prop.CustomAttributes.Where(x => x.AttributeType ==typeof(CategoryAttribute)).First();
sCategory = category.ConstructorArguments[0].Value.ToString();
}

这就是我们如何获得CustomAttribute

的价值