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); }
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); }
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); }
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); }
public EmitInstruction Emit(IEmitOpCodeConstructor opCode, EmitConstructor constructor) { var instruction = new EmitInstruction(opCode, constructor); AddInstruction(instruction); return instruction; }
public static EmitObjectCreationExpression New(this EmitConstructor constructor, params EmitExpression[] arguments) { return(new EmitObjectCreationExpression(constructor, arguments)); }