2016-02-28 90 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