新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
常用自定義C#類型轉(zhuǎn)換函數(shù)
這里將介紹常用自定義C#類型轉(zhuǎn)換函數(shù),大家經(jīng)常碰到類弄轉(zhuǎn)換,但都不知道哪些系統(tǒng)函數(shù)才可以轉(zhuǎn)換。希望本文能對(duì)大家有所幫助。

- ///
- /// 將字符型類型轉(zhuǎn)換為整型值
- ///
- /// 字符型
- /// 無(wú)法轉(zhuǎn)換時(shí)的默認(rèn)值
- ///
整型值 - 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;
- }
- ///
- /// 將對(duì)象類型轉(zhuǎn)換為整型值
- ///
- /// 對(duì)象類型
- /// 無(wú)法轉(zhuǎn)換時(shí)的默認(rèn)值
- ///
整型值 - 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;
- }
- ///
- /// 將對(duì)象類型轉(zhuǎn)換為整型值
- ///
- /// 對(duì)象類型
- ///
整型值 - public static int IntParse(object objValue)
- {
- return IntParse(objValue, 0);
- }
- ///
- /// 將對(duì)象類型轉(zhuǎn)換為日期值
- ///
- /// 對(duì)象類型
- /// 無(wú)法轉(zhuǎn)換時(shí)的默認(rèn)值
- ///
日期值 - 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;
- }
- ///
- /// 將對(duì)象類型轉(zhuǎn)換為日期值
- ///
- /// 對(duì)象類型
- ///
日期值 - public static DateTime DateTimeParse(object objValue)
- {
- return DateTimeParse(objValue, DateTime.MinValue);
- }
- ///
- /// 將對(duì)象類型轉(zhuǎn)換為字符型
- ///
- /// 對(duì)象類型
- /// 無(wú)法轉(zhuǎn)換時(shí)的默認(rèn)值
- ///
字符型 - 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;
- }
- ///
- /// 將對(duì)象類型轉(zhuǎn)換為字符型
- ///
- /// 對(duì)象類型
- ///
字符型 - public static string StringParse(object objValue)
- {
- return StringParse(objValue, string.Empty);
- }
- ///
- /// 將對(duì)象類型轉(zhuǎn)換為GUID
- ///
- /// 對(duì)象類型
- /// 無(wú)法轉(zhuǎn)換時(shí)的默認(rèn)值
- ///
GUID - 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;
- }
- ///
- /// 將對(duì)象類型轉(zhuǎn)換為GUID
- ///
- /// 對(duì)象類型
- ///
GUID - public static Guid GuidParse(object objValue)
- {
- return GuidParse(objValue, Guid.Empty);
- }
- ///
- /// C#類型轉(zhuǎn)換函數(shù)
- ///
- ///
目標(biāo)類型值 - /// 對(duì)象類型
- /// 無(wú)法轉(zhuǎn)換時(shí)的默認(rèn)值
- ///
目標(biāo)類型值 - public static T Parse
(object objValue, T defaultValue) - {
- T returnValue = defaultValue;
- if (objValue != null && objValue != DBNull.Value)
- {
- try
- {
- returnValue = (T)objValue;
- }
- catch
- {
- returnValue = defaultValue;
- }
- }
- //返回值
- return returnValue;
- }
- ///
- /// C#類型轉(zhuǎn)換函數(shù)
- ///
- ///
目標(biāo)類型值 - /// 對(duì)象類型
- ///
目標(biāo)類型值 - public static T Parse
(object objValue) - {
- return Parse
(objValue, default(T)); - }
【編輯推薦】
- C#運(yùn)算符重載實(shí)現(xiàn)復(fù)數(shù)運(yùn)算
- C#運(yùn)算符重載實(shí)例解析
- C#運(yùn)算符重載的一些總結(jié)
- C#運(yùn)算符重載“>”的操作淺析
- C#運(yùn)算符優(yōu)先級(jí)介紹
當(dāng)前標(biāo)題:常用自定義C#類型轉(zhuǎn)換函數(shù)
網(wǎng)頁(yè)鏈接:http://m.fisionsoft.com.cn/article/djepdcc.html


咨詢
建站咨詢
