我正在阅读Kinect,我发现了这段代码:
using System;
using System.ComponentModel;
using System.Linq.Expressions;
namespace Kinect.Toolbox {
public abstract class Notifier: INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged < T > (Expression < Func < T >> propertyExpression) {
var memberExpression = propertyExpression.Body as MemberExpression;
if (memberExpression == null)
return;
string propertyName = memberExpression.Member.Name;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
我能够理解一点,但我想知道Generics如何在这里工作:
protected void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression)
答案 0 :(得分:0)
Linq Expression是值得关注的。
提供从中派生表示表达式树节点的类的基类。
This is a good resource for combining the generics with expression, it goes through an example
它是类型委托的参数('Func'是。它也可以使用'Action'或'Predicate',因为它们是一种委托)和Func&lt; T>表示您的代理人将返回一种T。