Use the attribute name and value of the Expression tree response class, expressiontree
Expression Tree Expression is an important function in Linq. It is helpful to have a deep understanding of Lamda and computation expressions.
The following describes how to use Expression <Func <Object> [] to obtain the operands, member names, and values in Func <Object>.
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Linq.Expressions;using System.Reflection;namespace ExpressionTree{ class Program { static void Main(string[] args) { MyClass cls = new MyClass() { Memo = "ffffddddd", Name = "dfdf", Age = 33 }; Dictionary<string, string> dic = GetProps(() => cls.Memo, () => cls.Age); foreach (KeyValuePair<string,string> item in dic) { Console.WriteLine(item.Key + "=" + item.Value); } Console.Read(); } static Dictionary<string, string> GetProps(params Expression<Func<Object>>[] funcs) { Dictionary<string, string> dic = new Dictionary<string, string>(); MemberExpression member = null; UnaryExpression unary = null; foreach (Expression<Func<Object>> func in funcs) { unary = func.Body as UnaryExpression; if (unary != null) { member = unary.Operand as MemberExpression; } else { member = func.Body as MemberExpression; } PropertyInfo prop = member.Member as PropertyInfo; object value = func.Compile().Invoke(); dic.Add(prop.Name, Convert.ToString(value)); } return dic; } } class MyClass { public string Name { get; set; } public int Age { get; set; } public string Memo { get; set; } }}