.net - 什么是 GenericParameterHelper,它是如何使用的?

标签 .net

我在 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>();
}

最佳答案

假设你有一个类:

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>();

...它或多或少代表“任何旧类型”。

关于.net - 什么是 GenericParameterHelper,它是如何使用的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1639968/

相关文章:

c# - RavenDB:返回属性为空值的对象

c# - 如何验证证书是由特定证书颁发机构创建的?

javascript - CSV 文件作为单个字符串返回

c# - .net 不受信任代码的新进程沙箱

c# - 如何向我的 backgroundworker dowork 事件添加耗时的进度?

c# - 进行回滚 - 存储库集成测试

.NET Web 服务 (asmx) 超时问题

c# - 组合框 : selected value stays in focus (blue)

c# - 验证摘要和错误的自定义格式

.net - 无法应用发布属性?