C# 如何使泛型类成为另一个实例类中的属性?
我是C泛型的新手,所以我有以下问题。我有两门课。一个基于实例的类和另一个泛型类。 我想在第二个泛型类的第一个类中有一个属性 这些方面的东西……功能方面的C# 如何使泛型类成为另一个实例类中的属性?,c#,generics,static,constructor,C#,Generics,Static,Constructor,我是C泛型的新手,所以我有以下问题。我有两门课。一个基于实例的类和另一个泛型类。 我想在第二个泛型类的第一个类中有一个属性 这些方面的东西……功能方面的 public class SampleClass { private SampleGenericClass<T> sampleClass; public SampleClass(string name, int age, string version) { if(version == "1.0")
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不支持泛型属性和构造函数。这只是为了说明我想要在概念上实现的目标,在我看来,您应该在上层决定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
e、 g:
主要方法:
if (version == "1")
{
DoAction<int>();
}
else
DoAction<long>();
.....
void DoAction<T>()
{
SampleClass<T> s = new SampleClass<T>(...)
}
另外,正如我所看到的,您不需要为您的成员变量键入,因此您可以在函数调用中将其移动到较低级别。我认为您应该在较高级别决定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
e、 g:
主要方法:
if (version == "1")
{
DoAction<int>();
}
else
DoAction<long>();
.....
void DoAction<T>()
{
SampleClass<T> s = new SampleClass<T>(...)
}
正如我所看到的,您不需要为成员变量键入,因此可以在函数调用中将其移动到较低级别。或者声明一个具体类型
public class SampleClass {
private SampleGenericClass<int> sampleClass;
...
}
或者声明一个具体类型
public class SampleClass {
private SampleGenericClass<int> sampleClass;
...
}
接口:
public interface ISampleGenericClass
{
void Load();
void Open();
void Close();
}
泛型类实现以下功能:
internal class SampleGenericClass<T> : ISampleGenericClass
{
...
}
我删除了类约束,因为int和long是值类型,所以它们不能在当前表单中使用。接口:
public interface ISampleGenericClass
{
void Load();
void Open();
void Close();
}
泛型类实现以下功能:
internal class SampleGenericClass<T> : ISampleGenericClass
{
...
}
我删除了类约束,因为int和long是值类型,所以它们不能在当前表单中使用。您的程序中有一个设计流程。如果您知道要使用int和long的类型,那么只需创建一个包含两个实现的抽象类,一个接受int,另一个需要long。您的程序中有一个设计流。如果您知道要使用int和long的类型,那么只需创建一个包含两个实现的抽象类,一个需要int,另一个需要long