/// <summary> /// Register a scoped component. /// </summary> /// <param name="type">Type of component to register</param> /// <param name="attribute">Component attribute</param> protected virtual void RegisterScoped(Type type, ComponentAttribute attribute) { if (type == null) { throw new ArgumentNullException("type"); } if (attribute == null) { throw new ArgumentNullException("attribute"); } _builder.RegisterType(type).AsImplementedInterfaces().AsSelf().InstancePerLifetimeScope(); }
private static ComponentModel?BuildComponentModel(this Type type, ComponentAttribute componentAttribute) { if (componentAttribute == null) { return(null); } return(new ComponentModel(type) { Service = componentAttribute.Service, AutofacScope = componentAttribute.AutofacScope, IsOnlyRegisterProperties = false, }); }
/// <summary> /// 注册一个Component /// </summary> /// <param name="oneType"></param> /// <param name="inputObj">注册的对象</param> private void RegiestComponent(Type oneType) { //获取Component特性 if (null != oneType.GetCustomAttribute(m_useCompentType)) { ComponentAttribute tempComponentAttribute = oneType.GetCustomAttribute(m_useCompentType) as ComponentAttribute; if (oneType.IsGenericType) { RegiestComponentByGeneric(oneType, tempComponentAttribute); } else { RegiestComponentByNoneGeneric(oneType, tempComponentAttribute); } } }
#pragma warning disable RCS1213 // Remove unused member declaration. private static PropertyDescription <T> CreatePropertyDescription <T>( PropertyInfo property, Type componentType, PropertyAttribute propertyAttribute, ComponentAttribute componentAttribute) where T : struct, IComparable <T> => new PropertyDescription <T> { ComponentId = componentAttribute.Id, PropertyInfo = property, IsCalculated = propertyAttribute.IsCalculated, MinValue = (T?)propertyAttribute.MinValue, MaxValue = (T?)propertyAttribute.MaxValue, DefaultValue = (T?)propertyAttribute.DefaultValue ?? (propertyAttribute.IsCalculated ? null : (T?)property.GetValue(Activator.CreateInstance(componentType))) };
public void Register <TLimit, TActivatorData, TRegistrationStyle>(IRegistrationBuilder <TLimit, TActivatorData, TRegistrationStyle> registration, Type type) { ComponentAttribute attribute = type.GetFirstAttribute <ComponentAttribute>(true); if (attribute == null) { return; } if (attribute.AsSelf) { registration.As(type); } if (attribute.AsImplementedInterfaces) { var interfaceTypes = type.GetInterfaces(); registration.As(interfaceTypes); } if (attribute.PropertyAutoWired) { registration.PropertiesAutowired(); } if (attribute.AutoActivate) { registration.AutoActivate(); } if (attribute.Lifetime == ServiceLifetime.Scoped) { registration.InstancePerMatchingLifetimeScope(attribute.MatchingLifetimeScope); } else if (attribute.Lifetime == ServiceLifetime.Singleton) { registration.SingleInstance(); } else { registration.InstancePerDependency(); } }
/// <summary> /// 设置生命周期行为 /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="X"></typeparam> /// <param name="tempComponentAttribute"></param> /// <param name="tempBuilder"></param> /// <returns></returns> private IRegistrationBuilder <object, T, X> SetLifeScope <T, X> (ComponentAttribute tempComponentAttribute, IRegistrationBuilder <object, T, X> tempBuilder) where T : ReflectionActivatorData { //设置生命周期 switch (tempComponentAttribute.LifeScope) { //在.net core Request 用 InstancePerLifetimeScope http://docs.autofac.org/en/latest/integration/aspnetcore.html case LifeScopeKind.Request: tempBuilder = tempBuilder.InstancePerLifetimeScope(); break; case LifeScopeKind.Singleton: tempBuilder = tempBuilder.SingleInstance(); break; default: break; } return(tempBuilder); }
private void CollectType(Type type) { NativeComponentAttribute nativeComponentAttribute = type.GetCustomAttribute <NativeComponentAttribute>(); ComponentAttribute componentAttr = type.GetCustomAttribute <ComponentAttribute>(); if (componentAttr is null && nativeComponentAttribute is null) { return; } BaseComponentAttribute baseAttr = nativeComponentAttribute; if (baseAttr is null) { baseAttr = componentAttr; } ComponentInfo componentInfo = new ComponentInfo(); componentInfo.IsNative = componentAttr is null; componentInfo.Type = baseAttr.InterfaceType ?? type; componentInfo.ImplType = type; componentInfo.Ctor = new TypeExtractor(type).CollectOnlyCtor(); componentInfo.HashCode = nativeComponentAttribute?.HashCode ?? 0u; KeyValuePair <Type, ComponentInfo> componentPair = new KeyValuePair <Type, ComponentInfo>(baseAttr.InterfaceType ?? type, componentInfo); if (componentInfo.IsNative) { KeyValuePair <uint, ComponentInfo> pair = new KeyValuePair <uint, ComponentInfo>(nativeComponentAttribute.HashCode, componentInfo); lock (_syncNativeObj) { _nativeComponents.Add(pair); } } lock (_syncManagedObj) _components.Add(componentPair); }
/// <summary> /// Loads the compoenent information. /// </summary> /// <param name="componentType">Component type.</param> private void LoadComponent(Type componentType) { if (!beanInfo.ContainsKey(componentType)) { beanInfo.Add(componentType, new TypeContainer(this, componentType)); } ConstructorInfo[] ctorInfos = componentType.GetConstructors(); if (ctorInfos.Length > 1 && ctorInfos.ToList().FindAll(x => x.GetCustomAttribute(typeof(AutowiredAttribute)) != null).Count() != 1) { throw new ComponentLoadException( String.Format("Multiple constructors found for type {0}. When multiple constructors are present in a component, exactly one must be marked with the Autowire attribute", componentType.Name)); } ComponentAttribute attr = componentType.GetTypeInfo().GetCustomAttribute <ComponentAttribute>(); ConstructorInfo ctor = ctorInfos.Length == 1 ? ctorInfos[0] : ctorInfos.First(x => x.GetCustomAttribute(typeof(AutowiredAttribute)) != null); string beanName = attr.Value ?? componentType.Name; beanInfo[componentType].AddBean(beanName, ctor, true); }
/// <summary> /// 注册组件 /// </summary> /// <param name="component"></param> /// <param name="services"></param> private static void RegisterComponents(ComponentAttribute component, IServiceCollection services) { switch (component.LifeTimeScope) { case ComponentLifeTimeScope.Scoped: if (component.Instance == null) { services.AddScoped(component.ServiceType, component.ImplementationType); } else { services.AddScoped(component.ServiceType, sp => Convert.ChangeType(component.Instance, component.ImplementationType)); } break; case ComponentLifeTimeScope.Singleton: if (component.Instance == null) { services.AddSingleton(component.ServiceType, component.ImplementationType); } else { services.AddSingleton(component.ServiceType, sp => Convert.ChangeType(component.Instance, component.ImplementationType)); } break; default: if (component.Instance == null) { services.AddTransient(component.ServiceType, component.ImplementationType); } else { services.AddTransient(component.ServiceType, sp => Convert.ChangeType(component.Instance, component.ImplementationType)); } break; } }
public void RemoveComponentByComponentType(Type componentType) { ComponentAttribute componentAttribute = componentType.GetCustomAttribute <ComponentAttribute>(); Type[] interfaces = componentType.GetInterfaces(); var hasInterface = Predicate.Create(() => interfaces.Length > 0); var doesNotHaveInterface = hasInterface.Not(); var hasSelf = Predicate.Create(() => EnumHelper.HasFlag(componentAttribute.RegistionMode, RegistionMode.AsSelf)); var hasInterfaces = Predicate.Create(() => EnumHelper.HasFlag(componentAttribute.RegistionMode, RegistionMode.AsInterfaces)); var asSelf = doesNotHaveInterface.Or(hasSelf); if (asSelf.IsTrue()) { this.RemoveComponentByServiceType(componentType); return; } foreach (var @interface in interfaces) { this.RemoveComponentByServiceType(@interface); } }
public static void Register <T>() { Type type = typeof(T); // reads [Component] ComponentAttribute componentAttr = null; foreach (var attr in type.GetCustomAttributes(typeof(ComponentAttribute), true)) { componentAttr = attr as ComponentAttribute; break; } if (componentAttr == null) { throw new Exception("Element class must specify a [Component] attribute"); } if (componentAttr.name == null) { throw new Exception("Element class must specify a name with a [Component(name)] attribute"); } // reads [Extend] ExtendAttribute extendAttr = null; foreach (var attr in type.GetCustomAttributes(typeof(ExtendAttribute), true)) { extendAttr = attr as ExtendAttribute; break; } // reads [HostAttributes] HostAttributesAttribute hostAttribute = null; foreach (var attr in type.GetCustomAttributes(typeof(HostAttributesAttribute), true)) { hostAttribute = attr as HostAttributesAttribute; break; } // // map published fields as properties // dynamic properties = new {}; foreach (var field in type.GetFields()) { var attributes = field.GetCustomAttributes(typeof(PropertyAttribute), true); if (attributes.Length == 0) { continue; } dynamic property = new {}; var attribute = (attributes[0] as PropertyAttribute); property["type"] = field.FieldType; if (attribute.value != null) { property["value"] = attribute.value; } if (attribute.reflectToAttribute != false) { property["reflectToAttribute"] = attribute.reflectToAttribute; } if (attribute.readOnly != false) { property["readOnly"] = attribute.readOnly; } if (attribute.notify != false) { property["notify"] = attribute.notify; } if (attribute.computed != null) { property["computed"] = attribute.computed; } if (attribute.observer != null) { property["observer"] = attribute.observer; } // write into properties object properties[field.Name] = property; } // // assemble Polymer configuration object // dynamic prototype = ((dynamic)type).prototype; prototype["is"] = componentAttr.name; if (componentAttr.extends != null) { prototype["extends"] = componentAttr.extends; } else if (extendAttr != null) { prototype["extends"] = extendAttr.extends; } if (hostAttribute != null) { prototype["hostAttributes"] = hostAttribute.hostAttributes; } prototype["properties"] = properties; // register element in Polymer RegisterInPolymer(prototype); }