Retrieving Property name from lambda expression

前端 未结 21 1732
迷失自我
迷失自我 2020-11-21 11:12

Is there a better way to get the Property name when passed in via a lambda expression? Here is what i currently have.

eg.

GetSortingInfo         


        
21条回答
  •  你的背包
    2020-11-21 12:01

    I created an extension method on ObjectStateEntry to be able to flag properties (of Entity Framework POCO classes) as modified in a type safe manner, since the default method only accepts a string. Here's my way of getting the name from the property:

    public static void SetModifiedProperty(this System.Data.Objects.ObjectStateEntry state, Expression> action)
    {
        var body = (MemberExpression)action.Body;
        string propertyName = body.Member.Name;
    
        state.SetModifiedProperty(propertyName);
    }
    

提交回复
热议问题