使用反射更新列表中的值

For*_*ore 3 .net c# reflection list

我有一个具有 100 多个属性的对象。然后我有一个包含很多这些对象的列表。我需要计算列表中所有属性的最小值、最大值、中值和中值。

所以不要写:

Ag = _valuesForExtraCalculations.Min(c => c.Ag),
Al = _valuesForExtraCalculations.Min(c => c.Al),
Alkal = _valuesForExtraCalculations.Min(c => c.Alkal),
Run Code Online (Sandbox Code Playgroud)

一百次,我以为我可以使用反射,所以我写道:

var Obj = new Analysis();
Type t = Obj.GetType();
PropertyInfo[] prop = t.GetProperties();
foreach (var propertyInfo in prop)
{
    propertyInfo = _valuesForExtraCalculations.Min(?????);
}
Run Code Online (Sandbox Code Playgroud)

但我不确定要在foreach循环中写入什么,因此我可以将该属性的新最小值设置为我创建的新 Analysis 对象。

Den*_*nis 5

据我理解你的问题,你可以使用表达式来实现这一点:

/// <summary>
/// A class with many-many properties
/// </summary>
class MyClass
{
    public Decimal A { get; set; }
    public Decimal B { get; set; }
    public Decimal C { get; set; }
}

class PropertyHelper<T, TProperty>
{
    private readonly Func<T, TProperty> selector;
    private readonly Action<T, TProperty> setter;

    public PropertyHelper(Func<T, TProperty> selector, Action<T, TProperty> setter)
    {
        this.selector = selector;
        this.setter = setter;
    }

    public Func<T, TProperty> Selector
    {
        get { return selector; }
    }

    public Action<T, TProperty> Setter
    {
        get { return setter; }
    }
}

class AggregateHelper<T, TProperty>
{
    private readonly Dictionary<PropertyInfo, PropertyHelper<T, TProperty>> helpers;

    public AggregateHelper()
    {
        this.helpers = typeof(T)
            .GetProperties()
            .Where(p => p.PropertyType == typeof(TProperty))
            .ToDictionary(p => p, p => new PropertyHelper<T, TProperty>(MakeSelector(p), MakeSetter(p)));
    }

    private Func<T, TProperty> MakeSelector(PropertyInfo property)
    {
        var parameterExpr = Expression.Parameter(typeof(T));
        var lambda = (Expression<Func<T, TProperty>>)Expression.Lambda(
            Expression.Property(parameterExpr, property), parameterExpr);

        return lambda.Compile();
    }

    private Action<T, TProperty> MakeSetter(PropertyInfo property)
    {
        var instanceExpr = Expression.Parameter(typeof(T));
        var parameterValueExpr = Expression.Parameter(typeof(TProperty));
        var lambda = (Expression<Action<T, TProperty>>)Expression.Lambda(
            Expression.Call(instanceExpr, property.GetSetMethod(), parameterValueExpr),
            instanceExpr,
            parameterValueExpr);

        return lambda.Compile();
    }

    public IEnumerable<PropertyInfo> Properties
    {
        get { return helpers.Keys; }
    }

    public PropertyHelper<T, TProperty> this[PropertyInfo property]
    {
        get { return helpers[property]; }
    }
}
Run Code Online (Sandbox Code Playgroud)

用法:

    public static void Do()
    {
        var target = new MyClass();
        var list = new List<MyClass>
        {
            new MyClass { A = 1M, B = 2M, C = 3M },
            new MyClass { A = 10M, B = 20M, C = 30M },
            new MyClass { A = 100M, B = 200M, C = 300M }
        };

        // calculate 'min' for all decimal properties
        var helper = new AggregateHelper<MyClass, Decimal>();

        foreach (var property in helper.Properties)
        {
            var propertyHelper = helper[property];

            propertyHelper.Setter(target, list.Min(propertyHelper.Selector));
        }
    }
Run Code Online (Sandbox Code Playgroud)

已编译的 lambda 运行速度比反射更快,并且不会出现装箱/拆箱。