/// <summary> /// Applys a service object type that used to create service objects. /// </summary> /// <param name="objectType">A <c>System.Type</c> that specifies the service object type to create service object.</param> /// <param name="parameters">A <c>IDictionary<String, String^gt;</c> that specifies a dictionary of the dependency constructor of the service object type.</param> /// <param name="properties">A <c>IDictionary<String, String^gt;</c> specifies a dictionary to set properties of the service object.</param> /// <param name="name">An <c>System.String</c> that specifies the name of service object to get.</param> /// <param name="version">An <c>System.String</c> that specifies the version of service object to get.</param> /// <returns>An IoC container that implements <c>Alioth.Framework.IAliothServiceContainer</c>.</returns> public IAliothServiceContainer Apply(Type objectType, IDictionary <String, String> parameters, IDictionary <String, String> properties, string name, string version) { #region precondition if (objectType == null) { throw new ArgumentNullException("objectType"); } #if NET451 if (!objectType.IsClass) { #elif DOTNET5_4 if (!objectType.GetTypeInfo().IsClass) { #endif throw new ArgumentOutOfRangeException("objectType", "The specified object type should be a concrete class."); } #endregion #if NET451 ServiceTypeAtrribute[] attributes = (ServiceTypeAtrribute[])objectType.GetCustomAttributes(typeof(ServiceTypeAtrribute), false); #elif DOTNET5_4 ServiceTypeAtrribute[] attributes = objectType.GetTypeInfo().GetCustomAttributes <ServiceTypeAtrribute>(false).ToArray(); #endif if (attributes.Length == 0) { throw new ArgumentException(String.Format("{0} should be to anotate with {1}", objectType.Name, attributes)); } IObjectBuilder builder = GetBuilder(objectType, parameters, properties, attributes); foreach (ServiceTypeAtrribute attribute in attributes) { ServiceKey key = ServiceKey.Create(attribute.ServiceType, name, version); AddBuilder(key, builder); } return(this); }
private void InjectDepedencyProperties(object instance) { #if NET451 PropertyInfo[] properties = objectType.GetProperties(BindingFlags.SetProperty | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance) #elif DOTNET5_4 PropertyInfo[] properties = objectType.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance) #endif .Where(p => p.GetCustomAttributes(false).Any(s => s.GetType() == typeof(DepedencyAtrribute))) .ToArray(); foreach (PropertyInfo p in properties) { #if NET451 DepedencyAtrribute attr = (DepedencyAtrribute)p.GetCustomAttributes(typeof(DepedencyAtrribute), false)[0]; #elif DOTNET5_4 DepedencyAtrribute attr = p.GetCustomAttributes <DepedencyAtrribute>().First(); #endif var v = this.GetService(attr.ServiceType, attr.ServiceName, attr.ServiceVersion); if (v == null) { throw new KeyNotFoundException( String.Format( "The sepecified depedency service with a key '{0}' could not be found. Type: {1}", ServiceKey.Create(attr.ServiceType, attr.ServiceName, attr.ServiceVersion), ObjectType.AssemblyQualifiedName)); } p.SetValue(instance, v, null); } }
/// <summary> /// Gets the service object of the specified type. /// </summary> /// <param name="serviceType">An <c>System.Type</c> that specifies the type of service object to get.</param> /// <param name="name">An <c>System.String</c> that specifies the name of service object to get.</param> /// <param name="version">An <c>System.String</c> that specifies the version of service object to get.</param> /// <returns>A service object of type serviceType.-or- null if there is no service object of type serviceType.</returns> public object GetService(Type serviceType, string name, string version) { Object obj = null; var key = ServiceKey.Create(serviceType, name, version); IObjectBuilder builder; if (builderContainer.TryGetValue(key, out builder)) { obj = builder.Build(); } else if (parent != null) { obj = parent.GetService(serviceType); } return(obj); }