我们有一个泛型类,它包含一个使用外部类的泛型类型参数的私有类。例如:
public class Foo<T>
{
private class Bar : IGrouping<Guid,T>{
}
}
问题是我们在单元测试项目中收到编译错误,该错误表明
“类型或方法有2个通用参数,但提供了1个通用参数。必须为每个通用参数提供通用参数。”I加速MSBuild详细程度(工具&gt;选项&gt;项目和解决方案&gt;构建和运行)。查看错误位置,它指向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
}
答案 0 :(得分:2)
因此私有访问器处理对于部分构造了泛型类定义的类型感到愤怒。例如,如果类声明如下,则失败:
private class LocalNameGrouping : IGrouping<StorageCategory, T> {
}
然而,它对此感到满意,仍然满足了需要(尽管有点俗气):
private class LocalNameGrouping<K> : IGrouping<K, T> where K: StorageCategory, new()
{
}
答案 1 :(得分:0)
不幸的是我无法重现这个问题。我使用下面的代码填写了您的示例,它在Visual Studio 2010中编译得很好。
public class StorageServiceBase<T> { }
public interface ICategoryStorageService<T> { }
public interface IStorageContent { }
public class StorageCategory { }
public interface IGrouping<T1, T2> : IEnumerable<T2> { }
public class CategoryStorageService<T> : StorageServiceBase<T>, ICategoryStorageService<T> where T : IStorageContent, new() {
public IEnumerable<IGrouping<StorageCategory, T>> GetContent(params StorageCategory[] categories) {
return null;
}
private IEnumerable<IGrouping<StorageCategory, T>> GetContent(params Guid[] categories) {
return null;
}
private class LocalNameGrouping : IGrouping<StorageCategory, T> {
private IEnumerable<T> items = null;
private StorageCategory category = null;
public LocalNameGrouping(StorageCategory localCategory, IGrouping<StorageCategory, T> items) {
}
#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
}
}