(エッセンス)2020年6月27日C#クラスライブラリobject(拡張方法)
37726 ワード
using Newtonsoft.Json;
using System;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace Core.Util
{
public static partial class Extention
{
///
///
///
static Extention()
{
JsonSerializerSettings setting = new JsonSerializerSettings();
JsonConvert.DefaultSettings = new Func<JsonSerializerSettings>(() =>
{
//
setting.DateFormatHandling = DateFormatHandling.MicrosoftDateFormat;
setting.DateFormatString = "yyyy-MM-dd HH:mm:ss";
return setting;
});
}
private static BindingFlags _bindingFlags {
get; }
= BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static;
///
/// object , byte[]
///
///
///
public static byte[] ToBytes(this object obj)
{
using (MemoryStream ms = new MemoryStream())
{
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(ms, obj);
return ms.GetBuffer();
}
}
///
/// Null
///
///
///
public static bool IsNullOrEmpty(this object obj)
{
if (obj == null)
return true;
else
{
string objStr = obj.ToString();
return string.IsNullOrEmpty(objStr);
}
}
///
/// Json
///
///
///
public static string ToJson(this object obj)
{
return JsonConvert.SerializeObject(obj);
}
///
/// json ,
///
///
///
public static string EntityToJson(this object t)
{
if (t == null)
return null;
string jsonStr = "";
jsonStr += "{";
PropertyInfo[] infos = t.GetType().GetProperties();
for (int i = 0; i < infos.Length; i++)
{
jsonStr = jsonStr + "\"" + infos[i].Name + "\":\"" + infos[i].GetValue(t).ToString() + "\"";
if (i != infos.Length - 1)
jsonStr += ",";
}
jsonStr += "}";
return jsonStr;
}
///
///
///
///
///
///
public static T DeepClone<T>(this T obj) where T : class
{
if (obj == null)
return null;
return obj.ToJson().ToObject<T>();
}
///
/// XML
///
///
///
///
public static string ToXmlStr<T>(this T obj)
{
var jsonStr = obj.ToJson();
var xmlDoc = JsonConvert.DeserializeXmlNode(jsonStr);
string xmlDocStr = xmlDoc.InnerXml;
return xmlDocStr;
}
///
/// XML
///
///
///
/// ( xml)
///
public static string ToXmlStr<T>(this T obj, string rootNodeName)
{
var jsonStr = obj.ToJson();
var xmlDoc = JsonConvert.DeserializeXmlNode(jsonStr, rootNodeName);
string xmlDocStr = xmlDoc.InnerXml;
return xmlDocStr;
}
///
///
///
///
///
///
public static bool ContainsProperty(this object obj, string propertyName)
{
return obj.GetType().GetProperty(propertyName, _bindingFlags) != null;
}
///
///
///
///
///
///
public static object GetPropertyValue(this object obj, string propertyName)
{
return obj.GetType().GetProperty(propertyName, _bindingFlags).GetValue(obj);
}
///
///
///
///
///
///
///
public static void SetPropertyValue(this object obj, string propertyName, object value)
{
obj.GetType().GetProperty(propertyName, _bindingFlags).SetValue(obj, value);
}
///
///
///
///
///
///
public static bool ContainsField(this object obj, string fieldName)
{
return obj.GetType().GetField(fieldName, _bindingFlags) != null;
}
///
///
///
///
///
///
public static object GetGetFieldValue(this object obj, string fieldName)
{
return obj.GetType().GetField(fieldName, _bindingFlags).GetValue(obj);
}
///
///
///
///
///
///
///
public static void SetFieldValue(this object obj, string fieldName, object value)
{
obj.GetType().GetField(fieldName, _bindingFlags).SetValue(obj, value);
}
///
///
///
///
///
///
public static MethodInfo GetMethod(this object obj, string methodName)
{
return obj.GetType().GetMethod(methodName, _bindingFlags);
}
///
///
///
///
///
///
public static object ChangeType(this object obj, Type targetType)
{
return obj.ToJson().ToObject(targetType);
}
///
///
///
///
///
///
public static T ChangeType<T>(this object obj)
{
return obj.ToJson().ToObject<T>();
}
///
///
///
///
///
///
public static object ChangeType_ByConvert(this object obj, Type targetType)
{
object resObj;
if (targetType.IsGenericType && targetType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
{
NullableConverter newNullableConverter = new NullableConverter(targetType);
resObj = newNullableConverter.ConvertFrom(obj);
}
else
{
resObj = Convert.ChangeType(obj, targetType);
}
return resObj;
}
}
}