mirror of
http://git.coreshop.cn/jianweie/coreshoppro.git
synced 2025-12-06 18:13:26 +08:00
添加项目文件。
This commit is contained in:
217
CoreCms.Net.Utility/Extensions/ConvertObject.cs
Normal file
217
CoreCms.Net.Utility/Extensions/ConvertObject.cs
Normal file
@@ -0,0 +1,217 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace CoreCms.Net.Utility.Extensions
|
||||
{
|
||||
public class ConvertObjectExtensions
|
||||
{
|
||||
|
||||
#region Method1
|
||||
|
||||
/// <summary>
|
||||
/// 将object对象转换为实体对象
|
||||
/// </summary>
|
||||
/// <typeparam name="T">实体对象类名</typeparam>
|
||||
/// <param name="asObject">object对象</param>
|
||||
/// <returns></returns>
|
||||
private T ConvertObject<T>(object asObject) where T : new()
|
||||
{
|
||||
//创建实体对象实例
|
||||
var t = Activator.CreateInstance<T>();
|
||||
if (asObject != null)
|
||||
{
|
||||
Type type = asObject.GetType();
|
||||
//遍历实体对象属性
|
||||
foreach (var info in typeof(T).GetProperties())
|
||||
{
|
||||
object obj = null;
|
||||
//取得object对象中此属性的值
|
||||
var val = type.GetProperty(info.Name)?.GetValue(asObject);
|
||||
if (val != null)
|
||||
{
|
||||
//非泛型
|
||||
if (!info.PropertyType.IsGenericType)
|
||||
obj = Convert.ChangeType(val, info.PropertyType);
|
||||
else//泛型Nullable<>
|
||||
{
|
||||
Type genericTypeDefinition = info.PropertyType.GetGenericTypeDefinition();
|
||||
if (genericTypeDefinition == typeof(Nullable<>))
|
||||
{
|
||||
obj = Convert.ChangeType(val, Nullable.GetUnderlyingType(info.PropertyType));
|
||||
}
|
||||
else
|
||||
{
|
||||
obj = Convert.ChangeType(val, info.PropertyType);
|
||||
}
|
||||
}
|
||||
info.SetValue(t, obj, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Method2
|
||||
|
||||
/// <summary>
|
||||
/// 将object对象转换为实体对象
|
||||
/// </summary>
|
||||
/// <typeparam name="T">实体对象类名</typeparam>
|
||||
/// <param name="asObject">object对象</param>
|
||||
/// <returns></returns>
|
||||
public static T ConvertObjectByJson<T>(object asObject) where T : new()
|
||||
{
|
||||
//将object对象转换为json字符
|
||||
var json = JsonConvert.SerializeObject(asObject);
|
||||
//将json字符转换为实体对象
|
||||
var t = JsonConvert.DeserializeObject<T>(json);
|
||||
return t;
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
#region Method3
|
||||
/// <summary>
|
||||
/// 将object尝试转为指定对象
|
||||
/// </summary>
|
||||
/// <param name="data"></param>
|
||||
/// <returns></returns>
|
||||
public static T ConvertObjToModel<T>(object data) where T : new()
|
||||
{
|
||||
if (data == null) return new T();
|
||||
// 定义集合
|
||||
T result = new T();
|
||||
|
||||
// 获得此模型的类型
|
||||
Type type = typeof(T);
|
||||
string tempName = "";
|
||||
|
||||
// 获得此模型的公共属性
|
||||
PropertyInfo[] propertys = result.GetType().GetProperties();
|
||||
foreach (PropertyInfo pi in propertys)
|
||||
{
|
||||
tempName = pi.Name; // 检查object是否包含此列
|
||||
|
||||
// 判断此属性是否有Setter
|
||||
if (!pi.CanWrite) continue;
|
||||
|
||||
try
|
||||
{
|
||||
object value = GetPropertyValue(data, tempName);
|
||||
if (value != DBNull.Value)
|
||||
{
|
||||
Type tempType = pi.PropertyType;
|
||||
pi.SetValue(result, GetDataByType(value, tempType), null);
|
||||
|
||||
}
|
||||
}
|
||||
catch
|
||||
{ }
|
||||
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取一个类指定的属性值
|
||||
/// </summary>
|
||||
/// <param name="info">object对象</param>
|
||||
/// <param name="field">属性名称</param>
|
||||
/// <returns></returns>
|
||||
public static object GetPropertyValue(object info, string field)
|
||||
{
|
||||
if (info == null) return null;
|
||||
Type t = info.GetType();
|
||||
IEnumerable<System.Reflection.PropertyInfo> property = from pi in t.GetProperties() where pi.Name.ToLower() == field.ToLower() select pi;
|
||||
return property.First().GetValue(info, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将数据转为制定类型
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="data1"></param>
|
||||
/// <returns></returns>
|
||||
public static object GetDataByType(object data1, Type itype, params object[] myparams)
|
||||
{
|
||||
object result = new object();
|
||||
try
|
||||
{
|
||||
if (itype == typeof(decimal))
|
||||
{
|
||||
result = Convert.ToDecimal(data1);
|
||||
if (myparams.Length > 0)
|
||||
{
|
||||
result = Convert.ToDecimal(Math.Round(Convert.ToDecimal(data1), Convert.ToInt32(myparams[0])));
|
||||
}
|
||||
}
|
||||
else if (itype == typeof(double))
|
||||
{
|
||||
|
||||
if (myparams.Length > 0)
|
||||
{
|
||||
result = Convert.ToDouble(Math.Round(Convert.ToDouble(data1), Convert.ToInt32(myparams[0])));
|
||||
}
|
||||
else
|
||||
{
|
||||
result = double.Parse(Convert.ToDecimal(data1).ToString("0.00"));
|
||||
}
|
||||
}
|
||||
else if (itype == typeof(Int32))
|
||||
{
|
||||
result = Convert.ToInt32(data1);
|
||||
}
|
||||
else if (itype == typeof(DateTime))
|
||||
{
|
||||
result = Convert.ToDateTime(data1);
|
||||
}
|
||||
else if (itype == typeof(Guid))
|
||||
{
|
||||
result = new Guid(data1.ToString());
|
||||
}
|
||||
else if (itype == typeof(string))
|
||||
{
|
||||
result = data1.ToString();
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (itype == typeof(decimal))
|
||||
{
|
||||
result = 0;
|
||||
}
|
||||
else if (itype == typeof(double))
|
||||
{
|
||||
result = 0;
|
||||
}
|
||||
else if (itype == typeof(Int32))
|
||||
{
|
||||
result = 0;
|
||||
}
|
||||
else if (itype == typeof(DateTime))
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
else if (itype == typeof(Guid))
|
||||
{
|
||||
result = Guid.Empty;
|
||||
}
|
||||
else if (itype == typeof(string))
|
||||
{
|
||||
result = "";
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
181
CoreCms.Net.Utility/Extensions/ObjectExtensions.cs
Normal file
181
CoreCms.Net.Utility/Extensions/ObjectExtensions.cs
Normal file
@@ -0,0 +1,181 @@
|
||||
/***********************************************************************
|
||||
* Project: CoreCms.Net *
|
||||
* Web: https://CoreCms.Net *
|
||||
* ProjectName: 核心内容管理系统 *
|
||||
* Author: 大灰灰 *
|
||||
* Email: JianWeie@163.com *
|
||||
* Versions: 1.0 *
|
||||
* CreateTime: 2020-02-01 17:48:52
|
||||
* NameSpace: CoreCms.Net.Framework.Utility.Extensions
|
||||
* FileName: ConvertExtensions
|
||||
* ClassDescription:
|
||||
***********************************************************************/
|
||||
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace CoreCms.Net.Utility.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 扩展数据转换
|
||||
/// </summary>
|
||||
public static class ObjectExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 数据转换为int类型
|
||||
/// </summary>
|
||||
/// <param name="thisValue"></param>
|
||||
/// <returns></returns>
|
||||
public static int ObjectToInt(this object thisValue)
|
||||
{
|
||||
int result = 0;
|
||||
if (thisValue == null)
|
||||
return 0;
|
||||
return thisValue != null && thisValue != DBNull.Value && int.TryParse(thisValue.ToString(), out result) ? result : result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 数据转换为int类型
|
||||
/// </summary>
|
||||
/// <param name="thisValue"></param>
|
||||
/// <param name="errorValue"></param>
|
||||
/// <returns></returns>
|
||||
public static int ObjectToInt(this object thisValue, int errorValue)
|
||||
{
|
||||
int result = 0;
|
||||
return thisValue != null && thisValue != DBNull.Value && int.TryParse(thisValue.ToString(), out result) ? result : errorValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 数据转换为Double类型
|
||||
/// </summary>
|
||||
/// <param name="thisValue"></param>
|
||||
/// <returns></returns>
|
||||
public static double ObjectToDouble(this object thisValue)
|
||||
{
|
||||
double result = 0.0;
|
||||
return thisValue != null && thisValue != DBNull.Value && double.TryParse(thisValue.ToString(), out result) ? result : 0.0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 数据转换为Double类型
|
||||
/// </summary>
|
||||
/// <param name="thisValue"></param>
|
||||
/// <param name="errorValue"></param>
|
||||
/// <returns></returns>
|
||||
public static double ObjectToDouble(this object thisValue, double errorValue)
|
||||
{
|
||||
double result = 0.0;
|
||||
return thisValue != null && thisValue != DBNull.Value && double.TryParse(thisValue.ToString(), out result) ? result : errorValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 数据转换为Float类型
|
||||
/// </summary>
|
||||
/// <param name="thisValue"></param>
|
||||
/// <returns></returns>
|
||||
public static float ObjectToFloat(this object thisValue)
|
||||
{
|
||||
float result = 0;
|
||||
return thisValue != null && thisValue != DBNull.Value && float.TryParse(thisValue.ToString(), out result) ? result : 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 数据转换为Float类型
|
||||
/// </summary>
|
||||
/// <param name="thisValue"></param>
|
||||
/// <param name="errorValue"></param>
|
||||
/// <returns></returns>
|
||||
public static float ObjectToFloat(this object thisValue, float errorValue)
|
||||
{
|
||||
float result = 0;
|
||||
return thisValue != null && thisValue != DBNull.Value && float.TryParse(thisValue.ToString(), out result) ? result : errorValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 数据转换为String类型
|
||||
/// </summary>
|
||||
/// <param name="thisValue"></param>
|
||||
/// <returns></returns>
|
||||
public static string ObjectToString(this object thisValue)
|
||||
{
|
||||
return thisValue != null ? thisValue.ToString().Trim() : "";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 数据转换为String类型
|
||||
/// </summary>
|
||||
/// <param name="thisValue"></param>
|
||||
/// <param name="errorValue"></param>
|
||||
/// <returns></returns>
|
||||
public static string ObjectToString(this object thisValue, string errorValue)
|
||||
{
|
||||
return thisValue != null ? thisValue.ToString().Trim() : errorValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 数据转换为Decimal类型
|
||||
/// </summary>
|
||||
/// <param name="thisValue"></param>
|
||||
/// <returns></returns>
|
||||
public static Decimal ObjectToDecimal(this object thisValue)
|
||||
{
|
||||
Decimal result = new Decimal();
|
||||
return thisValue != null && thisValue != DBNull.Value && Decimal.TryParse(thisValue.ToString(), out result) ? result : Decimal.Zero;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 数据转换为Decimal类型
|
||||
/// </summary>
|
||||
/// <param name="thisValue"></param>
|
||||
/// <param name="errorValue"></param>
|
||||
/// <returns></returns>
|
||||
public static Decimal ObjectToDecimal(this object thisValue, Decimal errorValue)
|
||||
{
|
||||
Decimal result = new Decimal();
|
||||
return thisValue != null && thisValue != DBNull.Value && Decimal.TryParse(thisValue.ToString(), out result) ? result : errorValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 数据转换为DateTime类型
|
||||
/// </summary>
|
||||
/// <param name="thisValue"></param>
|
||||
/// <returns></returns>
|
||||
public static DateTime ObjectToDate(this object thisValue)
|
||||
{
|
||||
DateTime result = DateTime.MinValue;
|
||||
if (thisValue != null && thisValue != DBNull.Value && DateTime.TryParse(thisValue.ToString(), out result))
|
||||
result = Convert.ToDateTime(thisValue);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 数据转换为DateTime类型
|
||||
/// </summary>
|
||||
/// <param name="thisValue"></param>
|
||||
/// <param name="errorValue"></param>
|
||||
/// <returns></returns>
|
||||
public static DateTime ObjectToDate(this object thisValue, DateTime errorValue)
|
||||
{
|
||||
DateTime result = DateTime.MinValue;
|
||||
return thisValue != null && thisValue != DBNull.Value && DateTime.TryParse(thisValue.ToString(), out result) ? result : errorValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 数据转换为bool类型
|
||||
/// </summary>
|
||||
/// <param name="thisValue"></param>
|
||||
/// <returns></returns>
|
||||
public static bool ObjectToBool(this object thisValue)
|
||||
{
|
||||
bool result = false;
|
||||
return thisValue != null && thisValue != DBNull.Value && bool.TryParse(thisValue.ToString(), out result) ? result : result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
54
CoreCms.Net.Utility/Extensions/SerializeExtensions.cs
Normal file
54
CoreCms.Net.Utility/Extensions/SerializeExtensions.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
/***********************************************************************
|
||||
* Project: CoreCms.Net *
|
||||
* Web: https://CoreCms.Net *
|
||||
* ProjectName: 核心内容管理系统 *
|
||||
* Author: 大灰灰 *
|
||||
* Email: JianWeie@163.com *
|
||||
* Versions: 1.0 *
|
||||
* CreateTime: 2020-02-02 1:03:21
|
||||
* NameSpace: CoreCms.Net.Framework.Utility.Extensions
|
||||
* FileName: SerializeExtensions
|
||||
* ClassDescription:
|
||||
***********************************************************************/
|
||||
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace CoreCms.Net.Utility.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 字符串序列化类(用于redis数据传递保持编码格式统一)
|
||||
/// </summary>
|
||||
public static class SerializeExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 序列化
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
/// <returns></returns>
|
||||
public static byte[] Serialize(object item)
|
||||
{
|
||||
var jsonString = JsonConvert.SerializeObject(item);
|
||||
|
||||
return Encoding.UTF8.GetBytes(jsonString);
|
||||
}
|
||||
/// <summary>
|
||||
/// 反序列化
|
||||
/// </summary>
|
||||
/// <typeparam name="TEntity"></typeparam>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
public static TEntity Deserialize<TEntity>(byte[] value)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return default(TEntity);
|
||||
}
|
||||
var jsonString = Encoding.UTF8.GetString(value);
|
||||
return JsonConvert.DeserializeObject<TEntity>(jsonString);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user