简体   繁体   English

如何使用Activator.CreateInstance实例化泛型类型作为using块的一部分?

[英]How can I use Activator.CreateInstance to instantiate a generic type as part of a using block?

What I'd like to achieve is to convert the following method to something more generic: 我想要实现的是将以下方法转换为更通用的方法:

    private Task<TestResult> SpecificServiceWarmUp(string serviceEndPoint)
    {
        return Task<TestResult>.Factory.StartNew(() =>
        {

            using (var service = new MySpecificClient(serviceEndPoint))
            {
                // do some stuff with specific client
            }
        });
    }

A more generic version would look like: 更通用的版本如下所示:

    private Task<TestResult> GenericServiceWarmUp<TService>(string serviceEndPoint)
    {
        return Task<TestResult>.Factory.StartNew(() =>
        {

            using (/* instance of TService */)
            {
                // do some stuff with generic client
            }
        });
    }

What I don't know is how to tell the using block to use Activator to create the instance of the generic type. 我不知道如何告诉using块使用Activator创建泛型类型的实例。 Is this possible? 这可能吗? If so, how can I do this? 如果是这样,我该怎么做?

You can add a constraint on your generic type parameter like that: 您可以像这样在通用类型参数上添加约束

private Task<TestResult> GenericServiceWarmUp<TService>()
where TService : IDisposable
{
    return Task<TestResult>.Factory.StartNew(() =>
    {
        using (var service = 
            (TService)Activator.CreateInstance(typeof(TService), serviceEndPoint))
        {
            // do some stuff with generic client
        }
    });
}

Otherwise if your service has no constructor parameter you don't need to use Activator.CreateInstance , there is a new() constraint : 否则,如果您的服务没有构造函数参数 ,则无需使用Activator.CreateInstance ,则存在new()约束

private Task<TestResult> GenericServiceWarmUp<TService>(string serviceEndPoint)
where TService : IDisposable, new()
{
    return Task<TestResult>.Factory.StartNew(() =>
    {

        using (var service = new TService())
        {
            // do some stuff with generic client
        }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Activator.CreateInstance进行通用类型转换 - Generic type casting using Activator.CreateInstance 我可以将 Activator.CreateInstance 与接口一起使用吗? - Can I use Activator.CreateInstance with an Interface? 无法使用通用方法类型 <T> 直接在Activator.CreateInstance中 - Unable to use generic method type <T> directly in Activator.CreateInstance 从类型创建泛型变量 - 如何? 或者使用带有属性{}而不是参数()的Activator.CreateInstance()? - Creating generic variables from a type - How? Or use Activator.CreateInstance() with properties { } instead of parameters ( )? Activator.CreateInstance-无法使用返回类型 - Activator.CreateInstance - Not able to use the return type 如何使用Activator.CreateInstance从系统dll中加载类型? - How to load type from system dll using Activator.CreateInstance? 我如何使用Activator.CreateInstance与字符串? - How do i use Activator.CreateInstance with strings? 如何使用Activator.CreateInstance将类转换为基类型以设置值? - How do I downcast a class to its base type to set values using Activator.CreateInstance? 如何使用Activator.CreateInstance创建List <T> T在运行时未知? - How can I use Activator.CreateInstance to create a List<T> where T is unknown at runtime? 具有通用存储库的Activator.CreateInstance - Activator.CreateInstance with a generic repository
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM