Exemple #1
0
        private static PropertyValueMethod GenerateGetPropertyValueMethod(Type objectType, string propertyName)
        {
            DynamicMethod method = new DynamicMethod(string.Format("GetPropertyValue_{0}", Guid.NewGuid().ToString("n"))
                                                     , typeof(object)
                                                     , new Type[] { typeof(object) }
                                                     , true);

            MethodGenerator g = new MethodGenerator(method);

            var result = g.Declare <object>("result");

            g.Assign(result, () =>
            {
                g.LoadParameter(0);
                g.Cast(objectType);
                g.LoadMember(propertyName);
                g.Cast(typeof(object));
            });

            g.LoadVariable("result");
            g.Return();

            var invoke = method.CreateDelegate(typeof(Func <object, object>));

            return(new PropertyValueMethod(propertyName, invoke));
        }
Exemple #2
0
        private static PropertyValueMethod GenerateSetPropertyValueMethod(Type objectType, string propertyName)
        {
            var propertyInfo = objectType.ResolveProperty(propertyName);
            var propertyType = propertyInfo.PropertyType;

            DynamicMethod method = new DynamicMethod(string.Format("SetPropertyValue_{0}", Guid.NewGuid().ToString("n"))
                                                     , null
                                                     , new Type[] { typeof(object), typeof(object) } //obj  value
                                                     , true);

            MethodGenerator g = new MethodGenerator(method);

            g.LoadParameter(0);
            g.Cast(objectType);
            g.Assign(propertyName, () =>
            {
                g.LoadParameter(1);
                g.Cast(propertyType);
            });
            g.Return();

            var invoke = method.CreateDelegate(typeof(Action <object, object>));

            return(new PropertyValueMethod(propertyName, invoke));
        }
        private static CreateInstanceMethod GenerateCreateInstanceMethod(ConstructorInfo constructor)
        {
            var           objectType = constructor.DeclaringType;
            DynamicMethod method     = new DynamicMethod(string.Format("CreateInstanceByConstructor_{0}", Guid.NewGuid().ToString("n"))
                                                         , typeof(object)
                                                         , new Type[] { typeof(object[]) }
                                                         , true);

            MethodGenerator g = new MethodGenerator(method);
            //以下代码把数组参数转成,new T(arg0,arg1)的形式
            var result = g.Declare(objectType, "result");
            var objs   = g.Declare <object[]>();

            g.Assign(objs, () =>
            {
                g.LoadParameter(0);
            });

            g.Assign(result, () =>
            {
                g.NewObject(constructor, () =>
                {
                    var index = g.Declare <int>();
                    var prms  = constructor.GetParameters();
                    for (var i = 0; i < prms.Length; i++)
                    {
                        g.Assign(index, () =>
                        {
                            g.Load(i);
                        });

                        g.LoadElement(objs, index);
                        g.Cast(prms[i].ParameterType);
                    }
                });
            });

            g.LoadVariable("result");
            g.Cast(typeof(object));
            g.Return();

            var invoke = method.CreateDelegate(typeof(Func <object[], object>));

            return(new CreateInstanceMethod(invoke));
        }
Exemple #4
0
        public void IntToObject()
        {
            DynamicMethod   method = new DynamicMethod("temp", typeof(object), Type.EmptyTypes);
            MethodGenerator g      = new MethodGenerator(method);

            g.Load(1);
            g.Cast(typeof(object));
            g.Return();
            object result = (object)method.Invoke(null, new object[] { });

            Assert.AreEqual(1, result);
        }
        private static CreateInstanceMethod GenerateCreateInstanceMethod(Type objectType)
        {
            DynamicMethod method = new DynamicMethod(string.Format("CreateInstance_{0}", Guid.NewGuid().ToString("n"))
                                                     , typeof(object)
                                                     , Array.Empty <Type>()
                                                     , true);

            MethodGenerator g = new MethodGenerator(method);

            var result = g.Declare <object>("result");

            g.Assign(result, () =>
            {
                g.NewObject(objectType);
                g.Cast(typeof(object));
            });

            g.LoadVariable("result");
            g.Return();

            var invoke = method.CreateDelegate(typeof(Func <object>));

            return(new CreateInstanceMethod(invoke));
        }