public void Returns_PassingInt_ReturnTypeShouldBeInt() { DynamicMethod dm = new DynamicMethodInfo() .Returns(typeof(int)); dm.ReturnType.Should().Be(typeof(int)); }
public void Emit( string propertyName, Type propertyType, Action <DynamicMethodBody> getmethod, Action <DynamicMethodBody> setmethod = null ) { PropertyBuilder property = dynamicTypeInfoField.TypeBuilder.DefineProperty( propertyName, PropertyAttributes.None, propertyType, new Type[] {} ); DynamicMethodInfo getMethodinfo = dynamicTypeInfoField .WithMethod(string.Format("get_{0}", propertyName)) .TurnOnAttributes(MethodAttributes.RTSpecialName) .TurnOnAttributes(MethodAttributes.SpecialName); getmethod(getMethodinfo.Returns(propertyType)); property.SetGetMethod(getMethodinfo.MethodBuilder); if (setmethod != null) { DynamicMethodInfo setMethodinfo = dynamicTypeInfoField .WithMethod(string.Format("set_{0}", propertyName)) .TurnOnAttributes(MethodAttributes.RTSpecialName) .TurnOnAttributes(MethodAttributes.SpecialName) .WithParameter(propertyType, "value"); setmethod(setMethodinfo.Returns(typeof(void))); property.SetSetMethod(setMethodinfo.MethodBuilder); } }
private void ClientProxyMethodDefinition(DynamicMethodInfo clientProxyMethod, MethodInfo interfaceMethodToProxy) { var methodParameters = interfaceMethodToProxy.Parameters(); //TODO : add exception handling instead of rethrow clientProxyMethod.WithParameters(methodParameters) .WithVariable(m_ProxyClientChannelType, SERVICE_CLIENT_VARIABLE_NAME) .WithVariable(interfaceMethodToProxy.ReturnType, METHOD_RESULT_VARIABLE_NAME) .WithVariable(typeof(OperationContext), OPERATION_CONTEXT_VARIABLE_NAME) .WithVariable(typeof(OperationContextScope), OPERATION_CONTEXT_SCOPE_VARIABLE_NAME) .Returns(interfaceMethodToProxy.ReturnType) .Try(/* body*/ m => ClientProxyMethodBody(m, interfaceMethodToProxy) .Ldloc(SERVICE_CLIENT_VARIABLE_NAME) .Call <ClientBase <TServiceInterface> >("Close") .Ldloc(OPERATION_CONTEXT_SCOPE_VARIABLE_NAME) .Call <IDisposable>("Dispose"), /*catches*/ IL.Catch <CommunicationException>(mc => mc.Ldloc(SERVICE_CLIENT_VARIABLE_NAME) .Call <ClientBase <TServiceInterface> >("Abort")), IL.Catch <TimeoutException>(mc => mc.Ldloc(SERVICE_CLIENT_VARIABLE_NAME) .Call <ClientBase <TServiceInterface> >("Abort")), IL.Catch <Exception>(mc => mc.Ldloc(SERVICE_CLIENT_VARIABLE_NAME) .Call <ClientBase <TServiceInterface> >("Abort") .Throw())) .Ldloc(METHOD_RESULT_VARIABLE_NAME) .Ret(); }
public void AsDynamicMethod_WithNotNullDynamicTypeInfo_ThrowsInvalidOperationException() { // arrange var dti = new DynamicTypeInfo("Type"); var dmi = new DynamicMethodInfo(dti, "Name"); // act var result = dmi.AsDynamicMethod; // assert }
internal ExpressionParser( IEnumerable<Token> source, DynamicMethodInfo method ) { Source = source; MethodInfo = method; MethodBody = method.Body; }
private static void MultiplyMethodDefinition(DynamicMethodInfo m) { m.WithParameter(typeof(int), "a") .WithParameter(typeof(int), "b") .Returns(typeof(int)) .Ldarg("a") .Ldarg("b") .Mul() .Ret(); }
public void WithParameter_OneTimePassingInt_DynamicMethodShouldHaveOneParamInt() { // arrange DynamicMethod dm = new DynamicMethodInfo() .WithParameter(typeof(int)); // act // assert dm.GetParameters().First().ParameterType.Should().Be(typeof(int)); }
public void Returns_PassingAny_ShouldReturnSameValueOfBodyProperty() { // arrange var dmi = new DynamicMethodInfo(); var expected = dmi.Body; // act var result = dmi.Returns(typeof(int)); // assert result.Should().Be(expected); }
private static void LoadArgsTo(this MethodDefinition that, DynamicMethodInfo info) { if (!that.IsStatic) { info.WithParameter <int>("$this"); } foreach (var arg in that.Parameters) { info.WithParameter(Type.GetType(arg.ParameterType.FullName), arg.Name); } }
public static DynamicMethodBody NewMethod (Type returnType, params Type[] parameterTypes) { var result = new DynamicMethodInfo(); foreach (Type param in parameterTypes) { result.WithParameter(param); } result.Returns(returnType); return(result.Body); }
public void WithParameter_PassingIntAnsString_DynamicMethodShouldHaveOneParamIntAndAnotherString() { // arrange DynamicMethod dm = new DynamicMethodInfo() .WithParameter(typeof(int)) .WithParameter(typeof(string)); // act // assert dm.GetParameters().Select(p => p.ParameterType) .Should().Have.SameSequenceAs( typeof(int), typeof(string) ); }
public void WithParameter_TwoTimesPassingInt_DynamicMethodShouldHaveOneParamInt() { // arrange DynamicMethod dm = new DynamicMethodInfo() .WithParameter(typeof(int)) .WithParameter(typeof(int)); // act // assert dm.GetParameters().Select(p => p.ParameterType) .Should().Have.SameSequenceAs( typeof(int), typeof(int) ); }
public static DynamicMethodBody InsertBefore (this MethodDefinition that) { var worker = that.Body.GetILProcessor(); var firstInstruction = worker.Body.Instructions[0]; var emitter = new CecilILEmitter( that.Module.Assembly, worker, inst => worker.InsertBefore(firstInstruction, inst)); var dinfo = new DynamicMethodInfo(emitter); that.LoadArgsTo(dinfo); return dinfo.Body; }
public static DynamicMethodBody ReplaceWith (this MethodDefinition that) { var worker = that.Body.GetILProcessor(); var emitter = new CecilILEmitter( that.Module.Assembly, worker, worker.Append ); worker.Body.Instructions.Clear(); var dinfo = new DynamicMethodInfo(emitter); that.LoadArgsTo(dinfo); return dinfo.Body; }
public static DynamicMethodBody InsertBefore (this MethodDefinition that) { var worker = that.Body.GetILProcessor(); var firstInstruction = worker.Body.Instructions[0]; var emitter = new CecilILEmitter( that.Module.Assembly, worker, inst => worker.InsertBefore(firstInstruction, inst)); var dinfo = new DynamicMethodInfo(emitter); that.LoadArgsTo(dinfo); return(dinfo.Body); }
public static DynamicMethodBody InsertBeforeRet (this MethodDefinition that) { var worker = that.Body.GetILProcessor(); var aggregator = new EmittersAggregator(); foreach (var emitter in worker.Body.Instructions.Where(instruction => instruction.OpCode == OpCodes.Ret).Select(instruction1 => new CecilILEmitter( that.Module.Assembly, worker, inst => worker.InsertBefore(instruction1, inst)))) { aggregator.Emitters.Add(emitter); } var dinfo = new DynamicMethodInfo(aggregator); that.LoadArgsTo(dinfo); return dinfo.Body; }
public static DynamicMethodBody ReplaceWith (this MethodDefinition that) { var worker = that.Body.GetILProcessor(); var emitter = new CecilILEmitter( that.Module.Assembly, worker, worker.Append ); worker.Body.Instructions.Clear(); var dinfo = new DynamicMethodInfo(emitter); that.LoadArgsTo(dinfo); return(dinfo.Body); }
public static DynamicMethodBody InsertBeforeRet (this MethodDefinition that) { var worker = that.Body.GetILProcessor(); var aggregator = new EmittersAggregator(); foreach (var emitter in worker.Body.Instructions.Where(instruction => instruction.OpCode == OpCodes.Ret).Select(instruction1 => new CecilILEmitter( that.Module.Assembly, worker, inst => worker.InsertBefore(instruction1, inst)))) { aggregator.Emitters.Add(emitter); } var dinfo = new DynamicMethodInfo(aggregator); that.LoadArgsTo(dinfo); return(dinfo.Body); }
public static DynamicMethodBody NewMethod (this TypeDefinition that, string methodName, MethodAttributes methodAttributes, Type returnType, AssemblyDefinition assembly) { var typeReference = assembly.MainModule.Import(returnType); var method = new MethodDefinition(methodName, methodAttributes, typeReference); var worker = method.Body.GetILProcessor(); var emitter = new CecilILEmitter( assembly, worker, method.Body.Instructions.Add); var dinfo = new DynamicMethodInfo(emitter); method.LoadArgsTo(dinfo); that.Methods.Add(method); return(dinfo.Body); }
public static DynamicMethodBody NewMethod (this TypeDefinition that, string methodName, MethodAttributes methodAttributes, Type returnType, AssemblyDefinition assembly) { var typeReference = assembly.MainModule.Import(returnType); var method = new MethodDefinition(methodName, methodAttributes, typeReference); var worker = method.Body.GetILProcessor(); var emitter = new CecilILEmitter( assembly, worker, method.Body.Instructions.Add); var dinfo = new DynamicMethodInfo(emitter); method.LoadArgsTo(dinfo); that.Methods.Add(method); return dinfo.Body; }
public void SetEntryPoint(DynamicMethodInfo method) { _assemblyBuilder.SetEntryPoint(method.MethodBuilder); }
public void ctor_implicitConvertionToDyamicMethod_ReturnTypeShouldBeVoid() { DynamicMethod dm = new DynamicMethodInfo(); dm.ReturnType.Should().Be(typeof(void)); }
public void ctor_implicitConvertionToDyamicMethod_ShouldHaveNoParameters() { DynamicMethod dm = new DynamicMethodInfo(); dm.GetParameters().Count().Should().Be(0); }
private static void LoadArgsTo(this MethodDefinition that, DynamicMethodInfo info) { if (!that.IsStatic) info.WithParameter<int>("$this"); foreach (var arg in that.Parameters) { info.WithParameter(Type.GetType(arg.ParameterType.FullName), arg.Name); } }
internal DynamicMethodBody(DynamicMethodInfo methodInfo) { methodInfoField = methodInfo; }
public DynamicMethodInfoReturnParameter(DynamicMethodInfo dynamicmethod) { this.dynamicmethod = dynamicmethod; }