[英]How to implement generic collection lookup method that can return class or nullable struct?
我想要一個異類查找輔助函數用於異構集合:它應該返回一個結構或類,如果找不到該項,則返回null。 下面是一個使用普通集合查找的示例,但它可能是數據庫調用或其他任何內容。
有沒有辦法通過單一方法簽名實現這一目標?
public T GetClass<T>(string key) where T : class
{
object o;
if (Contents.TryGetValue(key, out o))
{
return o as T;
}
return null;
}
public T? GetStruct<T>(string key) where T : struct
{
object o;
if (Contents.TryGetValue(key, out o))
{
if (o is T)
{
return (T?) o;
}
}
return null;
}
我已經嘗試過的:
(Default) T
不是一個選項,因為0是一個有效的int值。 <int ?>
作為類型進行調用,但如上所述, Nullable<T>
不是引用類型。 有沒有辦法表明我要返回一個盒裝的int?
有沒有辦法通過單一方法簽名實現這一目標?
使用可選參數這是一種可怕的 (真正可怕的)方式,因此在兩種情況下調用代碼看起來都一樣。 但它很蹩腳。
選項:
Tuple<T, bool>
而不是使用nullity out
參數(如int.TryParse
等) 請注意,通過單獨表示缺少值,可以使null
成為有效的“找到”結果,這有時很有用。 或者您可能只想保證它永遠不會被退回。
如果你真的想使用nullity,我會選擇最后一個選項。 我相信它會讓你的代碼更清晰。 IMO,實際上只有當方法使用不同的參數表達完全相同的東西時才會使用重載 - 而在一種情況下返回Nullable<T>
作為返回類型而在另一種情況下返回T
作為返回類型不能真正看到那樣。
以下方法適用於類和可空結構:
public static T GetValue<T>(string key)
{
object o;
if (Contents.TryGetValue(key, out o))
{
if (o is T)
{
return (T)o;
}
}
return default(T);
}
用法:
int? result1 = GetValue<int?>("someInt");
string result2 = GetValue<string>("someString");
注意怎么樣?
是泛型類型參數的一部分,並且不是由返回類型上的方法定義的。
這應該完全符合你的需要。 如果請求的類型是可空類型,請在轉換之前檢查基礎類型。
public static T GetValue<T>(string key)
{
object o;
if (Contents.TryGetValue(key, out o))
{
if (o is T || Nullable.GetUnderlyingType(typeof(T)) == o.GetType())
{
return (T)o;
}
}
return default(T);
}
我的測試代碼:
Contents.Add("a string", "string value");
Contents.Add("an integer", 1);
Contents.Add("a nullable integer", new Nullable<int>(2));
// Get objects as the type we originally used.
Debug.WriteLine(string.Format("GetValue<string>(\"a string\") = {0}", GetValue<string>("a string")));
Debug.WriteLine(string.Format("GetValue<int>(\"an integer\") = {0}", GetValue<int>("an integer")));
Debug.WriteLine(string.Format("GetValue<int?>(\"a nullable integer\") = {0}", GetValue<int?>("a nullable integer")));
// Get objects as base class object.
Debug.WriteLine(string.Format("GetValue<object>(\"a string\") = {0}", GetValue<object>("a string")));
Debug.WriteLine(string.Format("GetValue<object>(\"an integer\") = {0}", GetValue<object>("an integer")));
Debug.WriteLine(string.Format("GetValue<object>(\"a nullable integer\") = {0}", GetValue<object>("a nullable integer")));
// Get the ints as the other type.
Debug.WriteLine(string.Format("GetValue<int?>(\"an integer\") = {0}", GetValue<int?>("an integer")));
Debug.WriteLine(string.Format("GetValue<int>(\"a nullable integer\") = {0}", GetValue<int>("a nullable integer")));
// Attempt to get as a struct that it's not, should return default value.
Debug.WriteLine(string.Format("GetValue<double>(\"a string\") = {0}", GetValue<double>("a string")));
// Attempt to get as a nullable struct that it's not, or as a class that it's not, should return null.
Debug.WriteLine(string.Format("GetValue<double?>(\"a string\") = {0}", GetValue<double?>("a string")));
Debug.WriteLine(string.Format("GetValue<StringBuilder>(\"a string\") = {0}", GetValue<StringBuilder>("a string")));
結果:
GetValue<string>("a string") = string value
GetValue<int>("an integer") = 1
GetValue<int?>("a nullable integer") = 2
GetValue<object>("a string") = string value
GetValue<object>("an integer") = 1
GetValue<object>("a nullable integer") = 2
GetValue<int?>("an integer") = 1
GetValue<int>("a nullable integer") = 2
GetValue<double>("a string") = 0
GetValue<double?>("a string") =
GetValue<StringBuilder>("a string") =
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.