public SetValueOnObjectAction(GeneratedMethod method, GeneratedVariable variable, object value, MethodInfo info) { this.method = method; this.variable = variable; this.value = value; this.info = info; }
public void Should_Be_Able_To_Add_A_Method_And_Instantiate_A_Type_And_Invoke_A_Method() { Type generatedType = generator.CreateType(type => { type.Named("TestType8"); type.AddMethod(method => { method.Named("TestMethod"); method.Returns(typeof(void)); method.CreateArgument <string>(); method.WithBody(body => { GeneratedVariable variable = body.CreateVariable <Processor>(); GeneratedVariable returnValue = body.CreateVariable <string>(); variable.AssignFrom(body.Instantiate <Processor>()); returnValue.AssignFrom(() => variable.Invoke <Processor>(processor => processor.Process(null, null))); body.Return(returnValue); }); }); }); Assert.IsNotNull(generatedType.GetMethod("TestMethod")); Assert.AreEqual(1, generatedType.GetMethod("TestMethod").GetParameters().Length); generatedType.GetMethod("TestMethod").Invoke(Activator.CreateInstance(generatedType), new[] { "yay" }); }
public void Should_Be_Able_To_Add_An_Override_Method_And_Instantiate_A_Type_And_Invoke_A_Method() { Type generatedType = generator.CreateType(type => { type.Named("TestType7"); type.InheritFrom <BaseType>(); type.OverrideMethod <BaseType>(baseType => baseType.DoSomething(null), method => { method.WithBody(body => { GeneratedVariable variable = body.CreateVariable <Processor>(); GeneratedVariable returnValue = body.CreateVariable <string>(); variable.AssignFrom(body.Instantiate <Processor>()); returnValue.AssignFrom(() => variable.Invoke <Processor>(processor => processor.Process(null, null))); GeneratedVariable baseValue = body.CreateVariable <string>(); baseValue.AssignFrom(() => body.CallBase(method.Method)); body.Return(baseValue); }); }); }); Assert.IsNotNull(generatedType.GetMethod("DoSomething")); Assert.AreEqual(1, generatedType.GetMethod("DoSomething").GetParameters().Length); Assert.AreEqual("yay", generatedType.GetMethod("DoSomething").Invoke(Activator.CreateInstance(generatedType), new[] { "yay" })); }
public SetValueAtIndexAction(GeneratedMethod method, GeneratedArray array, GeneratedVariable variable, int index) { this.method = method; this.array = array; this.variable = variable; this.index = index; }
public void Intercept(MethodInfo methodInfo, object attribute, GeneratedVariable processor, GeneratedVariable variable, GeneratedVariable encapsulating) { var defaultAttribute = attribute as IDefaultProcessEncapsulatingAttribute; var funcProcessor = GetMethodInfo(() => defaultAttribute.Process<object>(null), methodInfo.ReturnType); if(processor != null) variable.AssignFrom(() => encapsulating.Invoke(funcProcessor, processor)); else variable.AssignFrom(() => encapsulating.Invoke(funcProcessor)); }
public void Intercept(MethodInfo methodInfo, object attribute, GeneratedVariable processor, GeneratedVariable variable, GeneratedVariable encapsulating) { var defaultAttribute = attribute as IDefaultProcessEncapsulatingActionAttribute; var funcProcessor = GetMethodInfo(() => defaultAttribute.Process(null)); if(processor != null) encapsulating.Invoke(funcProcessor, processor); else encapsulating.Invoke(funcProcessor); }
private GeneratedVariable GenerateEncapsulatedCalls(MethodInfo methodInfo, MethodBodyContext body, GeneratedVariable serviceLocator, GeneratedField field) { var attributes = methodInfo.GetCustomAttributes(typeof(IProcessEncapsulatingAttribute), true); GeneratedVariable variable = null; if (attributes.Length == 0) { if (methodInfo.ReturnType == typeof(void)) { body.CallBase(methodInfo); } else { variable = body.CreateVariable(methodInfo.ReturnType); variable.AssignFrom(() => body.CallBase(methodInfo)); } return(variable); } var encapsulating = body.CreateVariable <IProcessEncapsulatingAttribute>(); if (useServiceLocator) { encapsulating.AssignFrom(() => serviceLocator.Invoke(typeof(Microsoft.Practices.ServiceLocation.IServiceLocator).GetMethod("GetInstance", new Type[0]).MakeGenericMethod(attributes[0].GetType()))); } else { encapsulating.AssignFrom(body.Instantiate(attributes[0].GetType())); } MethodInfo target = null; var lambdaVariable = body.CreateLambda(lambda => { target = lambda.Target(methodInfo); RecursivelyGenerateCalls(attributes, 1, lambda, methodInfo, field); }); var func = lambdaVariable.CreateFunc(target); if (methodInfo.ReturnType != typeof(void)) { variable = body.CreateVariable(methodInfo.ReturnType); new DefaultProcessEncapsulatingInterceptionStrategy().Intercept(methodInfo, attributes[0], func, variable, encapsulating); } else { new DefaultProcessEncapsulatingActionInterceptionStrategy().Intercept(methodInfo, attributes[0], func, null, encapsulating); } return(variable); }
public void Intercept(MethodInfo methodInfo, object attribute, GeneratedVariable processor, GeneratedVariable variable, GeneratedVariable encapsulating) { var defaultAttribute = attribute as IDefaultProcessEncapsulatingAttribute; var funcProcessor = GetMethodInfo(() => defaultAttribute.Process <object>(null), methodInfo.ReturnType); if (processor != null) { variable.AssignFrom(() => encapsulating.Invoke(funcProcessor, processor)); } else { variable.AssignFrom(() => encapsulating.Invoke(funcProcessor)); } }
public void Intercept(MethodInfo methodInfo, object attribute, GeneratedVariable processor, GeneratedVariable variable, GeneratedVariable encapsulating) { var defaultAttribute = attribute as IDefaultProcessEncapsulatingActionAttribute; var funcProcessor = GetMethodInfo(() => defaultAttribute.Process(null)); if (processor != null) { encapsulating.Invoke(funcProcessor, processor); } else { encapsulating.Invoke(funcProcessor); } }
private void ProxyMethods(BaseTypeGenerationContext type, Type typeToProxy, GeneratedField field) { var methods = typeToProxy.GetMethods(BindingFlags.Public | BindingFlags.Instance); for (int i = 0; i < methods.Length; i++) { var methodInfo = methods[i]; if (methodInfo.IsVirtual && methodInfo.GetBaseDefinition().DeclaringType != typeof(object)) { type.OverrideMethod(methodInfo, method => method.WithBody(body => { if (methodInfo.GetCustomAttributes(typeof(IAopAttribute), true).Length == 0) { body.CallBase(methodInfo); return; } GeneratedVariable locator = null; if (useServiceLocator) { locator = body.CreateVariable <Microsoft.Practices.ServiceLocation.IServiceLocator>(); locator.AssignFrom(field); } GeneratePreProcessors(body, methodInfo, locator); var returnValue = GenerateEncapsulatedCalls(methodInfo, body, locator, field); GeneratePostProcessors(body, methodInfo, locator); if (returnValue != null) { body.Return(returnValue); } })); } } }
public void Intercept(GeneratedVariable postProcessor) { postProcessor.Invoke <IDefaultPostProcessingAttribute>(processor => processor.Process()); }
public void Intercept(GeneratedVariable preProcessor) { preProcessor.Invoke<IDefaultPreProcessingAttribute>(processor => processor.Process()); }
public void Target(GeneratedVariable variable, MethodInfo method) { actions.Add(new LoadVariableFunctionAction(() => this, variable.LocalIndex, method)); }
public Type CreateDelegate(GeneratedVariable variable, Func<MethodBuilderBundle> info, Type returnType) { this.actions.Add(new LoadVariableFunctionAction(() => this, variable.LocalIndex, info)); var action = new CreateDelegateAction(returnType); this.actions.Add(action); return action.DelegateType; }
public Type CreateDelegate(GeneratedVariable variable, MethodInfo info) { this.actions.Add(new LoadVariableFunctionAction(() => this, variable.LocalIndex, info)); var action = new CreateDelegateAction(info.ReturnType); this.actions.Add(action); return action.DelegateType; }
private void GeneratePostProcessors(MethodBodyContext body, ICustomAttributeProvider methodInfo, GeneratedVariable serviceLocator) { var attributes = methodInfo.GetCustomAttributes(typeof(IPostProcessingAttribute), true); for (int i = 0; i < attributes.Length; i++) { var attribute = attributes[i]; var postProcessor = body.CreateVariable <IPostProcessingAttribute>(); if (useServiceLocator) { postProcessor.AssignFrom(() => serviceLocator.Invoke(typeof(Microsoft.Practices.ServiceLocation.IServiceLocator).GetMethod("GetInstance", new Type[0]).MakeGenericMethod(attribute.GetType()))); } else { postProcessor.AssignFrom(body.Instantiate(attribute.GetType())); } postProcessor.Invoke <IDefaultPostProcessingAttribute>(processor => processor.Process()); } }
public void SetValueAtIndex(GeneratedVariable variable, int index) { actions.Add(new SetValueAtIndexAction(method, this, variable, index)); }