Accessing C# property name or attributes

前端 未结 7 2129
花落未央
花落未央 2021-01-31 21:39

I would like to automatically generate SQL statements from a class instance. The method should look like Update(object[] Properties, object PrimaryKeyProperty). The method is pa

7条回答
  •  盖世英雄少女心
    2021-01-31 21:55

    I found a perfect solution in This Post

    public static string GetPropertyName(Expression> propertyExpression)
    {
       return (propertyExpression.Body as MemberExpression).Member.Name;
    }
    

    And then for the usage :

    var propertyName = GetPropertyName(
    () => myObject.AProperty); // returns "AProperty"
    

    Works like a charm

提交回复
热议问题