【C#表达式树 七】 反射在表达式树中的应用 ListInitExpression

以下都是反射在表达式树中的应用

 

对象初始化

 

Expression.MemberInit 反射获取成员(字段 或者属性),绑定数据,然后生成 成员表达式节点

 

 class Animal
        {
            public string Species { get; set; }
            public int Age;
        }
        public static void CreateMemberInitExpression()
        {
            NewExpression newAnimal = Expression.New(typeof(Animal));
            //利用反射获取类的成员信息,保存为反射成员信息 memberinfo
            System.Reflection.MemberInfo speciesMember = typeof(Animal).GetMember("Species")[0];
            System.Reflection.MemberInfo ageMember = typeof(Animal).GetMember("Age")[0];


            //把数据绑定到成员上,相当于赋值   MemberAssignment继承MemberBinding
            MemberBinding speciesMemberBinding = Expression.Bind(speciesMember, Expression.Constant("horse"));
           MemberAssignment ageMemberBinding = Expression.Bind(ageMember, Expression.Constant(12));


            // 表示一个表达式,该表达式初始化该对象的属性
            MemberInitExpression memberInitExpression = Expression.MemberInit(newAnimal,new[] { speciesMemberBinding, ageMemberBinding });
            //也可以这样写,后面参数都默认为数组  MemberInitExpression memberInitExpression = Expression.MemberInit(Expression.New(typeof(Animal)), speciesMemberBinding, ageMemberBinding );
            Console.WriteLine(memberInitExpression.ToString());

            // This code produces the following output:
            //
            // new Animal() {Species = "horse", Age = 12}
        }
        static void Main(string[] args)
        {

            CreateMemberInitExpression();

    }

 

 集合类初始化

 Expression.ListInit、Expression.ElementInit

new List<string>() {
    "sam",
    "korn"
}

代码如下

//创建一个ListInitExpression表示集合初始化器
//示例:
string proner1 = "sam";
string proner2 = "korn";

MethodInfo add = typeof ( List<string> ).GetMethod ( "Add" );
//生成表达式:Add("sam")
var elm1 = Expression.ElementInit (add ,Expression.Constant ( proner1 ));
//生成表达式:Add("korn")
var elm2 = Expression.ElementInit (add ,Expression.Constant ( proner2 ));
//生成表达式:new List<string>( )
var list = Expression.New ( typeof ( List<string> ) );
//生成表达式:new List<string> { "sam" , "korn" }
var listObject = Expression.ListInit (list , elm1 , elm2);

Console.WriteLine(listObject.ToString() );

 

Expression.ElementInit (add ,Expression.Constant ( proner2 ));  元素初始化 第一方法一定是Add,这个方法只能用于集合类

属性

par.Name == "xiaoli"

以上写成表达树节点式代码如下:

 static void Main(string[] args)
        {
            ParameterExpression par = Expression.Parameter(typeof(People),"par");
            MemberExpression property = Expression.Property(par, "Name");
            BinaryExpression binary = Expression.Assign(property, Expression.Constant("xiaoming"));
            BinaryExpression eqb = Expression.Equal(property,Expression.Constant("xiaoli"));

        }

        public class People
        {
            public int Age { get; set; }
            public string Name { get; set; }

            public int Id;

        }
posted @ 2021-10-23 18:21  小林野夫  阅读(254)  评论(0编辑  收藏  举报
原文链接:https://www.cnblogs.com/cdaniu/