Accessing C# property name or attributes

前端 未结 7 2131
花落未央
花落未央 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:58

    Just wrote an implementation of this for a presentation on lambdas for our usergroup last Tuesday.

    • You can do

      MembersOf.GetName(x => x.Status)

    • Or

      var a = new Animal() a.MemberName(x => x.Status)

    the code:

    public static class MembersOf {
        public static string GetName(Expression> expr) {
            var node = expr.Body as MemberExpression;
            if (object.ReferenceEquals(null, node)) 
                throw new InvalidOperationException("Expression must be of member access");
            return node.Member.Name;
        }
    }
    
    • Link to the presentation and code samples.
    • Also in SVN (more likely to be updated): http://gim-projects.googlecode.com/svn/presentations/CantDanceTheLambda

提交回复
热议问题