当前位置:编程学习 > C#/ASP.NET >>

C#的拓展方法

必须是静态类的静态方法 参数必须使用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
 
补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,