2012-01-07 81 views
4

我可以将IList转换成ArrayList如何将Ilist投射到ArrayList?

如果是,我该怎么办?

IList alls = RetrieveCourseStudents(cf); 
ArrayList a = (ArrayList)alls; 

这是正确的吗?

是有错误:

Unable to cast object of type

+7

什么类型不'RetrieveCourseStudents'返回?你为什么需要将它转换为'ArrayList'?你不应该在新代码中使用'ArrayList',而是使用泛型集合。 – Oded 2012-01-07 15:01:20

+0

public EntityCollection RetrieveCourseStudents(CF cf); – 2012-01-07 15:29:06

+0

你为什么要把它转换成'ArrayList'? – Oded 2012-01-07 15:30:13

回答

0

这是所有关于多态性。 ArrayList是来自接口IList的实现。

IList iList = new ArrayList(); 

从变量ILIST静态类型为IList的但它引用了一个ArrayList对象!

没有从IList到ArrayList的真实转换,因为您无法从接口或抽象类实例化/创建对象。

4

正如意见建议,你应该考虑使用泛型集合而不是

List<Student> students = RetrieveCourseStudents(cf).Cast<Student>().ToList() 
+0

Tanx非常多.... – 2012-01-07 15:30:16

4

你只能将投allsArrayList如果它已经是ArrayList,即如果由RetrieveCourseStudents返回的对象是ArrayList

如果没有,那么你需要创建一个新的对象,luckly ArrayListconstructor可以做到这一点:new ArrayList(RetrieveCourseStudents(cf))


值得一提的是,你应该使用泛型(如List<T>)而不是现在的ArrayList,所以除非你需要与一些无法更新的旧代码进行交互,否则我会远离它。

0

是, 只有当RetrieveCourseStudents(cf)返回Arraylist类型时,我们才能将IList转换为ArrayList。

例如

static void Main(string[] args) 
     { 
      IList test1 = GetList(); 
      IList test2= GetIList(); 
      ArrayList list1 = (ArrayList)test1; // Fails 
      ArrayList list2 = (ArrayList)test2; // Passes 

      Console.ReadKey(); 
     } 

     private static IList GetIList() 
     { 
      return new ArrayList(); 
     } 

     private static IList GetList() 
     { 
      return new CustomList(); 
     } 
0

既然您评论说您只是想要订购返回的列表(在另一个评论中说您的类型为EntityCollection<CourseStudent>) - 您无需将其转换为ArrayList,只需直接使用该值即可。

您可以使用OrderBy LINQ扩展方法(以及您使用的变量类型 - IList也不合适)。

这会工作为您的需求(其中CourseStudentPropertyCourseStudent属性):

var alls = RetrieveCourseStudents(cf); 
var orderedAlls = alls.OrderBy(cs => cs.CourseStudentProperty); 
0
using System; 
using System.Collections; 
using System.Collections.Generic; 
namespace MyILists 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 
     IList<int> intArrayList = new ArrayList().ToIList<int>(); 
     intArrayList.Add(1); 
     intArrayList.Add(2); 
     //intArrayList.Add("Sample Text"); // Will not compile 

     foreach (int myInt in intArrayList) 
     { 
      Console.WriteLine(" Number : " + myInt.ToString()); 
     } 
     Console.Read(); 
    }  
} 

public static class MyExtensions 
{ 
    public static IList<T> ToIList<T>(this ArrayList arrayList) 
    { 
     IList<T> list = new List<T>(arrayList.Count); 
     foreach (T instance in arrayList) 
     { 
      list.Add(instance); 
     } 
     return list; 
    } 

} 
} 
+0

感谢您发布一个答案!虽然代码片段可以回答这个问题,但添加一些附加信息仍然很棒,比如解释等。 – j0k 2012-10-04 16:14:50

0

只是用这个简单的代码:)

(From x In list).ToArray 
0

你可以使用LINQ联盟延期。

请注意,您可以将任何类型的IEnumerable与此技术(Array,IList等)结合使用,因此您无需担心“添加”方法。你必须明白,LINQ产生了不可变的结果,所以如果你想随后操作集合,那么你需要使用“ToList()”,“ToDictionary()”或其他。

var list = 
     (IList<Student>) new [] 
     { 
      new Student {FirstName = "Jane"}, 
      new Student {FirstName = "Bill"}, 
     }; 

    var allStudents = list.Union(
     new [] {new Student {FirstName = "Clancey"}}) 
      .OrderBy(s => s.FirstName).ToList(); 

    allStudents[0].FirstName = "Billy"; 

    foreach (var s in allStudents) 
    { 
     Console.WriteLine("FirstName = {0}", s.FirstName); 
    } 

输出:

FirstName = Billy 
FirstName = Clancey 
FirstName = Jane