This project has moved and is read-only. For the latest updates, please go here.
1

Closed

Possible NullReferenceException in RaisePropertyChanged

description

This code
            var body = propertyExpression.Body as MemberExpression;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(body.Member.Name));
            }

            if (broadcast)
            {
                Broadcast(oldValue, newValue, body.Member.Name);
            }
doesn't test for body being null.

Prism does a more robust extraction of the property name, along the lines of
    public static string ExtractPropertyName(LambdaExpression propertyExpression)
    {
        if (propertyExpression == null)
        {
            throw new ArgumentNullException("propertyExpression");
        }

        var memberExpression = propertyExpression.Body as MemberExpression;
        if (memberExpression == null)
        {
            throw new ArgumentException(@"Not a member expression", "propertyExpression");
        }

        var property = memberExpression.Member as PropertyInfo;
        if (property == null)
        {
            throw new ArgumentException(@"Not a property", "propertyExpression");
        }

        var getMethod = property.GetGetMethod(true);
        if (getMethod.IsStatic)
        {
            throw new ArgumentException(@"Can't be static", "propertyExpression");
        }

        return memberExpression.Member.Name;
    }
Closed Oct 13, 2014 at 1:56 PM by lbugnion

comments

lbugnion wrote Oct 23, 2011 at 3:54 PM

Hi,
Yep good one. I will handle that in V4 beta 2.
Cheers,
Laurent