tijian_tieying/web/cyqdata-master/Tool/StaticTool.cs

411 lines
13 KiB
C#
Raw Normal View History

2025-02-20 12:14:39 +08:00
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Data;
using CYQ.Data.Table;
using CYQ.Data.SQL;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace CYQ.Data.Tool
{
/// <summary>
/// <20><><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>֧<EFBFBD><D6A7>jsonתʵ<D7AA>
/// </summary>
public static class ConvertTool
{
/// <summary>
/// <20><><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>(<28><>׼ǿ<D7BC><C7BF>)
/// </summary>
/// <param name="value">ֵ<><D6B5><EFBFBD><EFBFBD></param>
/// <param name="t"><3E><><EFBFBD><EFBFBD></param>
/// <returns></returns>
public static object ChangeType(object value, Type t)
{
return StaticTool.ChangeType(value, t);
}
}
/// <summary>
/// <20><><EFBFBD><EFBFBD>ߣ<EFBFBD><DFA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
public static class ReflectTool
{
/// <summary>
/// <20><>ȡ<EFBFBD><C8A1><EFBFBD>Ͳ<EFBFBD><CDB2><EFBFBD><EFBFBD>ij<EFBFBD><C4B3><EFBFBD>
/// </summary>
public static int GetGenericArgumentLength(ref Type t)
{
return StaticTool.GetArgumentLength(ref t);
}
/// <summary>
/// <20><>ȡ<EFBFBD><C8A1><EFBFBD>Ͳ<EFBFBD><CDB2><EFBFBD><EFBFBD>ij<EFBFBD><C4B3>ȣ<EFBFBD><C8A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͣ<EFBFBD>
/// </summary>
public static int GetGenericArgumentLength(ref Type t, out Type[] argTypes)
{
return StaticTool.GetArgumentLength(ref t, out argTypes);
}
/// <summary>
/// <20><><EFBFBD>÷<EFBFBD><C3B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԣ<EFBFBD><D4A3>ڲ<EFBFBD><DAB2>л<EFBFBD><D0BB>
/// </summary>
public static List<PropertyInfo> GetPropertys(Type t)
{
return StaticTool.GetPropertyInfo(t);
}
/// <summary>
/// <20><>ȡϵͳ<CFB5><CDB3><EFBFBD>ͣ<EFBFBD><CDA3><EFBFBD><EFBFBD><EFBFBD>Nullable<6C><65><EFBFBD>ͣ<EFBFBD><CDA3><EFBFBD>תΪ<D7AA><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͡<EFBFBD>
/// </summary>
public static SysType GetSystemType(ref Type t)
{
return StaticTool.GetSystemType(ref t);
}
}
/// <summary>
/// <20><>̬<EFBFBD><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
internal static class StaticTool
{
/// <summary>
/// <20><>PropertyInfo[] <20>ij<EFBFBD>PropertyInfo List<73><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ.NET<45><54>CLR<4C><52><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD>д<EFBFBD><EFBFBD><ECB3A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IntelliTraceʱ<65><CAB1>
/// </summary>
static MDictionary<string, List<PropertyInfo>> propCache = new MDictionary<string, List<PropertyInfo>>();
/// <summary>
/// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD>
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
public static List<PropertyInfo> GetPropertyInfo(Type t)
{
string key = t.GUID.ToString();
if (propCache.ContainsKey(key))
{
return propCache[key];
}
else
{
bool isInheritOrm = t.BaseType.Name == "OrmBase" || t.BaseType.Name == "SimpleOrmBase";
PropertyInfo[] pInfo = isInheritOrm ? t.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly) : t.GetProperties();
List<PropertyInfo> list = new List<PropertyInfo>(pInfo.Length);
try
{
list.AddRange(pInfo);
propCache.Set(key, list);
}
catch (Exception err)
{
Log.WriteLogToTxt(err);
}
return list;
}
}
static Dictionary<string, Type[]> argumentCache = new Dictionary<string, Type[]>();
/// <summary>
/// <20><>ȡ<EFBFBD><C8A1><EFBFBD>͵IJ<CDB5><C4B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȣ<EFBFBD><C8A3>Ƿ<EFBFBD><C7B7>Ͱ<EFBFBD>Ĭ<EFBFBD>Ϸ<EFBFBD><CFB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
public static int GetArgumentLength(ref Type t)
{
Type[] argTypes;
return GetArgumentLength(ref t, out argTypes);
}
/// <summary>
/// <20><>ȡ<EFBFBD><C8A1><EFBFBD>͵IJ<CDB5><C4B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȣ<EFBFBD><C8A3>Ƿ<EFBFBD><C7B7>Ͱ<EFBFBD>Ĭ<EFBFBD>Ϸ<EFBFBD><CFB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
public static int GetArgumentLength(ref Type t, out Type[] argTypes)
{
if (argumentCache.ContainsKey(t.FullName))
{
argTypes = argumentCache[t.FullName];
return argTypes.Length;
}
else
{
int len = 0;
if (t.IsGenericType)
{
argTypes = t.GetGenericArguments();
len = argTypes.Length;
for (int i = 0; i < argTypes.Length; i++)
{
if (argTypes[i].IsGenericType && argTypes[i].Name.StartsWith("Nullable"))
{
argTypes[i] = Nullable.GetUnderlyingType(argTypes[i]);
}
}
if (t.Name.StartsWith("Nullable"))
{
t = Nullable.GetUnderlyingType(t);
}
}
else
{
if (t.Name.EndsWith("[]") || t.Name == "MDataRowCollection")
{
len = 1;
}
else if (t.Name == "NameValueCollection" || (t.BaseType != null && t.BaseType.Name == "NameValueCollection"))
{
len = 2;
}
else
{
System.Reflection.MethodInfo mi = t.GetMethod("Add");
if (mi != null)
{
len = mi.GetParameters().Length;
}
}
argTypes = new Type[len];
for (int i = 0; i < argTypes.Length; i++)
{
argTypes[i] = typeof(object);
}
}
try
{
argumentCache.Add(t.FullName, argTypes);
}
catch
{
}
return len;
}
}
/// <summary>
/// <20><>ȡϵͳ<CFB5><CDB3><EFBFBD>ͣ<EFBFBD><CDA3><EFBFBD><EFBFBD><EFBFBD>Nullable<6C><65><EFBFBD>ͣ<EFBFBD><CDA3><EFBFBD>תΪ<D7AA><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͡<EFBFBD>
/// </summary>
public static SysType GetSystemType(ref Type t)
{
if (t.IsEnum)
{
return SysType.Enum;
}
if (t.FullName.StartsWith("System.")) // ϵͳ<CFB5><CDB3><EFBFBD><EFBFBD>
{
if (t.IsGenericType)
{
if (t.Name.StartsWith("Nullable"))//int? id
{
t = Nullable.GetUnderlyingType(t);
return SysType.Base;
}
return SysType.Generic;
}
else if (t.FullName.StartsWith("System.Collections."))
{
return SysType.Collection;
}
else if (t.Name.EndsWith("[]"))
{
return SysType.Array;
}
if (t.FullName.Split('.').Length > 2)
{
return SysType.Custom;
}
return SysType.Base;
}
else
{
return SysType.Custom;
}
}
/// <summary>
/// <20><>GUIDת<44><D7AA>16<31>ֽ<EFBFBD><D6BD>ַ<EFBFBD><D6B7><EFBFBD>
/// </summary>
/// <returns></returns>
internal static string ToGuidByteString(string guid)
{
return BitConverter.ToString(new Guid(guid).ToByteArray()).Replace("-", "");
}
/// <summary>
/// <20><>ȡԼ<C8A1><D4BC>ö<EFBFBD>ٵ<EFBFBD><D9B5><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
/// <param name="tableNamesEnum"><3E><>ö<EFBFBD>ٻ<EFBFBD><D9BB><EFBFBD><EFBFBD><EFBFBD></param>
/// <returns></returns>
internal static string GetDbName(ref object tableNamesEnum)
{
string dbName = string.Empty;
if (tableNamesEnum is Enum)
{
Type t = tableNamesEnum.GetType();
string enumName = t.Name;
if (enumName != "TableNames" && enumName != "ViewNames")
{
if (enumName.Length > 1 && enumName[1] == '_')
{
dbName = enumName.Substring(2, enumName.Length - 6);//.Replace("Enum", "Conn");
}
else
{
string[] items = t.FullName.Split('.');
if (items.Length > 1)
{
dbName = items[items.Length - 2];// +"Conn";
items = null;
}
}
}
t = null;
}
else if (tableNamesEnum is string)
{
string tName = tableNamesEnum.ToString();
int index = tName.LastIndexOf(')');
if (index > 0) // <20><>ͼ
{
string viewSQL = tName;
string a = tName.Substring(0, index + 1);//a<><61><EFBFBD><EFBFBD>
tName = tName.Substring(index + 1).Trim();//b<><62><EFBFBD>֡<EFBFBD>ddd.v_xxx
//<2F>޸<EFBFBD>ԭ<EFBFBD><D4AD><EFBFBD><EFBFBD>
if (tName.Contains("."))
{
tableNamesEnum = a + " " + tName.Substring(tName.LastIndexOf('.') + 1);
}
}
if (tName.Contains(".") && !tName.Trim().Contains(" "))
{
dbName = tName.Split('.')[0];
}
}
return dbName;
}
/// <summary>
/// <20><><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>(<28><>׼ǿ<D7BC><C7BF>)
/// </summary>
/// <param name="value">ֵ<><D6B5><EFBFBD><EFBFBD></param>
/// <param name="t"><3E><><EFBFBD><EFBFBD></param>
/// <returns></returns>
public static object ChangeType(object value, Type t)
{
if (t == null)
{
return null;
}
string strValue = Convert.ToString(value);
if (t.IsGenericType && t.Name.StartsWith("Nullable"))
{
t = Nullable.GetUnderlyingType(t);
if (strValue == "")
{
return null;
}
}
if (t.Name == "String")
{
return strValue;
}
if (t.FullName == "System.Text.Encoding")
{
return value as Encoding;
}
if (strValue == "")
{
return Activator.CreateInstance(t);
}
else if (t.IsValueType)
{
if (t.Name == "DateTime")
{
return Convert.ChangeType(value, t);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>value<75><65><EFBFBD><EFBFBD><EFBFBD>ⶪʧ<E2B6AA><CAA7><EFBFBD><EFBFBD>
}
if (t.Name == "Guid")
{
return new Guid(strValue);
}
else if (t.Name.StartsWith("Int") && strValue.IndexOf('.') > -1)
{
strValue = strValue.Split('.')[0];
}
else if (t.Name == "Boolean")
{
switch (strValue.ToLower())
{
case "yes":
case "true":
case "1":
case "on":
case "<22><>":
return true;
case "no":
case "false":
case "0":
case "":
case "<22><>":
default:
return false;
}
}
return Convert.ChangeType(strValue, t);
}
else
{
if (strValue != t.FullName)
{
switch (GetSystemType(ref t))
{
case SysType.Custom:
return MDataRow.CreateFrom(strValue).ToEntity(t);
case SysType.Generic:
if (t.Name.StartsWith("List"))
{
return MDataTable.CreateFrom(strValue).ToList(t);
}
break;
case SysType.Array:
if (t.Name == "Byte[]" && value.GetType().Name != t.Name)
{
using (MemoryStream ms = new MemoryStream())
{
new BinaryFormatter().Serialize(ms, value);
return ms.ToArray();
}
}
break;
}
}
return Convert.ChangeType(value, t);
}
}
#region <EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>HashKey
static MDictionary<string, string> hashKeyCache = new MDictionary<string, string>(32);
internal static string GetHashKey(string sourceString)
{
try
{
if (hashKeyCache.ContainsKey(sourceString))
{
return hashKeyCache[sourceString];
}
else
{
if (hashKeyCache.Count > 512)
{
hashKeyCache.Clear();
hashKeyCache = new MDictionary<string, string>(64);
}
string value = "K" + Math.Abs(sourceString.GetHashCode()) + sourceString.Length;
hashKeyCache.Add(sourceString, value);
return value;
}
}
catch
{
return sourceString;
}
}
#endregion
}
}