什么是GenericParameterHelper,它如何使用?

12

我在VS 2008中为一个泛型类生成了单元测试,它在所有使用泛型类型的地方都使用了GenericParameterHelper类型。这是一个应该被替换的占位符吗?还是它有一些用途?如果有用途,它们是什么?

以下是其中一个生成的测试作为示例:

/// <summary>
///A test for Count
///</summary>
public void CountTestHelper<TKey, TValue>()
{
    ObservableDictionary<TKey, TValue> target = new ObservableDictionary<TKey, TValue>(); // TODO: Initialize to an appropriate value
    int actual;
    actual = target.Count;
    Assert.Inconclusive("Verify the correctness of this test method.");
}

[TestMethod()]
public void CountTest()
{
    CountTestHelper<GenericParameterHelper, GenericParameterHelper>();
}
2个回答

8

假设您有一个类:

public class Foo<T>
{
    public bool DoSomething()
    {
        return false;
    }

    public T DoSomethingElse()
    {
    // ...
}

现在您想测试DoSomething。首先,您必须实例化Foo。您不能这样做:

var foo = new Foo<T>();

你需要使用真实类型。但是在该方法中并未使用T,因此它会干扰测试。所以你可以这样做:

var foo = new Foo<GenericParameterHelper>();

...这个词或多或少代表“任何旧类型”。


1
应该叫它“AnyType”,或者其他什么名称。这样会短得多 :P - Svish
所以它们基本上是占位符?好的,知道了。 - Bryan Anderson
这个类是用于占位泛型类型的。它能够帮助您通过传递非特定类型参数来测试泛型类型。 - Craig Stuntz
它还添加了Data属性,所以我认为它可能对某些事情有用。 - Bryan Anderson

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接