简体   繁体   English

在.NET4.5下使用ArrayList进行转换

[英]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.

相关问题 升级到.net4.5后的多个引用 - Multiple references after upgrading to .net4.5 如何使用HttpClient解决与.Net4.0与.Net4.5中的Uri和编码URL的差异 - How-to workaround differences with Uri and encoded URLs in .Net4.0 vs .Net4.5 using HttpClient .NET4.5将组合框绑定到字典问题。 WPF - .NET4.5 Binding a Combobox to a Dictionary Issues. WPF C#.net4.5解包Zip文件 - C# .net4.5 Unpack Zip file 为UAP和.NET4.5编译相同的C#代码 - Compile identical C# code for UAP and .NET4.5 .NET4.5 中异步 POST 方法的 HttpResponseMessage 异常 - Exception in HttpResponseMessage for async POST method in .NET4.5 从.net4.5转换为.net3.5后,NTLM身份验证不起作用 - NTLM authentication not working after convertion to .net3.5 from .net4.5 为什么在Windows7(.net 3.5)中运行的应用程序无法在Win8(.net4.5)中运行 - Why an application working in Windows7(.net 3.5)is not working in Win8(.net4.5) 忽略EF6代码优先映射中的某些继承属性(.NET4而不是.NET4.5) - Ignore some inherited properties in EF6 code first mapping(.NET4 not .NET4.5) 面向.NET4.5,SL和Windows Store应用的MEF for Portable Library Class - MEF for Portable Library Class targeting .NET4.5, SL and Windows Store apps
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM