Код: Выделить всё
Getter = (Func)Delegate.CreateDelegate(typeof(Func),
propertyInfo.GetGetMethod());
Код: Выделить всё
Setter = (Action)Delegate.CreateDelegate(typeof(Action),
propertyInfo.GetSetMethod());
Я не ищу делегатов для GetValue и SetValue (это означает, что я буду каждый раз вызывать отражение)
Код: Выделить всё
Getter = s => (T)fieldInfo.GetValue(s);
Setter = (s, t) => (T)fieldInfo.SetValue(s, t);
Код: Выделить всё
Getter = (Func)Delegate.CreateDelegate(typeof(Func), fieldInfo.??);
Setter = (Action)Delegate.CreateDelegate(typeof(Action), fieldInfo.??);
Код: Выделить всё
var instExp = Expression.Parameter(typeof(S));
var fieldExp = Expression.Field(instExp, fieldInfo);
Getter = Expression.Lambda(fieldExp, instExp).Compile();
if (!fieldInfo.IsInitOnly)
{
var valueExp = Expression.Parameter(typeof(T));
Setter = Expression.Lambda(Expression.Assign(fieldExp, valueExp), instExp, valueExp).Compile();
}
Подробнее здесь: https://stackoverflow.com/questions/160 ... -fieldinfo
Мобильная версия