C# 如何基于自定义属性从集合中动态创建对象?

C# 如何基于自定义属性从集合中动态创建对象?,c#,C#,我必须根据自定义属性从对象集合创建动态对象 public class Customer { [AccountAttribute(name: "CustomerAccountID")] public int CustomerID { get; set; } [RoleAttribute(name: "RoleUserID")] [AccountAttribute(name: "AccountRole")] public i

我必须根据自定义属性从对象集合创建动态对象

public class Customer
{
    [AccountAttribute(name: "CustomerAccountID")]        
    public int CustomerID { get; set; } 

    [RoleAttribute(name: "RoleUserID")]
    [AccountAttribute(name: "AccountRole")]        
    public int RoleID { get; set; }
}

我有一个客户数据列表

var custData=GetCustomerData()

我想根据属性过滤客户集合中的数据

如果我基于AccountAttribute进行筛选,我希望CustomerID和RoleID的列表,并且在新创建的列表中,属性名称应该是CustomerAccountID,AccountRole

如果基于RoleAttribute进行筛选,则只需要RoleID,字段名应为RoleUserID

上面的类只是一个示例,有20多个字段可用,并且有三个不同的属性


有些字段属于单个属性,但有些字段属于多个属性。

当您在编译时不知道属性名称时,创建动态对象的最佳方法是
ExpandoObject
-它允许您使用
IDictionary
接口访问对象,因此,您只需添加适当的键值对:

private static dynamic CustomerToCustomObject<TAttribute>(Customer customer) 
    where TAttribute : BaseAttribute // assuming the Name property is on a base class for all attributes
{
    dynamic result = new ExpandoObject();
    var dictionary = (IDictionary<string, object>)result;

    var propertiesToInclude = typeof(Customer).GetProperties()
        .Where(property => property.GetCustomAttributes(typeof(TAttribute), false).Any());
    foreach (var property in propertiesToInclude)
    {
        var attribute = (BaseAttribute)(property.GetCustomAttributes(typeof(TAttribute), false).Single());
        dictionary.Add(attribute.Name, property.GetValue(customer));
    }
    return result;
}
私有静态动态客户ocustombject(客户)
其中,tatAttribute:BaseAttribute//假设Name属性位于所有属性的基类上
{
动态结果=新的ExpandooObject();
变量字典=(IDictionary)结果;
var propertiesToInclude=typeof(Customer).GetProperties()
.Where(property=>property.GetCustomAttributes(typeof(tatAttribute),false).Any());
foreach(propertiesToInclude中的var属性)
{
var属性=(BaseAttribute)(property.GetCustomAttributes(typeof(tatAttribute),false).Single());
Add(attribute.Name,property.GetValue(customer));
}
返回结果;
}

在编译时不知道属性名称时创建动态对象的最佳方法是
ExpandoObject
——它允许您使用
IDictionary
接口访问对象,因此您只需添加适当的键值对:

private static dynamic CustomerToCustomObject<TAttribute>(Customer customer) 
    where TAttribute : BaseAttribute // assuming the Name property is on a base class for all attributes
{
    dynamic result = new ExpandoObject();
    var dictionary = (IDictionary<string, object>)result;

    var propertiesToInclude = typeof(Customer).GetProperties()
        .Where(property => property.GetCustomAttributes(typeof(TAttribute), false).Any());
    foreach (var property in propertiesToInclude)
    {
        var attribute = (BaseAttribute)(property.GetCustomAttributes(typeof(TAttribute), false).Single());
        dictionary.Add(attribute.Name, property.GetValue(customer));
    }
    return result;
}
私有静态动态客户ocustombject(客户)
其中,tatAttribute:BaseAttribute//假设Name属性位于所有属性的基类上
{
动态结果=新的ExpandooObject();
变量字典=(IDictionary)结果;
var propertiesToInclude=typeof(Customer).GetProperties()
.Where(property=>property.GetCustomAttributes(typeof(tatAttribute),false).Any());
foreach(propertiesToInclude中的var属性)
{
var属性=(BaseAttribute)(property.GetCustomAttributes(typeof(tatAttribute),false).Single());
Add(attribute.Name,property.GetValue(customer));
}
返回结果;
}

非常感谢。。经过一些小的修改,我就可以使用这段代码了。非常感谢。。经过一些小的修改,我就可以使用这段代码了。