UseDIAttribute Class

指示一个类可用于依赖注入,并可设置生命周期和一个或多个服务类型(服务类型通常为类所实现的接口)。此特性只可应用于类。

Definition

Namespace: QuickAdmin.Common.Attributes
Assembly: QuickAdmin.Net (in QuickAdmin.Net.dll) Version: 2.0.13
C#
public class UseDIAttribute : Attribute
Inheritance
object    Attribute    UseDIAttribute

Remarks

生命周期默认为 Singleton
通常如果你的服务直接或间接实现了多个接口,需要通过 [UseDI] 特性来指定要注册的是哪一个接口。
C#
[UseDI(ServiceLifetime.Transient)]
public class MyService : xxx { }

[UseDI(typeof(xxx), typeof(yyy))]
public class MyService : xxx { }

[UseDI(ServiceLifetime.Transient, typeof(xxx))]
public class MyService : xxx { }

// Keyed Service
[UseDI("someKey", ServiceLifetime.Singleton, typeof(xxx))]
public class MyService : xxx { }
参见 AddServicesFromAssemblyExceptNotUseDIs(IServiceCollection, Assembly, string, ServiceLifetime, FuncType, bool) 扩展方法。

Constructors

UseDIAttribute(object) 使用指定的服务 Key 初始化实例,生命周期将为 Singleton
UseDIAttribute(Type) 使用指定的服务类型初始化实例,生命周期将为 Singleton
UseDIAttribute(ServiceLifetime, Type) 使用指定的生命周期以及指定的服务类型初始化实例。
UseDIAttribute(object, ServiceLifetime, Type) 使用指定的服务 Key、生命周期以及指定的服务类型初始化实例。

Properties

Public Properties

Lifetime 获取要注册的服务的生命周期。
ServiceKey 获取要注册的服务的 Key。
ServiceTypes 获取要注册的服务类型数组。

See Also