如何将泛型类型限制为必须具有承担某些参数的construtor? [英] How to constrain generic type to must have a construtor that takes certain parameters?

查看:135
本文介绍了如何将泛型类型限制为必须具有承担某些参数的construtor?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包装器通用类,旨在与一组类型一起使用。这些类型由实用程序生成,并且都是从基类ClientBase派生的。虽然ClientBase只有一个默认构造函数,所有生成的类型都有默认构造函数,构造函数接受一个字符串作为参数。在包装类的构造函数中,我使用接受字符串的构造函数来实例化类型的实例。下面是一个示例代码:

  public class ClientBase 
{}

public class GenericProxy< ; T>
其中T:ClientBase,new()
{
T _proxy;

public GenericProxy(string configName)
{
_proxy = new T(configName);
}
}


$ b $ p

此代码不能编译,因为类型T不能保证有一个构造函数接受一个字符串。有没有一种方法来定义通用类的约束,强制类型T必须有一个构造函数接受一个字符串?如果这是不可能的,什么是好的替代方案来处理这种情况?

解决方案

这是不可能的。我想查看static interfaces来处理这个问题,但不要期望它们在任何时间很快...



替代方法:




  • 指定代理作为T的工厂

  • 指定另一个接口作为T的工厂

  • 在T本身上指定一个接口进行初始化(并添加约束,使 T 实现接口)



    • 前两个是等效的。基本上你会将你的代理类更改为这样:

        public class GenericProxy< T& 
      其中T:ClientBase,new()
      {
      string _configName;
      T _proxy;
      Func< string,T> _厂;

      public GenericProxy(Func< string,T> factory,string configName)
      {
      _configName = configName;
      _factory = factory;
      RefreshProxy();
      }

      void RefreshProxy()//作为例子;假设我们以后需要这样做
      {
      _proxy = factory(configName);
      }
      }

      (假设您要创建更多的实例以后 - 否则你可能会传递一个T的实例到构造函数。)


      I have a wrapper generic class that intended to be used with a set of types. Those types are generated by a utility and are all derived from a base class ClientBase. While ClientBase has only a default constructor, all generated types have default constructor as well as a constructor takes a string as parameter. In the constructor of the wrapper class, I instantiate an instance of the type with the constructor that takes a string. Here is a sample code:

      public class ClientBase
      { }
      
      public class GenericProxy<T>
          where T: ClientBase, new()
      {
          T _proxy;
      
          public GenericProxy(string configName)
          {
              _proxy = new T(configName);    
          }
      }
      

      This code does not compile because type T is not guaranteed to have a constructor that takes a string. Is there a way to define a constrain on the generic class to enforce that the type T must have a constructor that take a string? If this is not possible, what are good alternatives to handle this kind of situation?

      解决方案

      It's not possible. I'd like to see "static interfaces" to handle this, but don't expect them any time soon...

      Alternatives:

      • Specify a delegate to act as a factory for T
      • Specify another interface to act as a factory for T
      • Specify an interface on T itself for initialization (and add a constraint so that T implements the interface)

      The first two are really equivalent. Basically you'd change your proxy class to something like this:

      public class GenericProxy<T>
          where T: ClientBase, new()
      {
          string _configName;
          T _proxy;
          Func<string, T> _factory;
      
          public GenericProxy(Func<string, T> factory, string configName)
          {
              _configName = configName;
              _factory = factory;
              RefreshProxy();
          }
      
          void RefreshProxy() // As an example; suppose we need to do this later too
          {
              _proxy = factory(configName);
          }
      }
      

      (I assume you're going to want to create more instances later - otherwise you might as well pass an instance of T into the constructor.)

      这篇关于如何将泛型类型限制为必须具有承担某些参数的construtor?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆