泛型类型具有带有参数的构造函数 [英] Generic Type has constructor with parameter(s)
本文介绍了泛型类型具有带有参数的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我对泛型类型有疑问.我试图制作一个泛型类,然后再制作一些带有带有参数的构造函数的类.
问题
--------
当我尝试从泛型类调用方法时,发生了错误.
这是用于代码:
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>
这篇关于泛型类型具有带有参数的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文