无法创建变量类型'Item'的实例,因为它没有new()约束 [英] Cannot create an instance of the variable type 'Item' because it does not have the new() constraint
问题描述
我试图测试一个方法 - 并得到一个错误:
无法创建变量类型的实例'Item '因为它没有new()约束
以下所需的信息:
public interface IHasRect
{
Rectangle Rectangle {get; }
}
助手类:
class Item:IHasRect
{
public Item(Point p,int size)
{
m_size = size;
m_rectangle = new Rectangle(p.X,p.Y,m_size,m_size);
$ b $ p
$ b 对于要测试的函数,我需要实例化一个对象......
public class SomeClass< T> T:IHasRect
测试:
public void CountTestHelper< Item>()
其中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); //错误在这里
[TestMethod()]
public void CountTest()
{
CountTestHelper< Item>();
}
我试图理解这个错误的含义或者解决方法,通过阅读 http://msdn.microsoft.com/en-us/library/d5x73970.aspx和 http://msdn.microsoft.com/en-us/library/x3y47hd4.aspx - 但它没有帮助。
我不明白这个错误 - 我已经将SomeClass限制为类型。我无法约束整个Test类(由Visual Studio生成的包含所有测试的单元测试类),否则我会得到一些其他错误。 Item类没有任何模板...
请帮我解决这个错误。谢谢。
Item i = new Item(p,10);
引用泛型类型参数 Item
of CountTestHelper
方法,而不是类 Item
。更改泛型参数名称例如:
public void CountTestHelper< TItem>()其中TItem:IHasRect
{
矩形矩形=新的矩形(0,0,100,100);
SomeClass< TItem> target = new SomeClass< TItem>(rectangle);
Point p = new Point(10,10);
Item i = new Item(p,10);
...
}
或者您可以完全符合 Item
要创建的类:
public void CountTestHelper< Item> ()其中Item:IHasRect
{
矩形矩形=新矩形(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);
}
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
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.aspx and http://msdn.microsoft.com/en-us/library/x3y47hd4.aspx - but it doesn't help.
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...
Please help me fix this error. Thank you.
The Item
in the line:
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.
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:
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);
}
这篇关于无法创建变量类型'Item'的实例,因为它没有new()约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!