Ejemplo n.º 1
0
            public MemberInfo VisitConstructor(EmitConstructor constructor, TypeBuilder parent)
            {
                var constructorBuilder = parent.DefineConstructor(
                    ReflectionMethodAttributes.ToMethodAttributes(constructor.Visibility, false, false, false, false, constructor.IsStatic),
                    constructor.IsStatic ? CallingConventions.Standard : CallingConventions.HasThis,
                    constructor.Parameters.Select(x => (Type)x.ParameterType).ToArray());

                return(constructorBuilder);
            }
        public EmitObjectCreationExpression(EmitConstructor constructor, params EmitExpression[] arguments)
        {
            if (constructor.IsStatic)
                throw new ArgumentException("Cannot create an instance using a static constructor", nameof(constructor));

            Type = constructor.DeclaringType;
            Constructor = constructor;
            Arguments.AddRange(arguments);
        }
Ejemplo n.º 3
0
        public void InstantiateStructWithConstructor()
        {
            var method = CreateMethod(block =>
            {
                EmitConstructor constructor = typeof(StructWithConstructor).GetConstructors()[0];
                block.Return(constructor.New("foo"));
            });
            var result = (StructWithConstructor)method.Invoke(null, null);

            Assert.AreEqual("foo", result.StringProperty);
        }
Ejemplo n.º 4
0
        public void InstantiateClass()
        {
            var method = CreateMethod(block =>
            {
                EmitConstructor constructorInfo = typeof(InstanceClass).GetConstructor(new Type[0]);
                block.Return(constructorInfo.New());
            });
            var result = method.Invoke(null, null);

            Assert.IsTrue(result is InstanceClass);
        }
        public EmitObjectCreationExpression(EmitConstructor constructor, params EmitExpression[] arguments)
        {
            if (constructor.IsStatic)
            {
                throw new ArgumentException("Cannot create an instance using a static constructor", nameof(constructor));
            }

            Type        = constructor.DeclaringType;
            Constructor = constructor;
            Arguments.AddRange(arguments);
        }
Ejemplo n.º 6
0
        public void BenchmarkCtorEmit()
        {
            var ctor  = new EmitConstructor(typeof(TestClass));
            var watch = new Stopwatch();

            watch.Reset();
            watch.Start();
            for (int i = 0; i < count; i++)
            {
                testReference = (TestClass)ctor.Create();
            }
            watch.Stop();
            BenchmarkResult.Add("Ctors", "Emit", watch.Elapsed);
        }
Ejemplo n.º 7
0
 public EmitInstruction Emit(IEmitOpCodeConstructor opCode, EmitConstructor constructor)
 {
     var instruction = new EmitInstruction(opCode, constructor);
     AddInstruction(instruction);
     return instruction;
 }
Ejemplo n.º 8
0
 public static EmitObjectCreationExpression New(this EmitConstructor constructor, params EmitExpression[] arguments)
 {
     return(new EmitObjectCreationExpression(constructor, arguments));
 }