给出以下简单的控制台应用程序,该应用程序说明了两种通知已更改属性的方法:
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var person = new Person(){ Name = "Me" };
person.Age = 20;
person.Weight = 80.5F;
person.RandomProperty = new RandomComplexObject();
Console.ReadKey();
}
}
public class Person : BaseObject
{
public string Name
{
get { return _name; }
set { SetProperty(ref value, ref _name, false); }
}
public int Age
{
get { return _age; }
set { SetProperty<int>(ref value, ref _age, true, "Age", "Weight"); }
}
public float Weight
{
get { return _weight; }
set { SetProperty(ref value, ref _weight, true, () => Weight, () => Age); }
}
public RandomComplexObject RandomProperty
{
get { return _rco; }
//*** the following line has the error:
//-------------------------------------
set { SetProperty(ref value, ref _rco, true, () => Name, () => Age, () => Weight); }
}
private float _weight;
private int _age;
private string _name;
private RandomComplexObject _rco;
}
public class BaseObject : INotifyPropertyChanged
{
protected void OnPropertyChanged<T>(Expression<Func<T>> propertyExpression)
{
var handler = PropertyChanged;
if (handler != null)
{
var body = propertyExpression.Body as MemberExpression;
var expression = body.Expression as ConstantExpression;
handler(expression.Value, new PropertyChangedEventArgs(body.Member.Name));
}
}
private void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler == null)
return;
handler(this, new PropertyChangedEventArgs(propertyName));
}
protected bool SetProperty<T>(ref T newValue, ref T currentValue, bool notify, params string[] notifications)
{
if (EqualityComparer<T>.Default.Equals(newValue, currentValue))
return false;
currentValue = newValue;
if (notify)
foreach (var propertyName in notifications)
OnPropertyChanged(propertyName);
return true;
}
protected bool SetProperty<T, TProperty>(ref T newValue, ref T currentValue, bool notify, params Expression<Func<TProperty>>[] notifications)
{
if (EqualityComparer<T>.Default.Equals(newValue, currentValue))
return false;
currentValue = newValue;
if (notify)
foreach (var notification in notifications)
OnPropertyChanged(notification);
return true;
}
public event PropertyChangedEventHandler PropertyChanged;
}
public class RandomComplexObject{}
}
在方法调用SetProperty(ref value, ref _rco, true, () => Name, () => Age, () => Weight);
的行上我遇到了编译错误:
无法将lambda表达式转换为'string'类型,因为它不是委托类型
IDE中直接显示的错误是:
无法从用法推断出方法'bool ConsoleApplication1.BaseObject.SetProperty(ref T,ref T,bool,params Expression&lt; Func&lt; TProperty&gt;&gt; [])'的类型参数。尝试明确指定类型参数。
如何消除对SetProperty()
方法的此调用的歧义?是否有一种语法上更清晰的方式来写这个?
答案 0 :(得分:2)
以下变体至少编译。
public RandomComplexObject RandomProperty
{
get { return _rco; }
set
{
SetProperty(
ref value,
ref _rco,
true,
() => Name,
() => Age.ToString(), //instead of () => Age
() => Weight.ToString());//instead of () => Weight
}
}
我猜测,你收到的错误首先是因为编译器无法推断TProperty
protected bool SetProperty<T, TProperty>(
ref T newValue,
ref T currentValue,
bool notify,
params Expression<Func<TProperty>>[] notifications)
{
//...
}
因为它期望可变数量的Expression<Func<TProperty>>
类型的参数,你传递了lambdas,返回string
,int
和float
。绝对是编译器无法确定哪一个是TProperty
。
在Weight
属性的setter中:
public float Weight
{
get { return _weight; }
set
{
SetProperty(ref value, ref _weight, true, () => Weight, () => Age);
}
}
Weight
类型为float
且Age
类型为int
,编译推断TProperty
为float
,因为存在隐式转换从int
到float
。