简体   繁体   English

通用方法-类型不能用作类型参数

[英]Generic method - Type cannot be used as Type Parameter

Given the following Generic Method: 给定以下通用方法:

    /// <summary>
    /// Performs a MoveNext on the IEnumerator 'Enoomerator'. 
    /// If it hits the end of the enumerable list, it resets to the beginning.
    /// </summary>
    /// <returns>If False, MoveNext has failed even after resetting,
    /// which means there are no entries in the list.</returns>
    private static bool CyclicalSafeMoveNext<T>(T Enoomerator) 
                                           where T : IEnumerator<T> 
    {
        if (Enoomerator.MoveNext()) //  successfully moved to the next element
        {
            return true;
        }
        else
        {
            // Either reached last element (so reset to beginning) or
            // trying to enumerate in a list with no entries 
            LogLine("CyclicalSafeMoveNext: failed a MoveNext, resetting.");
            Enoomerator.Reset();

            if (!Enoomerator.MoveNext())
            {
                // Still at end. Zero entries in the list?
                LogLine("CyclicalSafeMoveNext: failed a MoveNext after 
                Reset(), which means there were no entries in the list.");
                return false;
            }
            else
            {
                // Resetting was successful
                return true;
            }
        }
    }

When I compile this code 当我编译这段代码

IEnumerator<FileInfo> FileInfoEnumerator files;                               
while (CyclicalSafeMoveNext(files))
{
    return files.Current.FullName;
}

I get the error: 我得到错误:

Error 7 The type 'System.Collections.Generic.IEnumerator<System.IO.FileInfo>' cannot 
be used as type parameter 'T' in the generic type or method
'CyclicalSafeMoveNext<T>(T)'. There is no implicit reference conversion from
'System.Collections.Generic.IEnumerator<System.IO.FileInfo>' to 'System.Collections.Generic.IEnumerator<System.Collections.Generic.IEnumerator<System.IO.FileInfo>>'.

Why am I getting this error and how do I correct my code? 为什么会出现此错误,以及如何更正我的代码?

One problem is here: 一个问题在这里:

where T : IEnumerator<T> 

You're restricting the generic class to a class that is its own enumerator . 您将泛型类限制为自己的枚举器类

Since FileInfo is not an IEnumerator<FileInfo> and IEnumerator<FileInfo> is not an IEnumerator<IEnumerator<FileInfo>> it fails the generic constraint. 由于FileInfo不是IEnumerator<FileInfo>并且IEnumerator<FileInfo>不是IEnumerator<IEnumerator<FileInfo>>因此它无法通过通用约束。

You could add a second generic type: 可以添加第二个泛型类型:

private static bool CyclicalSafeMoveNext<T, U>(T Enoomerator) 
                                       where T : IEnumerator<U>

or just make IEnumerator<T> part of the signature: 或者只是将IEnumerator<T>设置为签名的一部分:

private static bool CyclicalSafeMoveNext<T>(IEnumerator<T> Enoomerator) 

try 尝试

private static bool CyclicalSafeMoveNext<T>(IEnumerator<T> Enoomerator)

having the where T : IEnumerator<T> is throwing it off. 具有where T : IEnumerator<T>正在抛出它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 该类型不能用作通用类型或方法中的类型参数 - The type cannot be used as type parameter in the generic type or method 类型&#39;&#39;不能在通用类型或方法中用作类型参数&#39;T&#39; - The type '' cannot be used as type parameter 'T' in the generic type or method 类型“ XXX”不能用作通用类型或方法中的类型参数“ T” - The type 'XXX' cannot be used as type parameter 'T' in the generic type or method 该类型不能用作泛型类型或方法UserStore中的类型参数TRole - The type cannot be used as type parameter TRole in the generic type or method UserStore 类型“ T”不能用作通用类型或方法中的类型参数“ T” - The type 'T' cannot be used as type parameter 'T' in the generic type or method 存储库不能用作方法的通用类型中的类型参数 - Repository cannot be used as a type parameter in the generic type of method 不能在泛型类型或方法中用作类型参数“TElement” - Cannot be used as type parameter 'TElement' in the generic type or method 当类型已知时,类型&#39;T&#39;不能用作泛型类型或方法错误中的类型参数 - The type 'T' cannot be used as type parameter in the generic type or method error when type is known Xamarin / Realm-该类型不能用作通用类型或方法中的类型参数“ T” - Xamarin/Realm - The type cannot be used as type parameter 'T' in the generic type or method 类型&#39;System.Windows.Forms.GroupBox&#39;不能在泛型类型或方法中用作类型参数&#39;T&#39; - The type 'System.Windows.Forms.GroupBox' cannot be used as type parameter 'T' in the generic type or method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM