用dynamic expression运行时访问dynamic object

实验了好久才弄出来,msdn上怎么就不给下示例。。。

            var eo = new System.Dynamic.ExpandoObject();

            dynamic o = eo;

            o.hello = "world";

            var oDynamic = Expression.Lambda<Func<string>>(

                Expression.MakeDynamic(

                    typeof(Func<System.Runtime.CompilerServices.CallSite, object, string>),

                    Microsoft.CSharp.RuntimeBinder.Binder.Convert(0, typeof(string), typeof(Program)),

                    Expression.MakeDynamic(typeof(Func<System.Runtime.CompilerServices.CallSite, object, object>),

                        Microsoft.CSharp.RuntimeBinder.Binder.GetMember(

                            Microsoft.CSharp.RuntimeBinder.CSharpBinderFlags.None,

                            "hello",

                            typeof(Program),

                            new Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo[] { Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create(0, null) }),

                        Expression.Constant(eo)

                    )

                )

            );

            Console.WriteLine(oDynamic.Compile()());

 

你可能感兴趣的:(express)