Extend
public static class Extend { public static DateTime Local(this DateTime now) { return DateTime.Parse(now.ToString("yyyy-MM-dd HH:mm:ss")); } public static byte[] ToBin(this object obj) { return MessagePack.MessagePackSerializer.Serialize(obj.GetType(), obj , MessagePack.MessagePackSerializerOptions.Standard .WithResolver(MessagePack.Resolvers.ContractlessStandardResolverAllowPrivate.Instance)); } public static T FromBin<T>(this byte[] bytes) { ReadOnlyMemory<byte> temp = new ReadOnlyMemory<byte>(bytes); return (T)MessagePack.MessagePackSerializer.Deserialize(typeof(T), temp , MessagePack.MessagePackSerializerOptions.Standard .WithResolver(MessagePack.Resolvers.ContractlessStandardResolverAllowPrivate.Instance)); } public static T ToObj<T>(this object obj) { var bytes = MessagePack.MessagePackSerializer.Serialize(obj.GetType(), obj , MessagePack.MessagePackSerializerOptions.Standard .WithResolver(MessagePack.Resolvers.ContractlessStandardResolverAllowPrivate.Instance)); ReadOnlyMemory<byte> temp = new ReadOnlyMemory<byte>(bytes); return (T)MessagePack.MessagePackSerializer.Deserialize(typeof(T), temp , MessagePack.MessagePackSerializerOptions.Standard .WithResolver(MessagePack.Resolvers.ContractlessStandardResolverAllowPrivate.Instance)); } /// <summary> /// 比较两个对象各属性值是否一样,非对象的Hashcode是否一样 /// </summary> /// <param name="source"></param> /// <param name="dest"></param> /// <returns></returns> public static bool Diff(this object source, object dest) { var binSource= MessagePack.MessagePackSerializer.Serialize(source.GetType(), source , MessagePack.MessagePackSerializerOptions.Standard .WithResolver(MessagePack.Resolvers.ContractlessStandardResolverAllowPrivate.Instance)); var binDest = MessagePack.MessagePackSerializer.Serialize(dest.GetType(), dest , MessagePack.MessagePackSerializerOptions.Standard .WithResolver(MessagePack.Resolvers.ContractlessStandardResolverAllowPrivate.Instance)); using (var md5 = new System.Security.Cryptography.MD5CryptoServiceProvider()) { byte[] sourceEncryptdata = md5.ComputeHash(binSource); var sourceStr = System.BitConverter.ToString(sourceEncryptdata); sourceStr = sourceStr.Replace("-", "").ToUpper(); byte[] destEncryptdata = md5.ComputeHash(binDest); var destStr = System.BitConverter.ToString(destEncryptdata); destStr = destStr.Replace("-", "").ToUpper(); return sourceStr == destStr; } } /// <summary> /// 综合对象各属性值,计算其MD5 /// </summary> /// <param name="obj"></param> /// <returns></returns> public static string Md5(this object obj) { var bin = MessagePack.MessagePackSerializer.Serialize(obj.GetType(), obj , MessagePack.MessagePackSerializerOptions.Standard .WithResolver(MessagePack.Resolvers.ContractlessStandardResolverAllowPrivate.Instance)); using (var md5 = new System.Security.Cryptography.MD5CryptoServiceProvider()) { byte[] encryptdata = md5.ComputeHash(bin); var str = System.BitConverter.ToString(encryptdata); str = str.Replace("-", "").ToUpper(); return str; } } }
posted on 2020-09-11 22:15 jonney_wang 阅读(193) 评论(0) 编辑 收藏 举报