繁体   English   中英

如何使泛型类成为另一个实例类的属性?

[英]How to make a generic class a property in another instance class?

我对C#泛型完全陌生,因此遇到以下问题。 我有2节课。 基于一个实例,另一个基于通用类。 我想在第二类的类型的第一类中有一个属性。

这些方面的东西。

public class SampleClass
{
  private SampleGenericClass<T> sampleClass;

   public SampleClass(string name, int age, string version)
   {
     if(version == "1.0")
     {
       this.sampleClass = new SampleGenericClass<int>(name, age);
     }
     else
     {
       this.sampleClass = new SampleGenericClass<long>(name.age);
     }
   }

  public void Load()
  {
    this.sampleClass.Load();
  }
  public void Open()
  {
    this.sampleClass.Open();
  }
  public void Close()
  {
    this.sampleClass.Close();
  }
} 

我的第二个泛型类是这样的

  internal class SampleGenericClass<T> where T : class
  {
    private string name;
    private string age;

    public SampleGenericClass(string name, int age)
    {
      this.name = name;
      this.age = age;
    }

    public void Load()
    {
      // Do something based on type
      if(typeof(T) == typeof(int))
      {
        // load int type
      }
      else if(typeof(T) == typeof (long))
      {
        // load long type
      }
      else
      {
        throw new ArgumentException("Un supported type");
      }
    }
    public void Open()
    {
      // Do something based on type
      if(typeof(T) == typeof(int))
      {
        // open int type
      }
      else if(typeof(T) == typeof (long))
      {
        // open long type
      }
      else
      {
        throw new ArgumentException("Un supported type");
      }
    }

    public void Close()
    {
      // Do something based on type
      if(typeof(T) == typeof(int))
      {
        // close int type
      }
      else if(typeof(T) == typeof (long))
      {
        // close long type
      }
      else
      {
        throw new ArgumentException("Un supported type");
      }
    }       

  }

现在我知道CLR不支持通用属性或构造函数。 那么我该如何解决这个问题呢? 我仍然希望有一个通用类,并根据传递给第一类构造函数的参数在第二类中以某种方式实例化它,以便在第二类中调用load,openm,close方法。

谢谢你的帮助。

注意:我知道上面的代码无法编译,bcoz CLR不支持通用属性和构造函数。 只是为了说明我要在概念上实现的目的

IMO,您应该在较高级别上确定SampleClass的类型是什么,实际上您应该将其定义为泛型:

public class SampleClass<T> where T : ....
{
  private SampleGenericClass<T> sampleClass;

   public SampleClass(string name, int age, string version)
   {
       this.sampleClass = new SampleGenericClass<T>(name, age);
   }

  public void Load()
  {
    this.sampleClass.Load();
  }
} 

并根据较高版本(和相关通用名称)创建您的SampleClass。

例如:

主要方法:

if (version == "1")
{
    DoAction<int>();
}
else
  DoAction<long>();

.....
void DoAction<T>()
{
   SampleClass<T> s = new SampleClass<T>(...)
}

同样,正如我所见,成员变量不需要T类型,因此可以在函数调用中将其移至较低级别。

要么声明一个具体类型

public class SampleClass {   
    private SampleGenericClass<int> sampleClass; 
    ...
}

或将通用类型参数添加到SampleClass

public class SampleClass<T> where T : class {   
    private SampleGenericClass<T> sampleClass; 
    ...
}

接口:

 public interface ISampleGenericClass
  {
        void Load();
        void Open();
        void Close();
  }

通用类实现了这一点:

internal class SampleGenericClass<T> : ISampleGenericClass
{
   ...
}

和SampleClass

public class SampleClass
{
  private ISampleGenericClass sampleClass;
....
}

我删除了类约束,因为int和long是值类型,因此不能在当前形式中使用它们。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM