我们有一个泛型类,它包含一个使用外部类的泛型类型参数的私有类.例:
public class Foo<T>
{
private class Bar : IGrouping<Guid,T>{
}
}
问题是我们在单元测试项目中遇到编译错误
“The Type or method has 2 generic parameter(s), but 1 generic argument(s) were provided. A generic argument must be provided for each generic parameter.”
我加快了MSBuild的详细程度(工具>选项>项目和解决方案>构建和运行).查看错误位置,它指向Microsoft.TeamTest.targets文件.我能够将它缩小到上面的这个类结构.如果我用类定义中的具体泛型参数替换私有类Bar(见下文),单元测试编译正常:
private class Bar : IGrouping<Guid,int>{
}
我需要提供第二个泛型参数generic来接受outter类中的任何类型.关于这个的想法?
为了澄清,编译错误在单元测试项目中,而不是上面列出的代码(编译正常).私有访问器代可能在MS单元测试基础设施中窒息?
这是[几乎]整个实际代码:
public class CategoryStorageService<T> : StorageServiceBase<T>, ICategoryStorageService<T> where T : IStorageContent, new()
{
/// <summary>
/// Returns a nested object graph of category-items
/// </summary>
/// <param name="categories"></param>
/// <returns></returns>
public IEnumerable<IGrouping<StorageCategory, T>> GetContent(params StorageCategory[] categories)
{
var results = this.GetContent(categories.Select(c => c.ID).ToArray());
foreach (var result in results)
{
yield return new LocalNameGrouping(
categories.First(c => c.ID == result.Key.ID),
result);
}
}
/// <summary>
/// Returns a nested object graph of categories, each containing their items
/// </summary>
/// <param name="categories">The ID(s) of the category(ies) to fetch</param>
/// <returns></returns>
private IEnumerable<IGrouping<StorageCategory, T>> GetContent(params Guid[] categories)
{
// implementation removed
return null;
}
private class LocalNameGrouping : IGrouping<StorageCategory, T>
{
private IEnumerable<T> items;
private StorageCategory category;
public LocalNameGrouping(StorageCategory localCategory, IGrouping<StorageCategory, T> items)
{
this.items = items;
string name = string.IsNullOrEmpty(localCategory.DisplayName) ? items.Key.DisplayName : localCategory.DisplayName;
this.category = new StorageCategory { DisplayName = name, ID = items.Key.ID };
}
#region IGrouping<StorageCategory,T> Members
public StorageCategory Key
{
get { return category; }
}
#endregion
#region IEnumerable<T> Members
public IEnumerator<T> GetEnumerator()
{
return items.GetEnumerator();
}
#endregion
#region IEnumerable Members
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
#endregion
}
最佳答案 因此私有访问器处理对于部分构造泛型类定义的类型感到愤怒.例如,如果类声明如下,则失败:
private class LocalNameGrouping : IGrouping<StorageCategory, T> {
}
然而,它对此很满意,仍然满足了需要(尽管有点俗气):
private class LocalNameGrouping<K> : IGrouping<K, T> where K: StorageCategory, new()
{
}