我有一个接受T类型的泛型方法,我需要能够调用一个需要单个XmlNode的构造函数。目前,我正在尝试通过拥有一个抽象基类来实现我想要的构造函数(加上一个无参数的基类,所以我不必编辑除了添加实际子类化之外的“子类”)并通过它进行约束。如果我尝试将这些类别实例之一,它抱怨:通用方法:使用参数实例化一个泛型类型
Cannot create an instance of the variable type 'T' because it does not have the new() constraint
,如果我加入new()
约束,我得到:
'T': cannot provide arguments when creating an instance of a variable type
我该怎么做我想要什么?
我不能使用反射,我想避免使用Initialize函数,如果可能的话。不过这是一个选项。 – RCIX 2010-07-25 01:25:30
用更多选项编辑答案......你甚至回答了上述第二个链接的问题:http://stackoverflow.com/questions/1682310/c-generics-problem-newing-up-the-generic-type-with-参数在构建/ 1691584#1691584 – 2010-07-25 01:29:31
我可能会去与该链接SO问题的建议之一,谢谢! – RCIX 2010-07-25 03:20:42