⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ctype.cs

📁 小型项目组用的工作日志记录系统
💻 CS
📖 第 1 页 / 共 2 页
字号:
/*
 * CType.cs
 * 
 * 作者:邱观琛
 * 创建时间:2002-5-6
 * 最后修改:2002-6-25
 * 
 * 
 * 提供一些主要涉及到数据类型的函数,例如更强的类型转换,数组的类型转换,
 * 类型兼容的判断等。
 * 
 */

using System;

namespace CFC
{
	/// <summary>
	/// 提供一些主要涉及到数据类型的函数
	/// </summary>
	public class CType
	{
		private CType()
		{
		}


		#region 判断各种类型的变量是否为空

		#region public static bool IsEmpty(string str) : 判断指定字符串是否是空串或由空格组成
		/// <summary>
		/// 判断指定字符串是否是空串或由空格组成。
		/// </summary>
		/// <param name="str">接受判断的字符串</param>
		/// <returns>判断结果,true或者false</returns>
		public static bool IsEmpty(string str)
		{
			if (str.Trim().Equals(String.Empty)) return true;
			else return false;
		}
		#endregion

		#region public static bool IsEmpty(object obj) : 判断指定对象是否是空串或由空格组成
		/// <summary>
		/// 判断指定对象是否是空串或由空格组成。
		/// </summary>
		/// <param name="obj">接受判断的对象</param>
		/// <returns>判断结果,true或者false</returns>
		public static bool IsEmpty(object obj)
		{
			if (obj.ToString().Trim().Equals(String.Empty)) return true;
			else return false;
		}
		#endregion

		#region public static bool IsNull(object obj) : 判断指定对象是否是null或DBNull
		/// <summary>
		/// 判断指定对象是否是空对象(null)或空数据对象(DBNull)。
		/// </summary>
		/// <param name="obj">接受判断的对象</param>
		/// <returns>判断结果,true或者false</returns>
		public static bool IsNull(object obj)
		{
			if ((obj == null) || Convert.IsDBNull(obj)) return true;
			else return false;
		}
		#endregion

		#endregion


		#region 判断变量是否能被转换成另一类型

		#region public static bool IsInt(string str, bool AllowEmpty) : 判断指定字符串是否能被转换成整数类型
		/// <summary>
		/// 判断指定字符串是否能被转换成整数类型。
		/// </summary>
		/// <param name="str">接受判断的字符串</param>
		/// <param name="AllowEmpty">是否允许空串</param>
		/// <returns>判断结果,true或者false</returns>
		public static bool IsInt(string str, bool AllowEmpty)
		{
			if (IsEmpty(str))
			{
				if (AllowEmpty) return true;
				else return false;
			}
			else
			{
				try
				{
					Convert.ToInt32(str);
					return true;
				}
				catch
				{
					return false;
				}
			}
		}
		#endregion


		#region public static bool IsInt(object obj, bool AllowEmpty) : 判断指定对象是否能被转换成整数类型
		/// <summary>
		/// 判断指定对象是否能被转换成整数类型。
		/// </summary>
		/// <param name="obj">接受判断的对象</param>
		/// <param name="AllowEmpty">是否允许空对象</param>
		/// <returns>判断结果,true或者false</returns>
		public static bool IsInt(object obj, bool AllowEmpty)
		{
			if (IsNull(obj))
			{
				if (AllowEmpty) return true;
				else return false;
			}
			else
			{
				try
				{
					Convert.ToInt32(obj);
					return true;
				}
				catch
				{
					return false;
				}
			}
		}
		#endregion


		#region public static bool IsDouble(string str, bool AllowEmpty) : 判断指定字符串是否能被转换成double类型
		/// <summary>
		/// 判断指定字符串是否能被转换成double类型。
		/// </summary>
		/// <param name="str">接受判断的字符串</param>
		/// <param name="AllowEmpty">是否允许空串</param>
		/// <returns>判断结果,true或者false</returns>
		public static bool IsDouble(string str, bool AllowEmpty)
		{
			if (IsEmpty(str))
			{
				if (AllowEmpty) return true;
				else return false;
			}
			else
			{
				try
				{
					Convert.ToDouble(str);
					return true;
				}
				catch
				{
					return false;
				}
			}
		}
		#endregion


		#region public static bool IsDouble(object obj, bool AllowEmpty) : 判断指定对象是否能被转换成double类型
		/// <summary>
		/// 判断指定对象是否能被转换成double类型。
		/// </summary>
		/// <param name="obj">接受判断的对象</param>
		/// <param name="AllowEmpty">是否允许空对象</param>
		/// <returns>判断结果,true或者false</returns>
		public static bool IsDouble(object obj, bool AllowEmpty)
		{
			if (IsNull(obj))
			{
				if (AllowEmpty) return true;
				else return false;
			}
			else
			{
				try
				{
					Convert.ToDouble(obj);
					return true;
				}
				catch
				{
					return false;
				}
			}
		}
		#endregion


		#region public static bool IsDecimal(string str, bool AllowEmpty) : 判断指定字符串是否能被转换成decimal类型
		/// <summary>
		/// 判断指定字符串是否能被转换成decimal类型。
		/// </summary>
		/// <param name="str">接受判断的字符串</param>
		/// <param name="AllowEmpty">是否允许空串</param>
		/// <returns>判断结果,true或者false</returns>
		public static bool IsDecimal(string str, bool AllowEmpty)
		{
			if (IsEmpty(str))
			{
				if (AllowEmpty) return true;
				else return false;
			}
			else
			{
				try
				{
					Convert.ToDecimal(str);
					return true;
				}
				catch
				{
					return false;
				}
			}
		}
		#endregion


		#region public static bool IsDecimal(object obj, bool AllowEmpty) : 判断指定对象是否能被转换成decimal类型
		/// <summary>
		/// 判断指定对象是否能被转换成decimal类型。
		/// </summary>
		/// <param name="obj">接受判断的对象</param>
		/// <param name="AllowEmpty">是否允许空对象</param>
		/// <returns>判断结果,true或者false</returns>
		public static bool IsDecimal(object obj, bool AllowEmpty)
		{
			if (IsNull(obj))
			{
				if (AllowEmpty) return true;
				else return false;
			}
			else
			{
				try
				{
					Convert.ToDecimal(obj);
					return true;
				}
				catch
				{
					return false;
				}
			}
		}
		#endregion


		#region public static bool IsDateTime(string str, bool AllowEmpty) : 判断指定字符串是否能被转换成DateTime类型
		/// <summary>
		/// 判断指定字符串是否能被转换成DateTime类型。
		/// </summary>
		/// <param name="str">接受判断的字符串</param>
		/// <param name="AllowEmpty">是否允许空串</param>
		/// <returns>判断结果,true或者false</returns>
		public static bool IsDateTime(string str, bool AllowEmpty)
		{
			if (IsEmpty(str))
			{
				if (AllowEmpty) return true;
				else return false;
			}
			else
			{
				try
				{
					Convert.ToDateTime(str);
					return true;
				}
				catch
				{
					return false;
				}
			}
		}
		#endregion


		#region public static bool IsDateTime(object obj, bool AllowEmpty) : 判断指定对象是否能被转换成DateTime类型
		/// <summary>
		/// 判断指定对象是否能被转换成DateTime类型。
		/// </summary>
		/// <param name="obj">接受判断的对象</param>
		/// <param name="AllowEmpty">是否允许空对象</param>
		/// <returns>判断结果,true或者false</returns>
		public static bool IsDateTime(object obj, bool AllowEmpty)
		{
			if (IsNull(obj))
			{
				if (AllowEmpty) return true;
				else return false;
			}
			else
			{
				try
				{
					Convert.ToDateTime(obj);
					return true;
				}
				catch
				{
					return false;
				}
			}
		}
		#endregion

		#endregion


		#region 整数类型和布尔类型之间的相互转换

		#region public static int BoolToInt(bool b) : 将布尔类型的数据转换为整数类型
		/// <summary>
		/// 将布尔类型的数据转换为整数类型。
		/// true转换成1,false转换成0。
		/// </summary>
		/// <param name="b">需要转换的布尔值</param>
		/// <returns>转换成的整数</returns>
		public static int BoolToInt(bool b)
		{
			if (b) return 1;
			else return 0;
		}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -