Get property name and type by pass it directly

前端 未结 1 1400
温柔的废话
温柔的废话 2021-01-16 13:13

I asked similar questions here And here

This is a sample Type:

    public class Product {

    public string Name { get; set; }
    public string Ti         


        
1条回答
  •  执笔经年
    2021-01-16 13:38

    You can use expression tree, than your code will look like this

    GT.SelectedProperties.Add(p=>p.Title);
    GT.SelectedProperties.Add(p=>p.IsAllowed);
    

    You will need to create custom collection class derived from List for SelectedProperties and create add method like this

       //where T is the type of your class
       public string Add(Expression> expression)
       {
            var body = expression.Body as MemberExpression;
            if (body == null) 
                throw new ArgumentException("'expression' should be a member expression");
            //Call List Add method with property name
            Add(body.Member.Name);
       }
    

    Hope this helps

    0 讨论(0)
提交回复
热议问题