-2
在C#上工作,在泛型中不適用於值類型(如int),希望所有數據類型的擴展方法如:int,long,string.conversion方法是波紋管通用如何將一種類型的數據轉換爲另一種類型的數據
public static T Translate<T>(this T inputNumber) where T : class
{
return inputNumber.ToString()
.Replace("1", "1")
.Replace("2", "2")
.Replace("3", "3")
.Replace("4", "4")
.Replace("5", "5")
.Replace("6", "6")
.Replace("7", "7")
.Replace("8", "8")
.Replace("9", "9")
.Replace("0", "0") as T;
}
var result= 2.Translate<longTypeData>();
希望結果變量長類型的數據。
@TimSchmelter他們似乎是不同的Unicode字符 –
@RenéVogt,你是對的。 – shamim
我不認爲這是可能的,因爲如果沒有'class'約束,你不能使用'as T'。 – Kamo