Search code examples
c#wpfkinectkinect-sdk

Generics inside the function parameters in Kinect app


i was reading about Kinect and i found this code :

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));
  }
 }
}

i was able to understand a little bit but i want to know how the Generics work in here:

protected void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression)


Solution

  • Linq Expression is something to look at.

    Provides the base class from which the classes that represent expression tree nodes are derived.

    This is a good resource for combining the generics with expression, it goes through an example

    It's an argument of type delegate(which 'Func' is. It could also use 'Action' or 'Predicate' because they are a type of delegate) and Func< T > means your delegate will return a type of T.