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

时间:2017-09-19 18:34:39

标签: c# class generics inheritance

所以我通过在列表的add方法中调用构造函数将类添加到类列表中:

SupportedTests.Add(new SpecificTestClass27());

但是,我使用的类是派生类(大多数时候它们有4或5个基类的链),我想只根据它们使用的基类将它们添加到列表中(不是直接的基类,但是有一个基类的基类)

链式基类的示例:

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

DomesticTestClasInternationalTestClass都来自同一个基类:TestClass,而这两个基类之上的不同类不一定相同,包括顶级类。< / p>

我无法更改任何代码,但我需要一种方法将最终派生自DomesticTestClassInternationalTestClass的特定类传递到方法中,然后决定将特定类添加到列表与否取决于它所具有的基类。

我尝试过制作一个正常的方法:

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

但它不喜欢参数是一个类。 当我尝试使用带有重载的泛型时:

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

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

}

注意:我的程序中没有SpecificTestClass引号,它不会显示在没有引号的克拉之间

这不允许我调用类的构造函数,因为它没有new()约束,并且在没有重载的情况下仍然失败。

还有另一种方法可以做到这一点还是只是不可能?

1 个答案:

答案 0 :(得分:0)

由于您正在创建一个新类以将其添加到集合中,因此需要在泛型类型参数中添加new constraint以告诉编译器有一个保证公共无参数构造函数。

$json_from_database='[{"genename":"xkr4","TA11MEAN":"974.25","TA11STD":"99.0085223605","TA21MEAN":"710.75","TA21STD":"115.79831605","TA22MEAN":"736.5","TA22STD":"115.79831605","TA23MEAN":"903.75","TA23STD":"107.283211641","TB11MEAN":"799.25","TB11STD":"97.2660655111","TB21MEAN":"658","TB21STD":"91.7959694104","TB22MEAN":"592.75","TB22STD":"70.9379129944","TB23MEAN":"864","TB23STD":"92.7280971443"}]';

//print decode array from databse
$decoded=json_decode($json_from_database);

var_dump($decoded);
foreach ($decoded[0] as $key => $value) {
  echo   "\n  ";
  print $key;
  print "   " .$decoded[0]->$key;
}
array(1) {
  [0]=>
  object(stdClass)#1 (17) {
    ["genename"]=>
    string(4) "xkr4"
    ["TA11MEAN"]=>
    string(6) "974.25"
    ["TA11STD"]=>
    string(13) "99.0085223605"
    ["TA21MEAN"]=>
    string(6) "710.75"
    ["TA21STD"]=>
    string(12) "115.79831605"
    ["TA22MEAN"]=>
    string(5) "736.5"
    ["TA22STD"]=>
    string(12) "115.79831605"
    ["TA23MEAN"]=>
    string(6) "903.75"
    ["TA23STD"]=>
    string(13) "107.283211641"
    ["TB11MEAN"]=>
    string(6) "799.25"
    ["TB11STD"]=>
    string(13) "97.2660655111"
    ["TB21MEAN"]=>
    string(3) "658"
    ["TB21STD"]=>
    string(13) "91.7959694104"
    ["TB22MEAN"]=>
    string(6) "592.75"
    ["TB22STD"]=>
    string(13) "70.9379129944"
    ["TB23MEAN"]=>
    string(3) "864"
    ["TB23STD"]=>
    string(13) "92.7280971443"
  }
}

  genename   xkr4
  TA11MEAN   974.25
  TA11STD   99.0085223605
  TA21MEAN   710.75
  TA21STD   115.79831605
  TA22MEAN   736.5
  TA22STD   115.79831605
  TA23MEAN   903.75
  TA23STD   107.283211641
  TB11MEAN   799.25
  TB11STD   97.2660655111
  TB21MEAN   658
  TB21STD   91.7959694104
  TB22MEAN   592.75
  TB22STD   70.9379129944
  TB23MEAN   864
  TB23STD   92.7280971443
This is how your data looks like from database it is a array of rows row is a object

可选地

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

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