[英]C# Limiting Generic types in RunTime
我需要限制可以在运行时中放入构造函数的内容。 我有简单的课程:
class Human
{
public string Name { get; set; } = "John";
public int Age { get; set; } = 20;
public bool IsAlive { get; set; } = true;
public override string ToString()
{
return $"Name: {Name}, Age: {Age}, Is alive: {IsAlive}";
}
}
class Animal
{
public string Type { get; set; } = "Bat";
public int Weight { get; set; } = 33;
public bool IsAlive { get; set; } = true;
public override string ToString()
{
return $"Type: {Type}, Weight: {Weight}, Is alive: {IsAlive}";
}
}
class Generics<T> where T : class, new()
{
public Generics()
{
T type = new T();
Console.WriteLine(type.GetType());
Console.WriteLine(type.ToString());
}
}
用户可以在运行时中说“类型T只能是人类”或“类型T只能是动物”的任何方式。 因此,如果用户(例如,在switch中)说“ Type T只能是人类”,则尝试创建构造函数(Type T是动物)将导致错误。
或者,如果用户说“ Type T只能是动物”,则尝试创建构造函数(Type T是人类)会导致错误。
另一个示例:用户说:“类型T只能是动物”:
然后做: Generics<Human> human = new Generics<Human>();
将导致错误。
或当然,如果用户说“类型T只能是人类”,这将导致错误:
Generics<Animal> animal = new Generics<Animal>();
我不知道这是否可行,但是如果您有解决方案,我将非常高兴。 谢谢。
您可以约束该方法,也可以只检查给定T的类型
public class Creator
{
public static T CreateHuman<T>()
where T : Human, new()
{
return new T();
}
public static T CreateAnimal<T>()
where T : Animal, new()
{
return new T();
}
public static T Create<T>()
where T : class, new()
{
switch (typeof(T))
{
case Type t when t == typeof(Human):
//throw new Exception("Type can be only Animal");
break;
case Type t when t == typeof(Animal):
//throw new Exception("Type can be only Human");
break;
}
return default(T);
}
}
}
由于您想在运行时限制泛型,因此我想您也希望该错误是运行时错误,即异常。
声明某处的属性/字段,如下所示:
public Type TypeMustBe { get; set; }
在运行时,要说“ T必须是Animal
”,您可以执行以下操作:
TypeMustBe = typeof(Animal);
同样,要说“ T必须是Human
”,您可以这样做:
TypeMustBe = typeof(Human);
在构造函数中,您可以执行以下操作:
if (typeof(T) != TypeMustBe) {
throw new Exception($"T must be {TypeMustBe}!");
}
但是我认为这种方法失去了泛型的意义。 也许重新考虑您的设计?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.