Visual sutdio 2008能够自动创建单元测试存根。我用它来创建一些基本的单元测试,但我的东西迷惑:C#泛型铸造
private class bla : BaseStoreItem
{
//
}
/// <summary>
///A test for StoreData
///</summary>
public void StoreDataTestHelper<T>() where T : BaseStoreItem
{
FileStore<T> target = new FileStore<T>(); // TODO: Initialize to an appropriate value
BaseStoreItem data = new bla();
target.StoreData(data);
}
[TestMethod()]
public void StoreDataTest()
{
//Assert.Inconclusive("No appropriate type parameter is found to satisfies the type constraint(s) of T. " +
// "Please call StoreDataTestHelper<T>() with appropriate type parameters.");
StoreDataTestHelper<bla>();
}
为什么我会收到“错误:无法转换类型‘StorageUnitTests.FileStoreTest.bla’到‘T’”当T键入“bla”?
我知道“bla”不是一个好的函数名,但它只是一个例子。
我认为它应该是Activator.CreateInstance()而不是新的T().. –
2010-03-26 10:01:19
@Akash为什么? Activator.CreateInstance使用反射,它很慢。它认为新的T()更优雅。 – gsharp 2010-03-26 10:11:59
gsharp,对不起,我错过了,在你的代码中的new()约束,因为没有指定new() – 2010-03-26 10:19:13