Example #1
0
        public static LambdaExpression CreateNewExpr(ParameterExpression Pr, Dictionary <string, string> Dict, object ParamsObject)
        {
            ParamsObject = ParamsObject ?? new { };
            var dict     = new Dictionary <string, Type>();
            var argsDict = new Dictionary <string, Expression>();

            foreach (var x in Dict)
            {
                var FX = x.Value.FixBracket().FixSpace().ToFxExpression();
                var fx = ExprBuilders.ToExpression(Pr, FX, ParamsObject);
                argsDict.Add(x.Key, fx);
                dict.Add(x.Key, fx.Type);
            }
            var t           = CreateNewType(dict);
            var args        = new List <Expression>();
            var outputParam = Expression.Parameter(t, "n");
            var mbxs        = new List <MemberInfo>();

            foreach (var x in argsDict)
            {
                //var mx = Expression.MakeMemberAccess(outputParam, t.GetProperty(x.Key));
                //var ax = Expression.Assign(mx, x.Value);
                args.Add(x.Value);
                mbxs.Add(t.GetProperty(x.Key));
            }
            var nx = Expression.New(t.GetConstructors()[0],
                                    args.ToArray(),
                                    mbxs.ToArray()
                                    );

            return(Expression.Lambda(nx, Pr));
        }
Example #2
0
        public static void Test20210205()
        {
            Expression <Func <MyClass, object> > px = p => new {
                MyCode = p.Code
            };
            var dict = new Dictionary <string, string>();

            dict.Add("MyCode", "Code");
            dict.Add("MyName", "Name");
            var Pr = Expression.Parameter(typeof(MyClass), "p");
            Expression <Func <MyClass, dynamic> > LD = ExprBuilders.CreateSelector <MyClass>("Code MyCode,Name MyName", new { });

            var lst = new List <MyClass>();

            lst.Add(new MyClass
            {
                Code = "123"
            });

            lst.Add(new MyClass
            {
                Code = "1234"
            });
            var ret = lst.Select(LD.Compile()).ToList();

            foreach (var x in ret)
            {
                Console.WriteLine(x.MyCode);
            }
        }