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

标签 c# generics static constructor

我完全不熟悉 C# 泛型,所以我遇到了以下问题。我有2节课。一个基于实例和另一个通用类。 我想在第 2 个通用类类型的第 1 类中拥有一个属性。

这些方面的东西......功能明智。

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 不支持泛型属性或构造函数。 那么我该如何解决这个问题呢?我仍然想要一个通用类,并根据传递给第一类构造函数的参数在我的第二类中以某种方式实例化它,以便在第二类中调用方法加载、openm、关闭。

感谢您的帮助。

注意:我知道上面的代码无法编译,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 类型,因此您可以在函数调用中将其移至较低级别。

关于c# - 如何使泛型类成为另一个实例类中的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9550953/

相关文章:

Android应用程序和Activity之间的静态存储

c# - 在 C# 中扩展枚举

c# - 如何从 mysql 查询中存储对象 max(date) 和 min(date)

c# - 作为一名熟练的 c# 程序员,Java 类型参数推理规则中有哪些陷阱在等着我?

PHP 静态属性和 const 覆盖

c# - 何时在 C# 中使用静态类

c# - 在一个循环中,我应该重用单个类实例,还是创建新实例?

c# - 将 Entity Framework 调用绑定(bind)到一个公共(public)参数,例如 UserId

ios - 如何使用泛型协议(protocol)作为变量类型

c# - 如何在 AOT 平台上在运行时生成任何泛型类型?