private void LoadOwner(MethodGenerator g)
 {
     if (this.OwnerType.IsValueType)
     {
         g.LoadVariable(SerializationArgs.InstanceName, LoadOptions.ValueAsAddress);
     }
     else
     {
         g.LoadVariable(SerializationArgs.InstanceName);
     }
 }
Exemple #2
0
        private static PropertyValueMethod GenerateGetPropertyValueMethod <T>(Type objectType, string propertyName)
        {
            DynamicMethod method = new DynamicMethod(string.Format("GetPropertyValue_{0}", Guid.NewGuid().ToString("n"))
                                                     , typeof(T)
                                                     , new Type[] { typeof(object) }
                                                     , true);

            MethodGenerator g = new MethodGenerator(method);

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

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

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

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

            return(new PropertyValueMethod(propertyName, invoke));
        }
        public static void ReadElement(MethodGenerator g, string dtoMemberName, Type elementType, IVariable index)
        {
            var method   = typeof(IDTOReader).ResolveMethod("ReadElement", new Type[] { elementType }, MethodParameter.Create <string>(), MethodParameter.Create <int>());
            var prmIndex = SerializationArgs.ReaderIndex;

            g.Call(method, () =>
            {
                g.LoadParameter(prmIndex);
                g.Load(dtoMemberName);
                g.LoadVariable(index, LoadOptions.Default);
            });
        }
        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));
        }
        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));
        }