我有以下代码。但是,它在SetValue
上有一个运行时异常。什么可能导致错误?
var _filter = new Filter(....); // Filter implemented IFilter
ApplyFilter(_view.Name, x => x.Name);
private void ApplyFilter<T>(T curr, Expression<Func<IFilter, T>> prev)
{
var expr = (MemberExpression)prev.Body;
var prop = (PropertyInfo)expr.Member;
if (!EqualityComparer<T>.Default.Equals(curr, (T)_filter[prop.Name]))
{
prop.SetValue(_filter, curr, null); // Error
..... // do something on _filter
例外是:
<>之前系统。ArgumentException未处理消息=未找到属性设置方法。源= mscorlib加:在System.Reflection.RuntimePropertyInfo。SetValue(Object obj, Object value, BindingFlags, invokeAttr, Binder, Binder, Object[] index, CultureInfo, culture)在System.Reflection.RuntimePropertyInfo。SetValue(对象obj,对象值,对象[]索引)在MyApp.ErrorLogPresenter。ApplyFilter[T](T curr, Expression ' 1 prev) in d:....cs:第50行Message=Property set method not found.
这通常仅仅意味着您使用的属性没有定义setter。要么确保存在合适的setter,要么使用不同的方法赋值