2016-02-28 89 views
-1

我想学习如何使用扩展方法,并创建了我自己的。现在我试图运行我的代码,但Visual Studio给了我一个错误,我有一个未处理的InvalidCastException,所以我处理它并试图运行它。扩展方法返回InvalidCastException

我不得不在catch块中返回null,所以我还有另一个未处理的异常,并将其打印出来。

现在,当我尝试运行此代码,我输出

InvalidCastException NullReferenceException

Generic conversion method throw InvalidCastException试过解决方案,发现这里加入(动态)的演员阵容,同样的结果。

注意,我有一个Java的背景下,不C#

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace ConsoleApplication4 
{ 

class Program 
{ 
    static void Main(string[] args) 
    { 
     Reeks r = new Reeks(); 
     IEnumerable<int> selectie = r.TakeRange(10, 1000); 
     try 
     { 
      foreach (int i in selectie) 
      { 
       Console.WriteLine("selectie: {0}", i); 
      } 
     } 
     catch (NullReferenceException) 
     { 
      Console.WriteLine("NullReferenceException"); 
     } 
     Console.ReadLine(); 

    } 
} 

static class MyExtension 
{ 
    public static Reeks TakeRange(this Reeks r, int start, int end) 
    { 
     try 
     { 
      return (Reeks)r.Where(i => i > start && i < end); 
     } 
     catch (InvalidCastException) { 
      Console.WriteLine("InvalidCast"); return null; 
     } 
    } 
} 


public class Reeks : IEnumerable<int> 
{ 

    public Reeks() 
    { 

    } 

    public IEnumerator<int> GetEnumerator() 
    { 
     int start = 2; 
     yield return start; 
     for (; ;) 
     { 
      int nieuw = start * 2; 
      yield return nieuw; 
      start = nieuw; 

     } 

    } 

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() 
    { 
     return this.GetEnumerator(); 
    } 


} 


} 
+0

在哪一行?你调试了你的代码吗? –

+0

在这两个try-bodies上,开始于'return(Reeks)r.Where(i => i> start && i Ivaro18

+1

'reeks.Where'不是'Reeks'的类型。 'Reeks'是'IEnumerable '但每个'IEnumerable '不是'Reeks'。 –

回答

1

你应该改变你的静态方法,所以它返回一个IEnumerable <int>,看看这个:

static class MyReeksExtension { 
     public static IEnumerable <int> TakeRange(this IEnumerable<int> r, int start, int end) { 
      return r.Where(i => i > start && i < end); 
     } 
    } 

确保您的“selectie”就是这种类型也:

IEnumerable<int> selectie = r.TakeRange(10, 1000); 

     foreach (int n in selectie) 
      Console.Write("{0}; ", n); 

没问题,我也得找到帮助:P

-1

你应该以下行更改 return (Reeks)r.Where(i => i > start && i < end);return (Reeks)(r.Where(i => i > start && i < end).ToList().AsEnumerable());

where子句返回应转换为一个枚举一个列表。此外,您可以尝试使用linq中的skiptake替换上述代码片段。

+0

无法将列表转换为Reeks – Ivaro18

+0

@ Ivaro18,似乎您有无限循环,请检查代码 – Saravanan

+0

这完全不起作用。因为这个问题是完全错误的。两个'IEnumberable '不一样 –

1

您铸造Where调用的返回值在try块键入Reeks

return (Reeks)r.Where(i => i > start && i < end); 

然而,没有任何地方叫Where实际返回Reeks类型的对象方法。该代码调用Enumerable.Where,它返回某种IEnumerable实现,但绝对不是您自己的类型。

您必须实现一个名为Where的新(扩展名或实例)方法,该方法可以在Reeks对象上调用,并返回Reeks对象。或者你可以简单地接受Where不返回Reeks这一事实,而只是期望IEnumerable<int>

+0

但是Reeks实现IEnumerable 不会解决这个问题吗?否则我会重写Where Where – Ivaro18

+1

在Java中,'ArrayList '也是一个'List ',但是您仍然不能将它转换为'LinkedList ',只是因为它们实现了相同的接口。 – Wormbo