1

我目前在我的程序中使用FieldInfo.GetValueFieldInfo.SetValue很多,这大大减慢了我的程序。

因为PropertyInfo我使用GetValueGetterandGetValueSetter方法,所以我只对给定类型使用一次反射。对于FieldInfo,方法不存在。

建议的方法是FieldInfo什么?

编辑:我从CodeCaster 的回复中关注了这个有用的链接。这是一个很好的搜索方向。

现在我在这个答案中没有得到的“唯一”点是我如何缓存getter / setter并以通用方式重新使用它们,只使用字段的名称 - 这基本上就是SetValue正在做的事情

// generate the cache
Dictionary<string, object> setters = new Dictionary<string, object>();
Type t = this.GetType();
foreach (FieldInfo fld in t.GetFields()) {
     MethodInfo method = t.GetMethod("CreateSetter");
     MethodInfo genericMethod = method.MakeGenericMethod( new Type[] {this.GetType(), fld.FieldType});
     setters.Add(fld.Name, genericMethod.Invoke(null, new[] {fld}));
}
// now how would I use these setters?
setters[fld.Name].Invoke(this, new object[] {newValue}); // => doesn't work without cast....
4

1 回答 1

1

您可以使用Expressions 生成更快的字段访问器。如果您希望它们工作而不是字段的具体类型,则必须在表达式Object中添加强制转换 ( )。Convert

using System.Linq.Expressions;

class FieldAccessor
{
    private static readonly ParameterExpression fieldParameter = Expression.Parameter(typeof(object));
    private static readonly ParameterExpression ownerParameter = Expression.Parameter(typeof(object));

    public FieldAccessor(Type type, string fieldName)
    {
        var field = type.GetField(fieldName,
            BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
        if (field == null) throw new ArgumentException();

        var fieldExpression = Expression.Field(
            Expression.Convert(ownerParameter, type), field);

        Get = Expression.Lambda<Func<object, object>>(
            Expression.Convert(fieldExpression, typeof(object)),
            ownerParameter).Compile();

        Set = Expression.Lambda<Action<object, object>>(
            Expression.Assign(fieldExpression,
                Expression.Convert(fieldParameter, field.FieldType)), 
            ownerParameter, fieldParameter).Compile();
    }

    public Func<object, object> Get { get; }

    public Action<object, object> Set { get; }
}

用法:

class M
{
    private string s;
}

var accessors = new Dictionary<string, FieldAccessor>();

// expensive - you should do this only once
accessors.Add("s", new FieldAccessor(typeof(M), "s"));

var m = new M();
accessors["s"].Set(m, "Foo");
var value = accessors["s"].Get(m);
于 2016-07-23T05:07:51.983 回答