|
|
/////////////////////////////////////////////////////////////////
|
|
|
//
|
|
|
// (C) Copyright 2009, Kenneth, Inc.
|
|
|
// All rights reserved. Confidential. Except as pursuant
|
|
|
// to a written agreement with Kenneth, this software may
|
|
|
// not be used or distributed. This software may be covered
|
|
|
// by one or more patents.
|
|
|
//
|
|
|
// 本软件为Kenneth开发,版权所有,违者必究,320325198102218110
|
|
|
//
|
|
|
/////////////////////////////////////////////////////////////////
|
|
|
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
using System.Text;
|
|
|
using System.Text.RegularExpressions;
|
|
|
using System.ComponentModel;
|
|
|
using System.Data.SqlClient;
|
|
|
using System.Data;
|
|
|
|
|
|
namespace GummingCommon
|
|
|
{
|
|
|
public class TypeHelper
|
|
|
{
|
|
|
/// <summary>
|
|
|
///
|
|
|
/// </summary>
|
|
|
/// <param name="str"></param>
|
|
|
/// <returns></returns>
|
|
|
public static string ConvertFromBase64String(string str)
|
|
|
{
|
|
|
return Encoding.Default.GetString(Convert.FromBase64String(str));
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
///
|
|
|
/// </summary>
|
|
|
/// <param name="str"></param>
|
|
|
/// <returns></returns>
|
|
|
public static string ConvertToBase64String(string str)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
return Convert.ToBase64String(Encoding.Default.GetBytes(str));
|
|
|
}
|
|
|
catch
|
|
|
{
|
|
|
return "";
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static string ConvertFromBase64UTF8(string str)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
return Encoding.UTF8.GetString(Convert.FromBase64String(str));
|
|
|
}
|
|
|
catch
|
|
|
{
|
|
|
|
|
|
return "";
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static string ConvertToBase64UTF8(string str)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
return Convert.ToBase64String(Encoding.UTF8.GetBytes(str));
|
|
|
}
|
|
|
catch
|
|
|
{
|
|
|
|
|
|
return "";
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static String UTF8ByteArrayToString(Byte[] characters)
|
|
|
{
|
|
|
|
|
|
UTF8Encoding encoding = new UTF8Encoding();
|
|
|
String constructedString = encoding.GetString(characters);
|
|
|
return (constructedString);
|
|
|
}
|
|
|
|
|
|
public static Byte[] StringToUTF8ByteArray(String pXmlString)
|
|
|
{
|
|
|
|
|
|
UTF8Encoding encoding = new UTF8Encoding();
|
|
|
Byte[] byteArray = encoding.GetBytes(pXmlString);
|
|
|
return byteArray;
|
|
|
}
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
///
|
|
|
/// </summary>
|
|
|
/// <param name="dt"></param>
|
|
|
/// <returns></returns>
|
|
|
public static string ToString(DateTime dt)
|
|
|
{
|
|
|
return dt.ToString("yyyy-MM-dd HH:mm:ss");
|
|
|
}
|
|
|
|
|
|
public static string ToString(DateTime? dt)
|
|
|
{
|
|
|
return Convert.ToDateTime(dt).ToString("yyyy-MM-dd HH:mm:ss");
|
|
|
}
|
|
|
|
|
|
public static string ToOCIString(DateTime? dt)
|
|
|
{
|
|
|
return Convert.ToDateTime(dt).ToString("yyyyMMdd");
|
|
|
}
|
|
|
|
|
|
public static DateTime ToDateTime(string dtstr)
|
|
|
{
|
|
|
DateTime dt;
|
|
|
DateTime.TryParse(dtstr, out dt);
|
|
|
return dt;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
///
|
|
|
/// </summary>
|
|
|
/// <param name="splitString"></param>
|
|
|
/// <param name="numbers"></param>
|
|
|
/// <returns></returns>
|
|
|
public static string[] Split(string splitString, int numbers)
|
|
|
{
|
|
|
string[] values = splitString.Split(',');
|
|
|
List<string> listValues = new List<string>();
|
|
|
listValues.AddRange(values);
|
|
|
while (listValues.Count < numbers)
|
|
|
{
|
|
|
listValues.Add("0");
|
|
|
}
|
|
|
|
|
|
for (int i = 0; i < listValues.Count; i++)
|
|
|
{
|
|
|
if (string.IsNullOrEmpty(listValues[i]))
|
|
|
{
|
|
|
listValues[i] = "0";
|
|
|
}
|
|
|
}
|
|
|
return listValues.ToArray();
|
|
|
}
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
///
|
|
|
/// </summary>
|
|
|
/// <param name="s"></param>
|
|
|
/// <returns></returns>
|
|
|
public static byte[] HexStringToByteArray(string s)
|
|
|
{
|
|
|
s = s.Replace(" ", "");
|
|
|
|
|
|
byte[] buffer = new byte[s.Length / 2];
|
|
|
|
|
|
for (int i = 0; i < s.Length; i += 2)
|
|
|
|
|
|
buffer[i / 2] = (byte)Convert.ToByte(s.Substring(i, 2), 16);
|
|
|
|
|
|
return buffer;
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
///
|
|
|
/// </summary>
|
|
|
/// <param name="data"></param>
|
|
|
/// <returns></returns>
|
|
|
public static string ByteArrayToHexString(byte[] data)
|
|
|
{
|
|
|
StringBuilder sb = new StringBuilder(data.Length * 3);
|
|
|
|
|
|
foreach (byte b in data)
|
|
|
|
|
|
sb.Append(Convert.ToString(b, 16).PadLeft(2, '0').PadRight(3, ' '));
|
|
|
|
|
|
return sb.ToString().ToUpper();
|
|
|
}
|
|
|
|
|
|
public static string IntToHex(int value)
|
|
|
{
|
|
|
return value.ToString("X");
|
|
|
}
|
|
|
|
|
|
public static byte[] IntToByteArray(int value)
|
|
|
{
|
|
|
//低位在前,高位在后
|
|
|
byte[] src = new byte[4];
|
|
|
src[3] = (byte)((value >> 24) & 0xFF);
|
|
|
src[2] = (byte)((value >> 16) & 0xFF);
|
|
|
src[1] = (byte)((value >> 8) & 0xFF);
|
|
|
src[0] = (byte)(value & 0xFF);
|
|
|
return src;
|
|
|
|
|
|
/*高位在前,低位在后
|
|
|
byte[] src = new byte[4];
|
|
|
src[0] = (byte) ((value>>24) & 0xFF);
|
|
|
src[1] = (byte) ((value>>16)& 0xFF);
|
|
|
src[2] = (byte) ((value>>8)&0xFF);
|
|
|
src[3] = (byte) (value & 0xFF);
|
|
|
return src;*/
|
|
|
}
|
|
|
|
|
|
public static byte[] ShortToByteArray(int value)
|
|
|
{
|
|
|
//低位在前,高位在后
|
|
|
byte[] src = new byte[2];
|
|
|
src[1] = (byte)((value >> 8) & 0xFF);
|
|
|
src[0] = (byte)(value & 0xFF);
|
|
|
return src;
|
|
|
}
|
|
|
|
|
|
public static int ByteArrayToInt(byte[] bytes)
|
|
|
{
|
|
|
int value;//低位在前,高位在后
|
|
|
value = (int)((bytes[0] & 0xFF)
|
|
|
| ((bytes[1] & 0xFF) << 8)
|
|
|
| ((bytes[2] & 0xFF) << 16)
|
|
|
| ((bytes[3] & 0xFF) << 24));
|
|
|
return value;
|
|
|
|
|
|
/*低位在后,高位在前
|
|
|
int value;
|
|
|
value = (int)(((src[offset] & 0xFF) << 24)
|
|
|
| ((src[offset + 1] & 0xFF) << 16)
|
|
|
| ((src[offset + 2] & 0xFF) << 8)
|
|
|
| (src[offset + 3] & 0xFF));
|
|
|
return value;*/
|
|
|
}
|
|
|
|
|
|
public static int ByteArrayToShort(byte[] bytes)
|
|
|
{
|
|
|
int value;//低位在前,高位在后
|
|
|
value = (int)((bytes[0] & 0xFF)
|
|
|
| ((bytes[1] & 0xFF) << 8));
|
|
|
return value;
|
|
|
|
|
|
/*低位在后,高位在前
|
|
|
int value;
|
|
|
value = (int)(((src[offset] & 0xFF) << 24)
|
|
|
| ((src[offset + 1] & 0xFF) << 16)
|
|
|
| ((src[offset + 2] & 0xFF) << 8)
|
|
|
| (src[offset + 3] & 0xFF));
|
|
|
return value;*/
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// valide the number type
|
|
|
/// </summary>
|
|
|
/// <param name="number"></param>
|
|
|
/// <returns></returns>
|
|
|
/*/////
|
|
|
valide the number type//*/
|
|
|
public static bool ValideDecimal(string number)/////*/
|
|
|
{
|
|
|
bool isnumber = false;
|
|
|
string macth = @"^[+-]?\d{0,19}\.\d*$";
|
|
|
|
|
|
isnumber = Regex.IsMatch(number, macth);
|
|
|
|
|
|
macth = @"^[+-]?\d{1,19}$";
|
|
|
|
|
|
isnumber = Regex.IsMatch(number, macth) || isnumber;
|
|
|
|
|
|
return isnumber;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
///
|
|
|
/// </summary>
|
|
|
/// <param name="pi"></param>
|
|
|
/// <returns></returns>
|
|
|
public static bool ValidTypeIsNumber(System.Reflection.PropertyInfo pi)
|
|
|
{
|
|
|
if (pi.PropertyType == typeof(double) || pi.PropertyType == typeof(int) || pi.PropertyType == typeof(decimal))
|
|
|
{
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// Convert Media Color (WPF) to Drawing Color (WinForm)
|
|
|
/// </summary>
|
|
|
/// <param name="mediaColor"></param>
|
|
|
/// <returns></returns>
|
|
|
public static System.Drawing.Color ToDrawingColor(System.Windows.Media.Color mediaColor)
|
|
|
{
|
|
|
return System.Drawing.Color.FromArgb(mediaColor.A, mediaColor.R, mediaColor.G, mediaColor.B);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// Convert Drawing Color (WPF) to Media Color (WinForm)
|
|
|
/// </summary>
|
|
|
/// <param name="drawingColor"></param>
|
|
|
/// <returns></returns>
|
|
|
public static System.Windows.Media.Color ToMediaColor(System.Drawing.Color drawingColor)
|
|
|
{
|
|
|
return System.Windows.Media.Color.FromArgb(drawingColor.A, drawingColor.R, drawingColor.G, drawingColor.B);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
///
|
|
|
/// </summary>
|
|
|
/// <param name="value"></param>
|
|
|
/// <param name="destinationType"></param>
|
|
|
/// <returns></returns>
|
|
|
public static object ConvertAnyType(object value, Type destinationType)
|
|
|
{
|
|
|
object returnValue;
|
|
|
if ((value == null) || destinationType.IsInstanceOfType(value))
|
|
|
{
|
|
|
return value;
|
|
|
}
|
|
|
string str = value as string;
|
|
|
if ((str != null) && (str.Length == 0))
|
|
|
{
|
|
|
return null;
|
|
|
}
|
|
|
TypeConverter converter = TypeDescriptor.GetConverter(destinationType);
|
|
|
bool flag = converter.CanConvertFrom(value.GetType());
|
|
|
if (!flag)
|
|
|
{
|
|
|
converter = TypeDescriptor.GetConverter(value.GetType());
|
|
|
}
|
|
|
if (!flag && !converter.CanConvertTo(destinationType))
|
|
|
{
|
|
|
throw new InvalidOperationException("can to finshed the convert:" + value.ToString() + "==>" + destinationType);
|
|
|
}
|
|
|
try
|
|
|
{
|
|
|
if (destinationType == typeof(bool) && TypeHelper.ValideDecimal((string)value))
|
|
|
{
|
|
|
string newvalue = value as string;
|
|
|
newvalue = newvalue.ToLower();
|
|
|
returnValue = newvalue == "true" || newvalue == "1" ? true : false;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
returnValue = flag ? converter.ConvertFrom(null, null, value) : converter.ConvertTo(null, null, value, destinationType);
|
|
|
}
|
|
|
}
|
|
|
catch (Exception e)
|
|
|
{
|
|
|
throw new InvalidOperationException("convert error:" + value.ToString() + "==>" + destinationType, e);
|
|
|
}
|
|
|
return returnValue;
|
|
|
}
|
|
|
|
|
|
|
|
|
public static DbType GetDbType(Type type)
|
|
|
{
|
|
|
String name = type.Name;
|
|
|
DbType val = DbType.String; // default value
|
|
|
try
|
|
|
{
|
|
|
val = (DbType)Enum.Parse(typeof(DbType), name, true);
|
|
|
}
|
|
|
catch (Exception)
|
|
|
{
|
|
|
// add error handling to suit your taste
|
|
|
}
|
|
|
return val;
|
|
|
}
|
|
|
|
|
|
public static SqlDbType GetSqlDbType(Type type)
|
|
|
{
|
|
|
SqlParameter p1 = new SqlParameter();
|
|
|
System.ComponentModel.TypeConverter tc;
|
|
|
tc = System.ComponentModel.TypeDescriptor.GetConverter(p1.DbType);
|
|
|
|
|
|
if (tc.CanConvertFrom(type))
|
|
|
p1.DbType = (DbType)tc.ConvertFrom(type.Name);
|
|
|
else
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
p1.DbType = (DbType)tc.ConvertFrom(type.Name);
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
}
|
|
|
}
|
|
|
return p1.SqlDbType;
|
|
|
}
|
|
|
|
|
|
public static SqlDbType GetSqlDbType(TypeCode type)
|
|
|
{
|
|
|
SqlDbType p = SqlDbType.VarChar;
|
|
|
switch (type)
|
|
|
{
|
|
|
case TypeCode.String:
|
|
|
p = SqlDbType.VarChar;
|
|
|
break;
|
|
|
case TypeCode.Int32:
|
|
|
p = SqlDbType.Int;
|
|
|
break;
|
|
|
case TypeCode.Int64:
|
|
|
p = SqlDbType.BigInt;
|
|
|
break;
|
|
|
default:
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
return p;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
}
|