基础 class 中表达式树的字段 getter/setter

Field getter/setter with expression tree in base class

按照 this post and its follow-up question 上的示例,我正在尝试使用编译表达式创建字段 getters / setters。

getter 工作得很好,但我被 setter 困住了,因为我需要 setter 来分配任何类型的字段。

这是我的 setter-动作生成器:

public static Action<T1, T2> GetFieldSetter<T1, T2>(this FieldInfo fieldInfo) {
  if (typeof(T1) != fieldInfo.DeclaringType && !typeof(T1).IsSubclassOf(fieldInfo.DeclaringType)) {
    throw new ArgumentException();
  }
  ParameterExpression targetExp = Expression.Parameter(typeof(T1), "target");
  ParameterExpression valueExp = Expression.Parameter(typeof(T2), "value");
  //
  // Expression.Property can be used here as well
  MemberExpression fieldExp = Expression.Field(targetExp, fieldInfo);
  BinaryExpression assignExp = Expression.Assign(fieldExp, valueExp);
  //
  return Expression.Lambda<Action<T1, T2>> (assignExp, targetExp, valueExp).Compile();
}

现在,我将通用 setter 存储到缓存列表中(当然,因为每次构建 setter 都是性能杀手),我将它们转换为简单的 "objects":

 // initialization of the setters dictionary
 Dictionary<string, object> setters = new Dictionary(string, object)();
 Dictionary<string, FieldInfo> fldInfos = new Dictionary(string, FieldInfo)();
 FieldInfo f = this.GetType().GetField("my_int_field");
 setters.Add(f.Name, GetFieldSetter<object, int>(f); 
 fldInfos.Add(f.Name, f); 
 //
 f = this.GetType().GetField("my_string_field");
 setters.Add(f.Name, GetFieldSetter<object, string>(f); 
 fldInfos.Add(f.Name, f); 

现在我尝试像这样设置一个字段值:

 void setFieldValue(string fieldName, object value) {
      var setterAction = setters[fieldName];
      // TODO: now the problem => how do I invoke "setterAction" with 
      // object and fldInfos[fieldName] as parameters...?
 }

我可以简单地调用一个泛型方法并每次都进行转换,但我担心性能开销...有什么建议吗?

-- 编辑后的答案 基于,我创建了一个小测试程序来比较直接设置值,缓存反射(缓存FieldInfo的地方)和缓存多类型代码。我使用最多 3 级继承 (ObjectC : ObjectB : ObjectA) 的对象继承。

Full code is of the example can be found here.

测试的单次迭代给出以下输出:

-------------------------
---      OBJECT A     ---
-------------------------
  Set direct:       0.0036 ms
  Set reflection:   2.319 ms
  Set ref.Emit:     1.8186 ms
  Set Accessor:     4.3622 ms

-------------------------
---      OBJECT B     ---
-------------------------
  Set direct:       0.0004 ms
  Set reflection:   0.1179 ms
  Set ref.Emit:     1.2197 ms
  Set Accessor:     2.8819 ms

-------------------------
---      OBJECT C     ---
-------------------------
  Set direct:       0.0024 ms
  Set reflection:   0.1106 ms
  Set ref.Emit:     1.1577 ms
  Set Accessor:     2.9451 ms

当然,这只是显示了创建对象的成本 - 这使我们能够测量创建反射和表达式缓存版本的偏移量。

接下来,让我们 运行 1.000.000 次:

-------------------------
---      OBJECT A     ---
-------------------------
  Set direct:       33.2744 ms
  Set reflection:   1259.9551 ms
  Set ref.Emit:     531.0168 ms
  Set Accessor:     505.5682 ms

-------------------------
---      OBJECT B     ---
-------------------------
  Set direct:       38.7921 ms
  Set reflection:   2584.2972 ms
  Set ref.Emit:     971.773 ms
  Set Accessor:     901.7656 ms

-------------------------
---      OBJECT C     ---
-------------------------
  Set direct:       40.3942 ms
  Set reflection:   3796.3436 ms
  Set ref.Emit:     1510.1819 ms
  Set Accessor:     1469.4459 ms

为了完整起见:我删除了对 "set" 方法的调用以突出显示获取 setter 的成本(FieldInfo 对于反射方法,Action<object, object> 用于表达式大小写)。结果如下:

-------------------------
---      OBJECT A     ---
-------------------------
  Set direct:       3.6849 ms
  Set reflection:   44.5447 ms
  Set ref.Emit:     47.1925 ms
  Set Accessor:     49.2954 ms


-------------------------
---      OBJECT B     ---
-------------------------
  Set direct:       4.1016 ms
  Set reflection:   76.6444 ms
  Set ref.Emit:     79.4697 ms
  Set Accessor:     83.3695 ms

-------------------------
---      OBJECT C     ---
-------------------------
  Set direct:       4.2907 ms
  Set reflection:   128.5679 ms
  Set ref.Emit:     126.6639 ms
  Set Accessor:     132.5919 ms

注意:这里的时间增加并不是因为较大的词典的访问时间较慢(因为它们有 O(1) 访问时间),而是因为我们访问它的次数增加了([=22= 每次迭代 4 次,ObjectB 每次迭代 8 次,ObjectC 每次迭代 12 次)...如您所见,只有创建偏移量在这里有所不同(这是预期)。

底线:我们确实将性能提高了 2 倍或更多,但我们距离直接字段集的性能还很远......在列表中检索正确的 setter 代表了一个好的10% 的时间。

我会尝试用表达式树代替 Reflection.Emit 看看我们是否可以进一步缩小差距...欢迎任何评论。

编辑 2 我按照 Eli Arbel on .

使用通用 "Accessor" class 的方法添加了结果

如果你希望它支持对多种类型的操作,你的函数缓存应该由 Type 和字段名 (string) 索引,并且应该延迟创建函数。试试这个:

private static Dictionary<Type, Dictionary<string, Action<object, object>>> _typeMapper = new Dictionary<Type, Dictionary<string, Action<object, object>>>();

public static void Set(object obj, string fieldName, object newValue)
{
    if (obj == null)
    {
        throw new ArgumentNullException("obj");
    }
    Type type = obj.GetType();
    Dictionary<string, Action<object, object>> fieldMapper;
    Action<object, object> action;
    if (_typeMapper.TryGetValue(type, out fieldMapper))
    {
        // entry has been created for this type.
        if (!fieldMapper.TryGetValue(fieldName, out action))
        {
            // method has not been created yet, must build it.
            FieldInfo fld = type.GetField(fieldName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            if (fld == null)
            {
                throw new ArgumentException("No field " + fieldName);
            }
            action = buildSetter(fld);
            fieldMapper.Add(fieldName, action); // add it to method cache for future use.
        }
    }
    else
    {
        // -- ADDED CODE: forgot to create the new fieldMapper.....
        fieldMapper = new Dictionary<string, Action<object, object>>();

     // type has not been added yet, so we know method has not been built yet either.
        FieldInfo fld = type.GetField(fieldName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
        if (fld == null)
        {
            throw new ArgumentException("No field " + fieldName);
        }
        action = buildSetter(fld);
        fieldMapper.Add(fieldName, action); // add it to method cache for future use.
        _typeMapper.Add(type, fieldMapper); // add it to type cache for future use.
    }
    action(obj, newValue); // invoke the method.
}
// this is my preferred setter-builder, feel free to use expressions instead.
private static Action<object, object> buildSetter(FieldInfo fld)
{
    DynamicMethod dyn = new DynamicMethod("set_" + fld, typeof(void), new[] { typeof(object), typeof(object) }, fld.DeclaringType);
    ILGenerator gen = dyn.GetILGenerator();
    gen.Emit(OpCodes.Ldarg_0);
    gen.Emit(OpCodes.Castclass, fld.DeclaringType);
    gen.Emit(OpCodes.Ldarg_1);
    if (fld.FieldType.IsClass)
    {
        gen.Emit(OpCodes.Castclass, fld.FieldType);
    }
    else
    {
        gen.Emit(OpCodes.Unbox_Any, fld.FieldType);
    }
    gen.Emit(OpCodes.Stfld, fld);
    gen.Emit(OpCodes.Ret);
    return (Action<object, object>)dyn.CreateDelegate(typeof(Action<object, object>));
}

否则,如果您只需要对一种类型执行此操作,您的过程将变为:

private static Dictionary<string, Action<MyType, object>> _mapper = new Dictionary<string, Action<MyType, object>>();

public static void Set(MyType obj, string fieldName, object newValue)
{
    if (obj == null)
    {
        throw new ArgumentNullException("obj");
    }
    Action<MyType, object> action;
    if (!_mapper.TryGetValue(fieldName, out action))
    {
        FieldInfo fld = typeof(MyType).GetField(fieldName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
        if (fld == null)
        {
            throw new ArgumentException("No field " + fieldName);
        }
        action = buildSetter(fld);
        _mapper.Add(fieldName, action);
    }
    action(obj, newValue); // invoke the method.
}

private static Action<MyType, object> buildSetter(FieldInfo fld)
{
    DynamicMethod dyn = new DynamicMethod("set_" + fld, typeof(void), new[] { typeof(MyType), typeof(object) }, typeof(MyType));
    ILGenerator gen = dyn.GetILGenerator();
    gen.Emit(OpCodes.Ldarg_0);
    gen.Emit(OpCodes.Ldarg_1);
    if (fld.FieldType.IsClass)
    {
        gen.Emit(OpCodes.Castclass, fld.FieldType);
    }
    else
    {
        gen.Emit(OpCodes.Unbox_Any, fld.FieldType);
    }
    gen.Emit(OpCodes.Stfld, fld);
    gen.Emit(OpCodes.Ret);
    return (Action<MyType, object>)dyn.CreateDelegate(typeof(Action<MyType, object>));
}