实体中的相同类别属性与 C# 中的字典

Same Category attributes in an entity to a Dictionary in C#

I have an entity witch has some properties. i have used category attribute to categorize similar properties together.`

       [System.ComponentModel.Category("cat_name1")]
       public string propname1 { get; set; }
       [System.ComponentModel.Category("cat_name2")]
       public string propname2 { get; set; }

I want to get these properties to a dictionary object key as category and value a property list

Dictionary<string, List<PropertyInfo>> variable_name= TypeDescriptor.GetProperties(new entityClass())
.Cast<PropertyDescriptor>()
    .ToDictionary(s =>s.Category,TypeDescriptor.GetProperti[enter image description here][1]es(new entityClass())
    .Cast<PropertyDescriptor()
    .GroupBy(p=>p.Category).ToList<PropertyInfo>());

im getting this error

这是一种可以创建 Dictionary<string, List<string> 的方法。键将是类别名称,值将是字符串列表(如果您有两个或更多属性具有相同的类别)。

EntityClass entityClass = new EntityClass();
entityClass.propname1 = "abc";
entityClass.propname2 = "def";

System.ComponentModel.PropertyDescriptorCollection pdc = System.ComponentModel.TypeDescriptor.GetProperties(entityClass);
var dict = pdc.OfType<PropertyDescriptor>().GroupBy(x => x.Category).ToDictionary(x => x.Key, x => x.Select(p => p.GetValue(entityClass).ToString()).ToList());

编辑: 我不确定这是否是你想要的结果,因为问题很糟糕format/structure。下次请尽量解释清楚一点..

This is what i have done..

Dictionary<string, Dictionary<string, AttributeCollection>> testingSectionFields =
                TypeDescriptor.GetProperties(new PP_MainDataEntity())
                .OfType<PropertyDescriptor>().ToList()
                .GroupBy(cat => cat.Category)
                .ToDictionary(c => c.Key, c1 => c1.ToDictionary(fld => fld.Name, fld1 => fld1.Attributes));

wich retreives entity properties -> group them by category attribute -> get all attributes specific to that property :)