在基类

时间:2016-07-22 13:07:20

标签: c# reflection expression-trees setter fieldinfo

按照this postits follow-up question上的示例,我尝试使用编译表达式创建字段getter / setter。

getter的工作效果很好,但是我遇到了setter,因为我需要setter来分配任何类型的字段。

这是我的setter-action builder:

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都是性能杀手),我将它们转换为简单的“对象”:

 // 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...?
 }

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

- 已编辑的答案 基于Mr Anderson's answer,我创建了一个小测试程序,它直接比较值,缓存反射(其中缓存了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>用于表达式case。结果如下:

-------------------------
---      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)访问时间),而是由于我们访问它的次数增加了(ObjectA每次迭代4次,ObjectB为8次,ObjectC为12次)......正如人们所看到的,只有创建偏移在这里有所不同(这是预期的)

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

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

编辑2 我使用Eli Arbel this post上{{3}}建议使用通用“访问者”类的方法添加了结果。

1 个答案:

答案 0 :(得分:1)

如果您希望它支持多种类型的操作,您的函数缓存应该由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>));
}