这里将介绍常用自定义C#类型转换函数,大家经常碰到类弄转换,但都不知道哪些系统函数才可以转换。
-
-
-
-
-
-
- public static int IntParse(string objValue, int defaultValue)
- {
- int returnValue = defaultValue;
- if (!string.IsNullOrEmpty(objValue))
- {
- try
- {
- returnValue = int.Parse(objValue);
- }
- catch
- {
- returnValue = defaultValue;
- }
- }
-
- return returnValue;
- }
-
-
-
-
-
-
-
- public static int IntParse(object objValue, int defaultValue)
- {
- int returnValue = defaultValue;
-
- if (objValue != null && objValue != DBNull.Value)
- {
- try
- {
- returnValue = int.Parse(objValue.ToString());
- }
- catch
- {
- returnValue = defaultValue;
- }
- }
-
-
- return returnValue;
- }
-
-
-
-
-
-
- public static int IntParse(object objValue)
- {
- return IntParse(objValue, 0);
- }
-
-
-
-
-
-
- public static DateTime DateTimeParse(object objValue, DateTime defaultValue)
- {
- DateTime returnValue = defaultValue;
-
- if (objValue != null && objValue != DBNull.Value)
- {
- try
- {
- returnValue = DateTime.Parse(objValue.ToString());
- }
- catch
- {
- returnValue = defaultValue;
- }
- }
-
-
- return returnValue;
- }
-
-
-
-
-
-
- public static DateTime DateTimeParse(object objValue)
- {
- return DateTimeParse(objValue, DateTime.MinValue);
- }
-
-
-
-
-
-
-
-
- public static string StringParse(object objValue, string defaultValue)
- {
- string returnValue = defaultValue;
-
- if (objValue != null && objValue != DBNull.Value)
- {
- try
- {
- returnValue = objValue.ToString();
- }
- catch
- {
- returnValue = defaultValue; ;
- }
-
- }
-
-
- return returnValue;
- }
-
-
-
-
-
-
- public static string StringParse(object objValue)
- {
- return StringParse(objValue, string.Empty);
- }
-
-
-
-
-
-
-
-
- public static Guid GuidParse(object objValue, Guid defaultValue)
- {
- Guid returnValue = defaultValue;
-
- if (objValue != null && objValue != DBNull.Value)
- {
- try
- {
- returnValue = new Guid(objValue.ToString());
- }
- catch
- {
- returnValue = defaultValue; ;
- }
-
- }
-
-
- return returnValue;
- }
-
-
-
-
-
-
-
- public static Guid GuidParse(object objValue)
- {
- return GuidParse(objValue, Guid.Empty);
- }
-
-
-
-
-
-
-
-
- public static T Parse<T>(object objValue, T defaultValue)
- {
- T returnValue = defaultValue;
-
- if (objValue != null && objValue != DBNull.Value)
- {
- try
- {
- returnValue = (T)objValue;
- }
- catch
- {
- returnValue = defaultValue;
- }
- }
-
-
- return returnValue;
- }
-
-
-
-
-
-
-
- public static T Parse<T>(object objValue)
- {
- return Parse<T>(objValue, default(T));
- }
来源:http://developer.51cto.com/art/200908/143529.htm