/// <summary> /// Defines the nested type that will instantiate the actual singleton service instance. /// </summary> /// <param name="module">The module that will host the singleton type.</param> /// <param name="singletonType">The singleton type.</param> /// <param name="instanceField">The field that will hold the singleton instance.</param> /// <param name="serviceMap">The service map that contains the list of dependencies in the application.</param> /// <param name="implementation">The implementation that will instantiate the dependency.</param> /// <param name="dependency">The dependency that will be instantiated by the singleton.</param> /// <param name="targetMethod">The method that will be used to instantiate the actual service instance.</param> private void DefineNestedType(ModuleDefinition module, TypeDefinition singletonType, FieldDefinition instanceField, IDictionary<IDependency, IImplementation> serviceMap, IImplementation implementation, IDependency dependency, MethodDefinition targetMethod) { var objectType = module.ImportType(typeof (object)); var nestedName = string.Format("Nested-{0}", dependency.GetHashCode()); const TypeAttributes nestedAttributes = TypeAttributes.NestedFamORAssem | TypeAttributes.Sealed | TypeAttributes.AutoClass | TypeAttributes.Class | TypeAttributes.AnsiClass; var nestedType = module.DefineClass(nestedName, "Hiro.Containers.Internal", nestedAttributes, objectType); singletonType.NestedTypes.Add(nestedType); nestedType.Fields.Add(instanceField); // Emit the static constructor body var cctor = DefineNestedConstructors(module, nestedType); EmitSingletonInstantiation(dependency, implementation, serviceMap, instanceField, cctor, module, targetMethod); }
/// <summary> /// Adds a default constructor to the singleton type. /// </summary> /// <param name="module">The module that will host the singleton type.</param> /// <param name="singletonName">The name of the singleton.</param> /// <param name="typeAttributes">The type attributes that describes the singleton type.</param> /// <param name="objectType">The object ty pe.</param> /// <returns>A <see cref="TypeDefinition"/> that represents the singleton type.</returns> private static TypeDefinition AddDefaultSingletonConstructor(ModuleDefinition module, string singletonName, TypeAttributes typeAttributes, TypeReference objectType) { // Add a default constructor and make it private var singletonType = module.DefineClass(singletonName, "Hiro.Containers.Internal", typeAttributes, objectType); singletonType.AddDefaultConstructor(); var constructor = singletonType.GetDefaultConstructor(); constructor.IsFamilyOrAssembly = true; return singletonType; }