简体   繁体   English

C#如何将类作为参数传递给方法并确定参数类的基类

[英]C# how to pass a class as a parameter into a method and determining the base class of the parameter class

So I have been adding a class to a list of classes by calling the constructor in the add method of the list: 所以我一直在通过在列表的add方法中调用构造函数来将类添加到类列表中:

SupportedTests.Add(new SpecificTestClass27());

However, the classes I'm using are derived classes (most of the time they have a chain of 4 or 5 base classes) and I would like to only add them to the list depending on which base class they use (not the immediate base class but one that's a couple base classes back) 但是,我正在使用的类是派生类(大多数情况下,它们具有4或5个基类链),我只想根据它们使用的基类将它们添加到列表中(而不是直接基类)课,但有几个基础课)

Example of chained base classes: 链接基类的示例:

public class SpecificTestClass27 : SpecificTestClass27_base

public abstract class SpecificTestClass27_base: OperationTestClass_base

public abstract class OperationTestClass_base: DomesticTestClass

OR 要么

public abstract class OperationTestClass_base: InternationalTestClass

Both DomesticTestClas s and InternationalTestClass derive from the same base class: TestClass , and the different classes above those 2 base classes are not necessarily the same, including the top class. DomesticTestClasInternationalTestClass都源自相同的基类: TestClass ,而这两个基类之上的不同类不一定相同,包括顶级类。

I can't change any of that code but I need a way of passing a specific class that eventually derives from either DomesticTestClass or InternationalTestClass into a method and then deciding to add the specific class to a list or not depending on which base class it has. 我无法更改任何代码,但是我需要一种方法,该方法将最终从DomesticTestClassInternationalTestClass派生的特定类传递给方法,然后决定是否将特定类添加到列表中,具体取决于它具有的基类。

I've tried just making a normal method: 我试过只是一个普通的方法:

public void AddaTestClass(object SpecificTestClass)
{
    if (base == DomesticTestClass) { SupportedTests.Add(new SpecificTestClass()); }
}

But it didn't like the parameter being a class. 但是它不喜欢参数是一个类。 And when I tried to use a generic with an overload: 当我尝试使用带有重载的泛型时:

public void AddaTestClass<<"SpecificTestClass">>() where SpecificTestClass : DomesticTestClass
{
    SupportedTests.Add(new SpecificTestClass());
}

AND

public void AddaTestClass<<"SpecificTestClass">>() where SpecificTestClass : InternationalTestClass
{

}

NOTE: I don't have SpecificTestClass in quotes in my program it just wouldn't show up here between the carats without quotation marks 注意:我的程序中没有引号中的SpecificTestClass ,只是在克拉之间没有引号就不会显示

This doesn't let me call the constructor for the class as because it does not have a new() constraint and still fails without the overload. 这不允许我调用该类的构造函数,因为它没有new()约束,并且在没有重载的情况下仍然失败。

Is there another way to do this or is it just impossible? 还有另一种方法可以做到吗?

Since you are creating a new class to add it to the collection there needs to be a new constraint added to the generic type parameter to tell the compiler there is a guaranteed public parameterless constructor. 由于您正在创建一个新类以将其添加到集合中,因此需要将一个new约束添加到通用类型参数中,以告诉编译器有一个保证的公共无参数构造函数。

public void AddADomesticTestClass<T>() where T: DomesticTestClass, new()
{
    SupportedTests.Add(new T());
}

public void AddAnInternationalTestClass<T>() where T: InternationalTestClass, new()
{
    SupportedTests.Add(new T());
}

Alternatively 或者

public void AddATestClass<T>() where T: TestClass, new()
{
    if (typeof(T).IsAssignableFrom(typeof(DomesticTestClass))
        || typeof(T).IsAssignableFrom(typeof(InternationalTestClass)))
    {
        SupportedTests.Add(new T());
    }
}

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

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