2009-07-17 198 views
1

我的方法:C#不能隱式轉換類型T爲T類型

public TableFilled<TKey, TRow> getTera() 
    { 

     Func<TablesFilled<TKey,TRow>> _getTera=new Func<TablesFilled<TKey,TRow>>(
     ()=>{return (TablesFilled<TKey,TRow>) chGetTera();}); 

     //Above does not compile says: Cannot convert type 
     //'AcapsVerify.FunctionalTables.TableFilled<TKey,TRow>' to 
     //'AcapsVerify.FunctionalTables.TablesFilled<TKey,TRow>' 
     // the line below has the same blue underline error. 
     return _getTera.TimeAndReport("Finished Teradata",OutputIfListener); 

     // this works fine 
     return chGetTera; 
    } 

靜態方法調用

public static T TimeAndReport<T>(this Func<T> timedFunc, String reportLead, Action<String> reporterAction) 
    { 
     T result; 
     var s = new System.Diagnostics.Stopwatch(); 
     s.Start(); 
     result = timedFunc(); 
     s.Stop(); 
     reporterAction(reportLead + " in " + s.WholePartOnly()); 
     return result; 
    } 

回報類的定義:

public class TableFilled<TKey,TRow> where TRow: STeraRow<TKey> 
+0

要把它放到更多的情況下,我正在做一個功能表比較程序,其中一個是主表,一個是複製。在功能上,填充表與構建它的類不同。我想讓所有的餐桌工廠報告,如果有人在傾聽,他們會花多長時間。 – Maslow 2009-07-17 19:52:36

回答

3

那麼TableFilled類型和TablesFilled類型有什麼區別?您的返回類型中有一個錯字,或者兩種類型之間沒有隱式轉換。

0

我相信差異來來自並非所有具有where TRow : STeraRow<TKey>指定的項目。

+0

即使擴展方法也必須具有相同的限制嗎? – Maslow 2009-07-17 19:50:23

+0

顯然不是,它現在正在編譯我修復了類型錯別字 – Maslow 2009-07-17 20:00:09

0

問題是Func鍵應該已經返回,而不是FilledTables

abstract protected TableFilled<TKey, TRow> chGetTera(); 

    public TableFilled<TKey, TRow> getTera() 
    { 

     Func<TableFilled<TKey,TRow>> _getTera=new Func<TableFilled<TKey,TRow>>(
     ()=>{return chGetTera();}); 

return _getTera.TimeAndReport("Finished Teradata",OutputIfListener); 

//return chGetTera(); 

    } 
4

您可以通過使用對象類型轉換一個FilledTable。

return (T)(object)(result); 

對我來說,它的工作原理。

0

Convert.ChangeType應該是你的問題的解決方案:

(T) Convert.ChangeType(yourObject, typeof(T)); 
相關問題