old_flsystem/类库/SqlSugar/Utilities/UtilMethods.cs

135 lines
4.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
namespace SqlSugar
{
public class UtilMethods
{
internal static Type GetUnderType(Type oldType)
{
Type type = Nullable.GetUnderlyingType(oldType);
return type == null ? oldType : type;
}
internal static Type GetRootBaseType(Type entityType)
{
var baseType = entityType.BaseType;
while (baseType != null && baseType.BaseType != UtilConstants.ObjType)
{
baseType = baseType.BaseType;
}
return baseType;
}
internal static Type GetUnderType(PropertyInfo propertyInfo, ref bool isNullable)
{
Type unType = Nullable.GetUnderlyingType(propertyInfo.PropertyType);
isNullable = unType != null;
unType = unType ?? propertyInfo.PropertyType;
return unType;
}
internal static Type GetUnderType(PropertyInfo propertyInfo)
{
Type unType = Nullable.GetUnderlyingType(propertyInfo.PropertyType);
unType = unType ?? propertyInfo.PropertyType;
return unType;
}
internal static bool IsNullable(PropertyInfo propertyInfo)
{
Type unType = Nullable.GetUnderlyingType(propertyInfo.PropertyType);
return unType != null;
}
internal static T IsNullReturnNew<T>(T returnObj) where T : new()
{
if (returnObj.IsNullOrEmpty())
{
returnObj = new T();
}
return returnObj;
}
internal static T ChangeType<T>(T obj, Type type)
{
return (T)Convert.ChangeType(obj, type);
}
internal static T ChangeType<T>(T obj)
{
return (T)Convert.ChangeType(obj, typeof(T));
}
internal static void RepairReplicationParameters(ref string appendSql, SugarParameter[] parameters, int addIndex,string append=null)
{
if (appendSql.HasValue() && parameters.HasValue())
{
foreach (var parameter in parameters.OrderByDescending(it => it.ParameterName.Length))
{
//Compatible with.NET CORE parameters case
var name = parameter.ParameterName;
string newName = name +append+ addIndex;
appendSql =Regex.Replace(appendSql,name,newName,RegexOptions.IgnoreCase);
parameter.ParameterName = newName;
}
}
}
internal static string GetPackTable(string sql, string shortName)
{
return string.Format(" ({0}) {1} ", sql, shortName);
}
internal static string GetParenthesesValue(string dbTypeName)
{
if (Regex.IsMatch(dbTypeName, @"\(.+\)"))
{
dbTypeName = Regex.Replace(dbTypeName, @"\(.+\)", "");
}
dbTypeName = dbTypeName.Trim();
return dbTypeName;
}
internal static T GetOldValue<T>(T value, Action action)
{
action();
return value;
}
internal static object DefaultForType(Type targetType)
{
return targetType.IsValueType ? Activator.CreateInstance(targetType) : null;
}
internal static Int64 GetLong(byte[] bytes)
{
return Convert.ToInt64(string.Join("", bytes).PadRight(20, '0'));
}
internal static string GetMD5(string myString)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] fromData = System.Text.Encoding.Unicode.GetBytes(myString);
byte[] targetData = md5.ComputeHash(fromData);
string byte2String = null;
for (int i = 0; i < targetData.Length; i++)
{
byte2String += targetData[i].ToString("x");
}
return byte2String;
}
}
}