I have found some really interesting code for wiring up IPropertyChanged, To see the article click here.
My problem is after I have converted it to VB.Net I am receiving a compiler error in OnPropertyChanged method on the last line - OnPropertyChanged(body.Member.Name)
Data type(s) of the type parameter(s) in method 'Protected Overridable Sub OnPropertyChanged(Of T)(selectorExpression As System.Linq.Expressions.Expression(Of System.Func(Of T)))' cannot be inferred from these arguments. Specifying the data type(s) explicitly might correct this error.
Can someone help me correct the code to remove the error please?
Original C# Version:
protected virtual void OnPropertyChanged<T>(Expression<Func<T>> selectorExpression)
{
if (selectorExpression == null)
throw new ArgumentNullException("selectorExpression");
MemberExpression body = selectorExpression.Body as MemberExpression;
if (body == null)
throw new ArgumentException("The body must be a member expression");
OnPropertyChanged(body.Member.Name);
}
protected bool SetField<T>(ref T field, T value, Expression<Func<T>> selectorExpression)
{
if (EqualityComparer<T>.Default.Equals(field, value)) return false;
field = value;
OnPropertyChanged(selectorExpression);
return true;
}
After it has been converted to VB.Net
Protected Overridable Sub OnPropertyChanged(Of T)(selectorExpression As Expression(Of Func(Of T)))
If selectorExpression Is Nothing Then
Throw New ArgumentNullException("selectorExpression")
End If
Dim body As MemberExpression = TryCast(selectorExpression.Body, MemberExpression)
If body Is Nothing Then
Throw New ArgumentException("The body must be a member expression")
End If
OnPropertyChanged(body.Member.Name)//====this is the line with the compiler error======
End Sub
Protected Function SetField(Of T)(ByRef field As T, value As T, selectorExpression As Expression(Of Func(Of T))) As Boolean
If EqualityComparer(Of T).[Default].Equals(field, value) Then
Return False
End If
field = value
OnPropertyChanged(selectorExpression)
Return True
End Function