C#在运行时限制通用类型

时间:2018-11-18 11:54:27

标签: c# visual-studio class generics

我需要限制可以在运行时中放入构造函数的内容。 我上了简单的课:

    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>();

我不知道这是否可行,但是如果您有解决方案,我将非常高兴。谢谢。

2 个答案:

答案 0 :(得分:0)

您可以约束该方法,也可以只检查给定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);
            }
        }
    }

答案 1 :(得分:0)

由于您想在 runtime 上限制泛型,因此我想您也希望该错误是 runtime 错误,即异常。

在某处声明一个属性/字段,如下所示:

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}!");
}

但是我认为这种失去了泛型的意义。也许重新考虑您的设计?

相关问题