/// <summary> /// /// </summary> /// <param name="il"></param> /// <param name="paramAttr"></param> /// <param name="parameterType"></param> public override void EmitParameterResolution(ILGenerator il, ParameterAttribute paramAttr, Type parameterType) { ProviderDependencyAttribute attr = (ProviderDependencyAttribute) paramAttr; MethodInfo getHeadOfChain = GetPropertyGetter<IBuilderContext>("HeadOfChain", typeof (IBuilderStrategy)); MethodInfo buildUp = GetMethodInfo<IBuilderStrategy>("BuildUp", typeof (IBuilderContext), typeof (Type), typeof (object), typeof (string)); PropertyInfo prop = attr.ProviderHostType.GetProperty(attr.ProviderGetterProperty, BindingFlags.Static | BindingFlags.Public); if (prop == null) { throw new ArgumentException(); } MethodInfo propInvoker = prop.GetGetMethod(); if (propInvoker == null) { throw new ArgumentException(); } Guid.NewGuid(); MethodInfo newGuidMethod = typeof (Guid).GetMethod("NewGuid"); MethodInfo guidToStringMethod = typeof (Guid).GetMethod("ToString", new Type[] {}); if ((newGuidMethod == null) || (guidToStringMethod == null)) { throw new ArgumentException(); } //object value (declaration) LocalBuilder valueIndex = il.DeclareLocal(typeof (object)); //object value = prop.GetGetMethod().Invoke(attr.ProviderHostType, null); //value = propInvoker.Invoke(null) (return value remains in the stack) il.EmitCall(OpCodes.Call, propInvoker, null); il.Emit(OpCodes.Stloc, valueIndex); //string id = Guid.NewGuid().ToString(); //il.Emit(OpCodes.Ldtoken, typeof(Guid)); //il.EmitCall(OpCodes.Call, newGuidMethod, null); //il.EmitCall(OpCodes.Call, guidToStringMethod, null); //il.Emit(OpCodes.Stloc, idIndex); // Get the head of the context chain il.Emit(OpCodes.Ldarg_0); // Get context onto the stack il.EmitCall(OpCodes.Callvirt, getHeadOfChain, null); // Now head of chain is on the stack // Build up parameters to the BuildUp call - context, type, existing, id il.Emit(OpCodes.Ldarg_0); // Push context onto stack EmitLoadType(il, parameterType); // Existing object is value il.Emit(OpCodes.Ldloc, valueIndex); // And the id //il.Emit(OpCodes.Ldloc,idIndex); il.Emit(OpCodes.Ldarg_3); // Call buildup on head of the chain il.EmitCall(OpCodes.Callvirt, buildUp, null); }
private IEnumerable <IParameter> GenerateIParametersFromParameterInfos(ParameterInfo[] parameterInfos) { List <IParameter> result = new List <IParameter>(); foreach (ParameterInfo parameterInfo in parameterInfos) { ParameterAttribute attribute = GetInjectionAttribute(parameterInfo); result.Add(attribute.CreateParameter(parameterInfo.ParameterType)); } return(result); }
/// <summary> /// /// </summary> /// <param name="il"></param> /// <param name="paramAttr"></param> /// <param name="parameterType"></param> public override void EmitParameterResolution(ILGenerator il, ParameterAttribute paramAttr, Type parameterType) { DependencyAttribute attr = (DependencyAttribute) paramAttr; ConstructorInfo dependencyResolverCtor = GetConstructor<DependencyResolver>(typeof (IBuilderContext)); MethodInfo resolve = GetMethodInfo<DependencyResolver>("Resolve", typeof (Type), typeof (Type), typeof (string), typeof (NotPresentBehavior), typeof (SearchMode)); // Create the dependency resolver object // Constructor takes context il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Newobj, dependencyResolverCtor); // Call frame for call to .Resolve // Actual parameter type EmitLoadType(il, parameterType); // Type to create if (attr.CreateType != null) { EmitLoadType(il, attr.CreateType); } else { il.Emit(OpCodes.Ldnull); } // id if (attr.Name != null) { il.Emit(OpCodes.Ldstr, attr.Name); } else { il.Emit(OpCodes.Ldnull); } // NotPresentBehavior il.Emit(OpCodes.Ldc_I4, (int) attr.NotPresentBehavior); // SearchMode il.Emit(OpCodes.Ldc_I4, (int) attr.SearchMode); // And call it il.EmitCall(OpCodes.Call, resolve, null); }
/// <summary> /// /// </summary> /// <param name="il"></param> /// <param name="paramAttr"></param> /// <param name="parameterType"></param> public override void EmitParameterResolution(ILGenerator il, ParameterAttribute paramAttr, Type parameterType) { ServiceDependencyAttribute attr = (ServiceDependencyAttribute) paramAttr; MethodInfo getLocator = GetPropertyGetter<IBuilderContext>("Locator", typeof (IReadWriteLocator)); MethodInfo getFromLocator = ObtainGetFromLocatorMethod(); MethodInfo getServices = GetPropertyGetter<CompositionContainer>("Services", typeof (IServiceCollection)); MethodInfo getFromServices = GetMethodInfo<IServiceCollection>("Get", typeof (Type), typeof (bool)); ConstructorInfo newDependencyKey = GetConstructor<DependencyResolutionLocatorKey>(typeof (Type), typeof (string)); // context.get_Locator il.Emit(OpCodes.Ldarg_0); il.EmitCall(OpCodes.Callvirt, getLocator, null); // new DependencyResolutionContainer(typeof(CompositionContainer), null) EmitLoadType(il, typeof (CompositionContainer)); il.Emit(OpCodes.Ldnull); il.Emit(OpCodes.Newobj, newDependencyKey); // locator.Get(key) il.EmitCall(OpCodes.Callvirt, getFromLocator, null); il.Emit(OpCodes.Castclass, typeof (CompositionContainer)); // container.get_Services il.EmitCall(OpCodes.Callvirt, getServices, null); if (attr.Type != null) { EmitLoadType(il, attr.Type); } else { EmitLoadType(il, parameterType); } if (attr.Required) { il.Emit(OpCodes.Ldc_I4_1); } else { il.Emit(OpCodes.Ldc_I4_0); } il.EmitCall(OpCodes.Callvirt, getFromServices, null); }
/// <summary> /// Gets the <see cref="IParameterResolver"/> registered for a specific <see cref="ParameterAttribute"/>. /// </summary> /// <param name="resolutionAttribute">The parameter to get the registered resolver for.</param> /// <returns>An instance of <see cref="IParameterResolver"/> used to emit the IL code.</returns> public static IParameterResolver GetResolver(ParameterAttribute resolutionAttribute) { Guard.ArgumentNotNull(resolutionAttribute, "resolutionAttribute"); return resolvers[resolutionAttribute.GetType()]; }
public void EmitParameterResolution(ILGenerator il, ParameterAttribute paramAttr, Type parameterType) { throw new NotImplementedException(); }
/// <summary> /// /// </summary> /// <param name="il"></param> /// <param name="paramAttr"></param> /// <param name="parameterType"></param> public override void EmitParameterResolution( ILGenerator il, ParameterAttribute paramAttr, Type parameterType) { if (!typeof (IStateValue).IsAssignableFrom(parameterType)) { throw new ArgumentException("StateDependency parameters must be of type IStateValue"); } StateDependencyAttribute stateAttr = (StateDependencyAttribute) paramAttr; MethodInfo getLocator = GetPropertyGetter<IBuilderContext>("Locator", typeof (IReadWriteLocator)); MethodInfo getFromLocator = ObtainGetFromLocatorMethod(); MethodInfo getSessionState = GetMethodInfo<ISessionStateLocatorService>("GetSessionState"); MethodInfo setSessionState = GetPropertySetter<IStateValue>("SessionState", typeof (IHttpSessionState)); MethodInfo setKeyName = GetPropertySetter<IStateValue>("KeyName", typeof (string)); ConstructorInfo stateValueCtor = parameterType.GetConstructor(new Type[0]); ConstructorInfo keyCtor = GetConstructor<DependencyResolutionLocatorKey>(typeof (Type), typeof (string)); // Get session locator from context, store in a local LocalBuilder sessionStateLocator = il.DeclareLocal(typeof (ISessionStateLocatorService)); // Get locator il.Emit(OpCodes.Ldarg_0); il.EmitCall(OpCodes.Callvirt, getLocator, null); // Get Session state provider // Create key EmitLoadType(il, typeof (ISessionStateLocatorService)); il.Emit(OpCodes.Ldnull); il.Emit(OpCodes.Newobj, keyCtor); // Look up in locator il.EmitCall(OpCodes.Callvirt, getFromLocator, null); il.Emit(OpCodes.Stloc, sessionStateLocator); Label noLocator = il.DefineLabel(); Label done = il.DefineLabel(); // Do we have a session locator? il.Emit(OpCodes.Ldloc, sessionStateLocator); il.Emit(OpCodes.Ldnull); il.Emit(OpCodes.Ceq); il.Emit(OpCodes.Brtrue, noLocator); // Session state locator, new up our state value LocalBuilder valueLocal = il.DeclareLocal(typeof (IStateValue)); il.Emit(OpCodes.Newobj, stateValueCtor); il.Emit(OpCodes.Castclass, typeof (IStateValue)); il.Emit(OpCodes.Stloc, valueLocal); il.Emit(OpCodes.Ldloc, valueLocal); il.Emit(OpCodes.Ldloc, sessionStateLocator); il.EmitCall(OpCodes.Callvirt, getSessionState, null); il.EmitCall(OpCodes.Callvirt, setSessionState, null); il.Emit(OpCodes.Ldloc, valueLocal); il.Emit(OpCodes.Ldstr, stateAttr.KeyName); il.EmitCall(OpCodes.Callvirt, setKeyName, null); il.Emit(OpCodes.Ldloc, valueLocal); il.Emit(OpCodes.Br_S, done); il.MarkLabel(noLocator); il.Emit(OpCodes.Ldnull); il.MarkLabel(done); }