【问题标题】:Reflection Performance - Create Delegate (Properties C#) With Conditional Parameters反射性能 - 使用条件参数创建委托(C# 属性)
【发布时间】:2016-04-22 08:39:59
【问题描述】:

我正在使用问题的解决方案:Reflection Performance - Create Delegate (Properties C#)

我有以下代码:

private static Action<object, object> BuildSetAccessor(string name, MethodInfo method)
{
    if (method == null) return null;
    if (method.DeclaringType == null) return null;
    var obj = Expression.Parameter(typeof(object), name);
    var value = Expression.Parameter(typeof(object));
    var expr = Expression.Lambda<Action<object, object>>(Expression.Call(Expression.Convert(obj, method.DeclaringType), method, Expression.Convert(value, method.GetParameters()[0].ParameterType)), obj, value);
    return expr.Compile();
}

private static Func<object, object> BuildGetAccessor(string name, MethodInfo method)
{
    if (method.DeclaringType == null) return null;
    var obj = Expression.Parameter(typeof(object), name);
    var expr = Expression.Lambda<Func<object, object>>(Expression.Convert(Expression.Call(Expression.Convert(obj, method.DeclaringType), method), typeof(object)), obj);
    return expr.Compile();
}

正在使用的代码...

var cacheProperty = new CacheForReflectionClassProperty()
                {
                    Name = p.Name,
                    SetValue = BuildSetAccessor(p.Name, p.GetSetMethod()),
                    GetValue = BuildGetAccessor(p.Name, p.GetGetMethod())
                };
if (Attribute.IsDefined(p, typeof(IsIdentityColumn)))
{
    // TODO: Instead of just getting the old value, 
    // I need to verify if the value is NULL
    // in that case I need to create a new value of ID type....
    // and at the end I need to use the SetValue() method as well
    GetValue = .... // TODO: 
}
else if (Attribute.IsDefined(p, typeof(IsCreatedOnColumn)))
                {
    // TODO: Instead of just getting the old value, 
    // I need to verify if the value is NULL
    // in that case I need to create a new value with DateTime.UtcNow
    // and at the end I need to use the SetValue() method as well
    GetValue = .... // TODO:
                }

我知道我需要创建以下方法:

private static Func<object, object> BuildGetAccessorForIdentityColumn(string name, MethodInfo method)

private static Func<object, object> BuildGetAccessorForCreatedOnColumn(string name, MethodInfo method)

我曾尝试学习如何使用表达式树,但我还没有找到一种方法,我的 BuildGetAccessor 不仅可以获取值,还可以将结果与某些东西进行比较,如果需要还需要使用 BuildSetAccessor。

我该怎么做?

【问题讨论】:

    标签: c# reflection setvalue


    【解决方案1】:

    您这样做是因为反射会影响性能。但是在你完成反射部分之后——你不再需要使用编译表达式——只需使用常规代码:

        var getAccessor = BuildGetAccessor(p.Name, p.GetGetMethod());
        var setAccessor = BuildSetAccessor(p.Name, p.GetSetMethod());
        // if IsCreatedOnColumn
        cacheProperty.GetValue = (instance) =>
        {
            var value = getAccessor(instance);
            if (value == null)
            {
                value = DateTime.UtcNow;
                setAccessor(instance, value);
            }
            return value;
        };
    

    【讨论】:

    • 今天我有时间回到这个问题上,使用你的代码我能够做我想做的事。谢谢:)(我给你投票和正确答案)。
    猜你喜欢
    • 2012-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-10
    • 1970-01-01
    • 1970-01-01
    • 2011-03-02
    相关资源
    最近更新 更多