无法转换IEnumerable类型列出 [英] Cannot convert type IEnumerable to List

查看:388
本文介绍了无法转换IEnumerable类型列出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直使用C#与Unity3d了几年了,但我只是用.NET编程开始。我得到的错误:



无法隐式转换类型 System.Collections.Generic.IEnumerable< URL> System.Collections.Generic.List< URL> 。一个显式转换存在(是否缺少强制转换?)



下面是我的代码:

 命名空间TestBrowserHistory 
{
公共类的Test1
{
公共Test1的()
{

}
上配置静态无效的主要()
{
的InternetExplorer = myClass的新的InternetExplorer();
名单,LT; URL> calledList = myClass.GetHistory();
Console.WriteLine(你好!);
Console.WriteLine(calledList [1]);
到Console.ReadLine();
}
}
}

公共类的InternetExplorer
{
// URL对象的列表
公开名单< URL>网址{搞定;组; }
公开的IEnumerable< URL> GetHistory()
{
//初始化主要目标
UrlHistoryWrapperClass urlhistory =新UrlHistoryWrapperClass();
$ B $历史上的
UrlHistoryWrapperClass.STATURLEnumerator枚举=
urlhistory.GetEnumerator b //枚举的URL();

//迭代通过枚举
,而(enumerator.MoveNext())
{
//获取URL和标题
字符串URL =枚举。 Current.URL.Replace(\'','');
//在标题,消除单引号,以避免混乱
字符串标题= string.IsNullOrEmpty(enumerator.Current.Title)
? enumerator.Current.Title.Replace('\'',''):;

//创建新条目
URL U =新的URL(URL,标题的Internet Explorer);

//添加条目列出
URLs.Add(U);
}

//可选
enumerator.Reset();

// URL清除历史记录
urlhistory.ClearHistory();

返回的URL;
}

}



感谢您的帮助!


解决方案

您得到这个错误,因为 myClass.GetHistory(); 收益的IEnumerable< URL> ,这是不一样的列表< URL> 在编译的时候,虽然它实际上是列表与LT; URL> 。更改方法签名返回列表< URL> ,因为你已经这样做了。



 公开名单< URL> GetHistory()



其他解决方法是投方法调用结果列表<网址>

 列表< URL> calledList =(列表< URL>)myClass.GetHistory(); 

或从结果



<$ P构造新的列表$ p> 列表< URL> calledList =新的List< URL>(myClass.GetHistory());

如果您不需要列表的功能,你可以定义 calledList 为IEnumerable

  VAR calledList = myClass.GetHistory(); 


I have been using C# with Unity3d for a few years now, but am just starting with .NET programming. I get the error:

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<URL>' to 'System.Collections.Generic.List<URL>'. An explicit conversion exists (are you missing a cast?)

Here is my code:

namespace TestBrowserHistory
{
    public class Test1
    {
        public Test1()
        {

        }
          static void Main()
    {
        InternetExplorer myClass = new InternetExplorer();
        List<URL> calledList = myClass.GetHistory();
        Console.WriteLine("Hello!");
        Console.WriteLine(calledList[1]);
        Console.ReadLine();
    }
    }
}

public class InternetExplorer
{
    // List of URL objects
    public List<URL> URLs { get; set; }
    public IEnumerable<URL> GetHistory()
    {
        // Initiate main object
        UrlHistoryWrapperClass urlhistory = new UrlHistoryWrapperClass();

        // Enumerate URLs in History
        UrlHistoryWrapperClass.STATURLEnumerator enumerator =
                                           urlhistory.GetEnumerator();

        // Iterate through the enumeration
        while (enumerator.MoveNext())
        {
            // Obtain URL and Title
            string url = enumerator.Current.URL.Replace('\'', ' ');
            // In the title, eliminate single quotes to avoid confusion
            string title = string.IsNullOrEmpty(enumerator.Current.Title)
                      ? enumerator.Current.Title.Replace('\'', ' ') : "";

            // Create new entry
            URL U = new URL(url, title, "Internet Explorer");

            // Add entry to list
            URLs.Add(U);
        }

        // Optional
        enumerator.Reset();

        // Clear URL History
        urlhistory.ClearHistory();

        return URLs;
    }

}

Thanks for any help!

解决方案

You get that error because myClass.GetHistory(); returns IEnumerable<URL>, which is not same as List<URL> at compile time, although it is actually List<URL> at runtime. Change method signature to return List<URL>, cause you already do that

public List<URL> GetHistory()

Other workarounds would be to cast method call result to List<URL>

List<URL> calledList = (List<URL>)myClass.GetHistory();

Or construct new list from result

List<URL> calledList = new List<URL>(myClass.GetHistory());

If you do not need List functionality, you could define calledList as IEnumerable

var calledList = myClass.GetHistory();

这篇关于无法转换IEnumerable类型列出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆