C# 无法创建变量类型“Item”的实例,因为它没有 new() 约束
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14696904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot create an instance of the variable type 'Item' because it does not have the new() constraint
提问by Thalia
I am trying to test a method - and getting an error:
我正在尝试测试一种方法 - 并出现错误:
Cannot create an instance of the variable type 'Item' because it does not have the new() constraint
无法创建变量类型“Item”的实例,因为它没有 new() 约束
Required information for below:
以下所需资料:
public interface IHasRect
{
Rectangle Rectangle { get; }
}
Helper class:
辅助类:
class Item : IHasRect
{
public Item(Point p, int size)
{
m_size = size;
m_rectangle = new Rectangle(p.X, p.Y, m_size, m_size);
}
}
For the function to be tested, I need to instantiate an object...
对于要测试的功能,我需要实例化一个对象...
public class SomeClass<T> where T : IHasRect
The test:
考试:
public void CountTestHelper<Item>() where Item : IHasRect
{
Rectangle rectangle = new Rectangle(0, 0, 100, 100);
SomeClass<Item> target = new SomeClass<Item>(rectangle);
Point p = new Point(10,10);
Item i = new Item(p, 10); // error here
...
}
[TestMethod()]
public void CountTest()
{
CountTestHelper<Item>();
}
I am trying to understand what this error means, or how to fix it, by reading http://msdn.microsoft.com/en-us/library/d5x73970.aspxand http://msdn.microsoft.com/en-us/library/x3y47hd4.aspx- but it doesn't help.
我试图通过阅读http://msdn.microsoft.com/en-us/library/d5x73970.aspx和http://msdn.microsoft.com/en-来了解此错误的含义或解决方法us/library/x3y47hd4.aspx- 但它没有帮助。
I don't understand this error - I have already constrained the "SomeClass" to be of type. I cannot constrain the entire Test class (the unit test class generated by Visual Studio, which contains all the tests) - I will get a number of other errors otherwise. The Item class doesn't have any template...
我不明白这个错误 - 我已经将“SomeClass”限制为类型。我无法约束整个 Test 类(由 Visual Studio 生成的单元测试类,其中包含所有测试) - 否则我会收到许多其他错误。Item 类没有任何模板...
采纳答案by Lee
The Item
in the line:
将Item
在该行:
Item i = new Item(p, 10);
refers to the generic type parameter Item
of the CountTestHelper
method, not the class Item
. Change the generic parameter name e.g.
指的是一般类型参数Item
的的CountTestHelper
方法,而不是类Item
。更改通用参数名称,例如
public void CountTestHelper<TItem>() where TItem : IHasRect
{
Rectangle rectangle = new Rectangle(0, 0, 100, 100);
SomeClass<TItem> target = new SomeClass<TItem>(rectangle);
Point p = new Point(10,10);
Item i = new Item(p, 10);
...
}
alternatively you can fully qualify the name of the Item
class you want to create:
或者,您可以完全限定Item
要创建的类的名称:
public void CountTestHelper<Item>() where Item : IHasRect
{
Rectangle rectangle = new Rectangle(0, 0, 100, 100);
SomeClass<Item> target = new SomeClass<Item>(rectangle);
Point p = new Point(10,10);
SomeNamespace.Item i = new SomeNamespace.Item(p, 10);
}
回答by MarcinJuraszek
You can't initialize Generic type object unless you mark it as implementing default constructor using new
keyword:
除非您使用new
关键字将其标记为实现默认构造函数,否则您无法初始化通用类型对象:
public void CountTestHelper<Item>() where Item : IHasRect, new()
{
Rectangle rectangle = new Rectangle(0, 0, 100, 100);
SomeClass<Item> target = new SomeClass<Item>(rectangle);
Point p = new Point(10,10);
Item i = new Item(); // constructor has to be parameterless!
...
}
On the other hand, if you're trying to initialize an Item
type object defined somewhere else in the application try using the namespace before it:
另一方面,如果您尝试初始化Item
在应用程序中其他地方定义的类型对象,请尝试使用它之前的命名空间:
MyAppNamespace.Item i = new MyAppNamespace.Item(p, 10);
回答by Zé Carlos
Because many people get here by the question tilte (which is very generic and matches the compiler message) let me give a more detailed answer about the compiling error itsef.
因为很多人是通过问题tilte(这是非常通用的并且与编译器消息匹配)到达这里的,所以让我就编译错误itsef 给出更详细的答案。
You are using generics in a method. The compiler doesn′t know which type it will receive and thus it is not warranted that your type has a parameterless construtor. For ex:
您在方法中使用泛型。编译器不知道它将接收哪种类型,因此不能保证您的类型具有无参数构造函数。例如:
class A {
A(int i){ ... }
}
class B { ... }
public void MyMethod<T>(){
T t = new T(); //This would be fine if you use 'MyMethod<B>' but you would have a problem calling 'MyMethod<A>' (because A doesn′t have a parameterless construtor;
}
To resolve this, you can tell the compiler that your generic parameter has a parameterless construtor. This is done by defining constraints:
要解决此问题,您可以告诉编译器您的泛型参数具有无参数构造函数。这是通过定义约束来完成的:
public void MyMethod<T>() where T: new(){
T t = new T(); //Now it's ok because compiler will ensure that you only call generic method using a type with parameterless construtor;
}
More information about constructor constraints may be found here: https://msdn.microsoft.com/en-us/library/bb384067.aspx
可以在此处找到有关构造函数约束的更多信息:https: //msdn.microsoft.com/en-us/library/bb384067.aspx