2011-10-07 105 views
1

我有這樣的代碼:錯誤消息:「無法轉換類型'字符串?'以「串」「

//Return null if the extension doesn't have the value, returns the value if it does. 
private T? getValue<T>(IEnumerable<Extension> extension, string attributeName) 
{ 
    IEnumerable<Extension> ext = extension.Where(e => e.attributeName == attributeName); 
    if (ext.Count() > 0) 
    { 
     return (T)ext.First().Attribute; 
    } 
    return null; 
} 

我打電話是這樣的:

//This works: 
retVal.byteValue= getValueFromExtension<byte>(u, "ByteToGet") ?? 0; 
//This doesn't work: 
getValueFromExtension<string>(u, "Text") ?? ""; 

我得到的編譯錯誤:」錯誤消息:「無法將類型「字符串? 「以「串」「

我怎樣纔能有效地做好代碼的理念之上,而無需創建一個新的方法?

我覺得我檢查,如果它是空與??運營商,因此,如果字符串爲空,它將總是被設置爲一個空字符串,它是如何處理字節和整數,爲什麼不是字符串?

上面的字節值,是字節類型,不是字節?

+2

這段代碼編譯過的?我很困惑'T?'方法的返回類型 – sll

+2

什麼是'getValueFromExtension'? – Oded

+4

我不明白爲什麼你的方法甚至編譯沒有'struct'約束。 – CodesInChaos

回答

6

看來你想null如果是引用類型和0,如果它是一個數字或類似的值類型。您可以簡單地使用default關鍵字從T獲取此類值。另外,您可能需要將this關鍵字添加到第一個參數,以便它可以用作擴展方法。

private T getValue<T>(this IEnumerable<Extension> extension, string attributeName) 
{ 
    Extension ext = extension.SingleOrDefault(e => e.attributeName == attributeName); 

    if (ext != null) 
     return (T)ext.Attribute; 
    else 
     return default(T); 
} 
+1

如果你需要值類型的自定義默認值,你仍然可以使用類似的東西(注意「** byte?**」): retVal.byteValue = getValueFromExtension (u,「ByteToGet」)? 42;但在這種情況下,將默認值傳遞給getValue方法作爲更多參數更合理 – SergGr

+0

@SergGr:非常好,沒有想到這一點。我只能想到爲這種方法增加另一個重載。 –

+0

StackOverflow非常棒,每個答案都在這裏幫助我,花了大約5分鐘纔得到完整的答案。多謝你們。 – Dale

3

T?表示Nullable<T>,這是一個限於結構的東西。字符串不是結構體,因此不適用於接受或返回T?的方法。

不幸的是,如果你想返回null值類型以及類(如字符串),那麼你就不能支持用一個通用的方法。您需要按照Allon的建議並返回default(T),這將而不是爲null(非Nullable<T>)結構爲空,或者定義兩個方法具有不同簽名,一個用於結構,一個用於類。

private T getValueForClass<T>(IEnumerable<Extension> extension, string attributeName) 
    where T : class 

private T? getValueForStruct<T>(IEnumerable<Extension> extension, string attributeName) 
    where T : struct 

... 

var theByte = getValueForStruct<byte>(extensions, "ByteToGet") ?? 0; 
var theString = getValueForClass<string>(extensions, "Text") ?? ""; 
+0

有道理,謝謝。我沒有想過這個。 – Dale

+0

你的回答很好,我只能接受其中之一作爲答案,所以我接受了其他實際使用的內容。 – Dale

+0

@ user788402,很好!始終接受對您有用的人。投幣翻轉也是可以接受的。 –

2

您不能有空字符串。 Nullable的類型參數被約束爲一個值類型,而String是一個引用類型。你getValue方法返回一個可爲空T-你需要將其約束到結構,並使用不同的方法類:

//Return null if the extension doesn't have the value, returns the value if it does. 
private T? getValue<T>(IEnumerable<Extension> extension, string attributeName) where T : struct 
{ 
    IEnumerable<Extension> ext = extension.Where(e => e.attributeName == attributeName); 
    if (ext.Count() > 0) 
    { 
     return (T)ext.First().Attribute; 
    } 
    return null; 
} 

//Return null if the extension doesn't have the value, returns the value if it does. 
private T getValueObject<T>(IEnumerable<Extension> extension, string attributeName) where T : class 
{ 
    IEnumerable<Extension> ext = extension.Where(e => e.attributeName == attributeName); 
    if (ext.Count() > 0) 
    { 
     return (T)ext.First().Attribute; 
    } 
    return null; 
} 

然後

//This works: 
getValue<Byte>(u, "ByteToGet") ?? 0; 

//This also works: 
getValueObject<String>(u, "Text") ?? String.Empty; 
相關問題