必須是靜態類的靜態方法 參數必須使用this指定是哪種類型的拓展方法
public static class Common
{
/// <summary>
/// 轉換Object類型為string,null為空值
/// </summary>
/// <param name="val"></param>
/// <returns></returns>
public static string Conversion(object val)
{
if (val == DBNull.Value)
return "";
else if (val == null)
return "";
else
return val.ToString();
}
public static int ToInt(this object val)
{
int i = 0;
try
{
i = Convert.ToInt32(val);
}
catch{}
return i;
}
}
摘自 AhJesus