简体   繁体   中英

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:

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)

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.

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.

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

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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