[英]Casting Using ArrayList Under .NET4.5
All I have a utility method that is defined as 我所有的实用方法都定义为
public static Dictionary<T, int> CountOccurences<T>(IEnumerable<T> items) { ... }
I have some legacy code that unfortunately uses ArrayList
s instead of List<T>
. 我有一些遗留代码,遗憾的是使用
ArrayList
而不是List<T>
。 Now, I need to cast the ArrayList
to use the above method and both of the following should work 现在,我需要转换
ArrayList
以使用上面的方法,以下两个都应该工作
var v = CountOccurences<String>(arrayList.Cast<String>().ToArray());
or 要么
var v = CountOccurences<String>(arrayList.OfType<String>().ToArray());
Neither of these work in VS2012 under .NET 4.5 giving 这些都不适用于.NET 4.5中的VS2012
'System.Collections.ArrayList' does not contain a definition for 'OfType' and no extension method 'OfType' accepting a first argument of type 'System.Collections.ArrayList' could be found (are you missing a using directive or an assembly reference?)
'System.Collections.ArrayList'不包含'OfType'的定义,并且没有可以找到接受类型'System.Collections.ArrayList'的第一个参数的扩展方法'OfType'(您是否缺少using指令或程序集引用?)
However, I have tested this in LINQpad and they both work. 但是,我已经在LINQpad中对它进行了测试,它们都可以工作。 Why can't I cast my
ArrayList
? 为什么我不能投射我的
ArrayList
?
Thanks for your time. 谢谢你的时间。
The following works fine for me in VS2012 以下在VS2012中对我来说很好
ArrayList al = new ArrayList();
al.Add("a");
al.Add("b");
al.Add("c");
var v = al.OfType<string>().ToArray();
var list = new List<string>(v); //Constructor taking an IEnumerable<string>();
What error message are you receiving. 您收到了什么错误消息。
Make sure you are including the following namespaces 确保包含以下命名空间
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
I assume that "not work" means that you get an InvalidCastException
. 我认为“不工作”意味着你得到一个
InvalidCastException
。 So not all objects in the ArrayList
are strings. 因此,并非
ArrayList
中的所有对象都是字符串。 You could circumvent the problem by creating a new non-generic overload of CountOccurences
which takes an ArrayList
: 你可以通过创建一个新的非泛型的
CountOccurences
重载来CountOccurences
这个问题,它需要一个ArrayList
:
(presuming the functionality of the method) (假设方法的功能)
public static Dictionary<string, int> CountOccurences(ArrayList items)
{
var dict = new Dictionary<string, int>();
foreach(object t in items)
{
string key = "";
if(t != null)
key = t.ToString();
int count;
dict.TryGetValue(key, out count);
dict[key] = count++;
}
return dict;
}
Doesn't throw any errors in my case: 在我的情况下不会抛出任何错误:
BTW, your usage of OfType<T>
is wrong. 顺便说一下,你对
OfType<T>
是错误的。 It's a method, so append ()
. 这是一个方法,所以append
()
。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _16853758
{
class Program
{
static void Main(string[] args)
{
ArrayList arrayList = new ArrayList();
var a = CountOccurences<String>(arrayList.Cast<String>().ToArray());
var v = CountOccurences<String>(arrayList.OfType<String>().ToArray());
}
public static Dictionary<T, int> CountOccurences<T>(IEnumerable<T> items) { return new Dictionary<T, int>(); }
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.