本文介绍了泛型类型具有带有参数的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好, 我对泛型类型有疑问.我试图制作一个泛型类,然后再制作一些带有带有参数的构造函数的类. 问题 -------- 当我尝试从泛型类调用方法时,发生了错误. 这是用于代码:
Hi all, I have a problem with generic type. I tried to make one generic class and then some classes that have constructor with parameter(s). Problem -------- When i''ll try to invoke method from generic class, the error was occurred. This is for the code :
public class Test { public Test(string a) { //todo.. } } public class Test2 { public Test(string a, int b) { //todo.. } } public Generic<T> where T : class, new() { public bool Save(T t){ //todo.. return blah... } } var result = new Generic<Test>().Save(blahhh);错误:
Error :
must be a non-abstract type with a public parameterless constructor in order to use it as parameter T in the generic type请帮帮我.. :(谢谢
Please help me.. :( Thanks
推荐答案首先,您发布的代码有很多错误:此编译: First of all, your code that you published had a ton of errors: This compiles: public class Test { public Test(string a) { //todo.. } } public class Test2 { public Test2(string a, int b) { //todo.. } public Test2(); } public class Generic<T> where T : class, new() { public bool Save(T t) { //todo.. return true; } }
现在可以编译:
This will now compile:
var result = new Generic<test2>().Save(new Test2("dfd", 2));</test2>
更多推荐
泛型类型具有带有参数的构造函数
发布评论