C# 类别属性和属性信息

C# 类别属性和属性信息,c#,C#,我有一个包含数百个属性的类。每个属性都是用[CategoryAttribute(“MyCategory Name”)]属性子句声明的,因此它将很好地显示在PropertyGrid中。我想使用这个CategoryAttribute属性来设置类中标记有特定CategoryAttribute类别的所有属性的值。下面的代码编译并运行,但它没有完成任务,因为att_coll不包含我所期望的CategoryAttribute属性。有人知道怎么做吗?非常感谢 class my_class {

我有一个包含数百个属性的类。每个属性都是用[CategoryAttribute(“MyCategory Name”)]属性子句声明的,因此它将很好地显示在PropertyGrid中。我想使用这个CategoryAttribute属性来设置类中标记有特定CategoryAttribute类别的所有属性的值。下面的代码编译并运行,但它没有完成任务,因为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);
            } 
        }
    }
使用实例方法而不是
TypeDescriptor.getAttributes

调用将是
object[]attributes=prop\u info.GetCustomAttributes(typeof(categoryattribute),false)

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


另外,for
Category.Equals的定义并不明确,但很可能它实现了引用相等(这是C#的默认值,除非有类专门重写它)。这意味着
Equals
只有在被比较的实例完全相同时才会返回true,而不管
Category
的值是多少。如果是这种情况,那么
ca.Equals(target_属性)
将始终为false,因为引用是不同的对象

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

因此,替换

if (ca.Equals(target_attribute))


非常感谢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);
                }
            }
        }
    }

这就是我们如何获得CustomAttribute的值的方法

也许我没有得到它(或者您总结了代码中的实际内容),但似乎您没有正确实现属性的使用。在尝试这种复杂程度之前,请确保您已经完成了。您的权利在于,此代码实际上并没有像您通常使用的那样使用属性。我试图使用Refection并扫描类中的所有属性,并根据CategoryAttribute对它们进行更改。但是,我的问题是我不知道如何访问CategoryAttributes。谢谢。很高兴知道。我的代码实际上在Equals()之前失败。我正在尝试访问以前分配给我的_类中属性的CategoryAttribute。我最好的猜测是,可以从我的_class_type.GetProperties()返回的PropertyInfo对象中提取信息。然而,它并不存在。所以我在寻找另一种方法来找到它。
    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);
                }
            }
        }
    }
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();
}