基于特性名称数组创建对象的子集

本文关键字:创建对象 子集 数组 于特性 | 更新日期: 2023-09-27 17:59:20

我有一个类和一个属性名称数组,定义如下:

public class Dog {
    public string Name { get; set; }
    public string Breed { get; set; }
    public int Age { get; set; }
}
var desiredProperties = new [] {"Name", "Breed"};

我还有一个返回dog对象列表的方法:

List<Dog> dogs = GetAllDogs();

有没有一种方法可以返回仅包含desiredProperties数组中定义的属性的dogs的子集?最终,这个结果列表将被序列化为JSON。

考虑到用户将被允许指定任何属性组合(假设它们都是有效的)作为数组中的输出,我已经为这个问题挣扎了一段时间。更多示例:

var desiredProperties = new [] {"Name", "Age"};
// Sample output, when serialized to JSON:
// [
//   { Name: "Max", Age: 5 },
//   { Name: "Spot", Age: 2 }
// ]
var desiredProperties = new [] {"Breed", "Age"};
// [
//   { Breed: "Scottish Terrier", Age: 5 },
//   { Breed: "Cairn Terrier", Age: 2 }
// ]

基于特性名称数组创建对象的子集

您可以编写一个函数来实现这一点。使用下面的扩展方法。

public static class Extensions
{
    public static object GetPropertyValue(this object obj, string propertyName)
    {
        return obj.GetType().GetProperty(propertyName).GetValue(obj);
    }
    public static List<Dictionary<string, object>> FilterProperties<T>(this IEnumerable<T> input, IEnumerable<string> properties)
    {
        return input.Select(x =>
        {
            var d = new Dictionary<string, object>();
            foreach (var p in properties)
            {
                d[p] = x.GetPropertyValue(p);
            }
            return d;
        }).ToList();
    }
}

像一样测试

var dogs = GetAllDogs();
var f1 = dogs.FilterProperties(new[]
{
    "Name", "Age"
});
var f2 = dogs.FilterProperties(new[]
{
    "Breed", "Age"
});
Console.WriteLine(JsonConvert.SerializeObject(f1));
Console.WriteLine(JsonConvert.SerializeObject(f2));

结果是

[{"Name":"Spot","Age":2},{"Name":"Max","Age":5}]
[{"品种":"凯恩梗","年龄":2},{"种类":"苏格兰梗",《年龄》:5}]

我不知道这是否是最有效的方法,但这是一种的方法:

var list = new List<Dog>();
list.Add(new Dog {Name = "Max", Breed = "Bull Terrier", Age = 5});
list.Add(new Dog {Name = "Woofie", Breed = "Collie", Age = 3});
var desiredProperties = new[] {"Name", "Breed"};
var exportDogs = new List<Dictionary<string, object>>();
foreach(var dog in list)
{
    var exportDog = new Dictionary<string, object>();
    foreach(var property in desiredProperties)
    {
        exportDog[property] = dog.GetType().GetProperty(property).GetValue(dog, null);
    }
    exportDogs.Add(exportDog);
}
var output = JsonConvert.SerializeObject(exportDogs);

输出将如下所示:

[{"Name":"Max","Breed":"Bull Terrier"},{"Name":"Woofie","Breed":"Collie"}]

然而,如果不需要动态访问属性,那么最好这样做:

var output = list.Select(dog => new {dog.Name, dog.Breed});

然后只是序列化输出。

类似的东西。。。未测试。。。

    var desiredProperties = new [] {"Name", "Breed"};
    var lst = (from asm in AppDomain.CurrentDomain.GetAssemblies()
                        from asmTyp in asm.GetTypes()
                        where typeof(dog).IsAssignableFrom(asmTyp) && desiredProperties.All(p=> PropertyExists(asmTyp, p))
                        select asmTyp).ToArray();
    private bool PropertyExists(Type dogType, string name)
    {
        bool ret=true;
        try{ dogType.GetProperty(name);}
        catch{ret=false};
        return ret;
    }