Exemplo n.º 1
0
        private Action <TInstance, object[]> GetPropertySetter(PropertyInfo[] properties, ITypeProvider provider)
        {
            var type = typeof(TInstance);

            var methodName = string.Concat(
                "D$PropertySetter$",
                type.FullName.Replace(".", "_"),
                "$",
                Guid.NewGuid());

            var method = new DynamicMethod(methodName, typeof(void), new[] { type, typeof(object[]) });

            var il       = method.GetILGenerator();
            var instance = ILPointer.Arg(0, type);
            var props    = ILPointer.Arg(1, typeof(object[]));

            for (var i = 0; i < properties.Length; i++)
            {
                var property = properties[i];
                var value    = props
                               .ElementAt(i)
                               .Cast(property.PropertyType);

                il.Set(instance, property, value);
            }
            il.Emit(OpCodes.Ret);

            return((Action <TInstance, object[]>)
                   method.CreateDelegate(typeof(Action <TInstance, object[]>)));
        }
Exemplo n.º 2
0
        private void BuildReadMethods(SerializableType target, TravellerContext context)
        {
            var typedMethodBuilder = _travelReadMethod;
            var readBuilder        = new DynamicReadTravellerBuilder(typedMethodBuilder, target, context, _typeProvider.Provider);

            readBuilder.BuildTravelReadMethod();

            var untypedMethodBuilder = _classBuilder.DefineOverloadMethod("Travel", typeof(void), new[] { typeof(IReadVisitor), typeof(object) });
            var il = untypedMethodBuilder.IL;

            il.InvokeMethod(ILPointer.This(),
                            typedMethodBuilder.Method,
                            ILPointer.Arg(1, typeof(IReadVisitor)),
                            ILPointer.Arg(2, typeof(object)).Cast(target.Type));

            il.Emit(OpCodes.Ret);
        }
Exemplo n.º 3
0
        public void IterateList()
        {
            var mt   = new DynamicMethod("Test$" + Guid.NewGuid(), typeof(string), new [] { typeof(List <string>) });
            var il   = mt.GetILGenerator();
            var list = ILPointer.Arg(0, typeof(List <string>));

            var concatMethod = typeof(string).GetMethod("Concat", new[] { typeof(object), typeof(object) });
            var res          = il.NewLocal(typeof(string));

            il.Enumerate(list, cur => il.Set(res, ILSnippet.Call(concatMethod, res, cur)));
            il.Load(res);
            il.Emit(OpCodes.Ret);
            var m = (Func <List <string>, string>)mt.CreateDelegate(typeof(Func <List <string>, string>));

            Assert.Equal("HelloWorld", m.Invoke(new List <string> {
                "Hello", "World"
            }));
        }
Exemplo n.º 4
0
        public DynamicActivator(ConstructorInfo constructor, ITypeProvider provider)
        {
            if (constructor == null)
            {
                throw new ArgumentNullException(nameof(constructor));
            }
            if (provider == null)
            {
                throw new ArgumentNullException(nameof(provider));
            }
            Type = constructor.DeclaringType;

            var methodName = string.Concat(
                "D$Activator$",
                Type.FullName.Replace(".", "_"),
                "$",
                constructor.GetParameters().Length,
                Guid.NewGuid());
            var method = new DynamicMethod(methodName, typeof(object), DynConstructorTypes);

            var il           = method.GetILGenerator();
            var methodParams = ILPointer.Arg(0, typeof(object[]));

            var parameters = constructor.GetParameters();

            var constructParameters = new ILPointer[parameters.Length];

            for (var i = 0; i < parameters.Length; i++)
            {
                var parameter      = parameters[i];
                var constructParam = methodParams
                                     .ElementAt(i)
                                     .Cast(parameter.ParameterType);

                constructParameters[i] = constructParam;
            }

            il.Construct(constructor, constructParameters);
            il.Emit(OpCodes.Ret);

            _activate = (Func <object[], object>)
                        method.CreateDelegate(typeof(Func <object[], object>));
        }