-1

I was using this code in a library project using the Framework .NET 4.5 and it works fine

protected void OnPropertyChanged<TProperty>(Expression<Func<TProperty>> property)
{
    if (this.IsInpcActive)
    {
        this.OnPropertyChanged(property.GetMemberInfo().Name);
    }
}

I copy-pasted this code in a project using the .NET Frameworkwith 4.5.2 and I receive this error message:

'System.Linq.Expressions.Expression<System.Func<TProperty>>' does not contain a definition for 'GetMemberInfo' and no extension method 'GetMemberInfo' accepting a first argument of type 'System.Linq.Expressions.Expression<System.Func<TProperty>>' could be found (are you missing a using directive or an assembly reference?

As I don't really want to loose too much time, I found a plan B: extension method:

internal static class ExpressionExtensions
{
    #region Methods

    public static MemberInfo GetMemberInfo(this Expression expression)
    {
        var lambda = (LambdaExpression)expression;

        MemberExpression memberExpression;
        if (lambda.Body is UnaryExpression)
        {
            var unaryExpression = (UnaryExpression)lambda.Body;
            memberExpression = (MemberExpression)unaryExpression.Operand;
        }
        else
            memberExpression = (MemberExpression)lambda.Body;

        return memberExpression.Member;
    }

    #endregion Methods
}

But I'm cusious: where did this method go?

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
JiBéDoublevé
  • 4,124
  • 4
  • 36
  • 57
  • 2
    Possible duplicate of [Where to find GetMemberInfo](http://stackoverflow.com/questions/20955547/where-to-find-getmemberinfo) – NikolaiDante Jan 21 '16 at 12:48
  • @Nikolai no, that's where OP got this code. – CodeCaster Jan 21 '16 at 12:49
  • 1
    @CodeCaster actually it is a duplicate - there is no such method so a new one must be created. It's just happened that the OP included the duplicate's answer in the question – Panagiotis Kanavos Jan 21 '16 at 12:54
  • @PanagiotisKanavos _"there is no such method so a new one must be created"_ - and that is not **explained** in that other question, so that is not a duplicate. Sure, it contains code that when copy-pasted solves OP's _problem_, but that is not their _question_, as they found that solution themselves already. – CodeCaster Jan 21 '16 at 12:55

1 Answers1

1

The Expression<TDelegate> class has no method GetMemberInfo(), and it never had. So your problem seems to lie here:

I copy-pasted this code in a project

So to me it looks like your version compiled against 4.5 used the same or an equivalent extension method, otherwise it wouldn't have compiled.

See for example this one from the Enterprise Library:

StaticReflection.GetMemberInfo<T, TProperty> Method

Retrieves a PropertyInfo object for the set method from an expression in the form x => x.SomeProperty.

So your original code should have this at the top:

using Microsoft.Practices.EnterpriseLibrary.Common.Utility;

And a reference to the Enterprise Library.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272