ServiceCreatorCallback 委托

定义

提供一种回调机制,它可以即需创建服务的实例。

C#
public delegate object ServiceCreatorCallback(IServiceContainer container, Type serviceType);
C#
public delegate object? ServiceCreatorCallback(IServiceContainer container, Type serviceType);
C#
[System.Runtime.InteropServices.ComVisible(true)]
public delegate object ServiceCreatorCallback(IServiceContainer container, Type serviceType);

参数

container
IServiceContainer

请求创建服务的服务容器。

serviceType
Type

要创建的服务的类型。

返回值

serviceType 指定的服务;如果未能创建服务,则为 null

属性

示例

下面的代码示例演示如何使用回调函数发布服务。

C#
// The following code shows how to publish a service using a callback function.

// Creates a service creator callback.
ServiceCreatorCallback callback1 = 
new ServiceCreatorCallback(myCallBackMethod);

// Adds the service using its type and the service creator callback.
serviceContainer.AddService(typeof(myService), callback1);

注解

ServiceCreatorCallback 提供了一种机制来发布可以请求在需要时创建的服务,而不是在设计器加载时立即创建服务。 如果服务不是必需的且不能使用,则可以使用回调函数。 如果未请求和创建服务,则使用 ServiceCreatorCallback 发布的服务不会使用尽可能多的其他资源。 若要使用回调函数发布服务,请将 传递给 ServiceCreatorCallbackAddServiceIServiceContainer方法。

扩展方法

GetMethodInfo(Delegate)

获取指示指定委托表示的方法的对象。

适用于

产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

另请参阅