CopyHelper.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections;
  6. using System.Runtime.Serialization;
  7. using System.Runtime.Serialization.Formatters.Binary;
  8. using System.IO;
  9. using System.Text.Json;
  10. using System.ComponentModel;
  11. using System.Reflection;
  12. namespace EasyDevCore.Common
  13. {
  14. /// <summary>
  15. ///
  16. /// </summary>
  17. public static class CopyHelper
  18. {
  19. /// <summary>
  20. /// Clones the hashtable.
  21. /// </summary>
  22. /// <param name="input">The input.</param>
  23. /// <returns></returns>
  24. public static Hashtable CloneHashtable(this Hashtable input)
  25. {
  26. Hashtable ret = new Hashtable();
  27. foreach (DictionaryEntry dictionaryEntry in input)
  28. {
  29. if (dictionaryEntry.Value is string)
  30. {
  31. ret.Add(dictionaryEntry.Key, dictionaryEntry.Value != null ? (string)dictionaryEntry.Value : null);
  32. }
  33. else if (dictionaryEntry.Value is Hashtable)
  34. {
  35. ret.Add(dictionaryEntry.Key, CloneHashtable((Hashtable)dictionaryEntry.Value));
  36. }
  37. else if (dictionaryEntry.Value is ArrayList)
  38. {
  39. ret.Add(dictionaryEntry.Key, new ArrayList((ArrayList)dictionaryEntry.Value));
  40. }
  41. }
  42. return ret;
  43. }
  44. /// <summary>
  45. /// Clones the specified source.
  46. /// </summary>
  47. /// <typeparam name="T"></typeparam>
  48. /// <param name="source">The source.</param>
  49. /// <returns>T.</returns>
  50. /// <exception cref="ArgumentException">The type must be serializable.;source</exception>
  51. public static T CloneObject<T>(this T source)
  52. {
  53. #pragma warning disable SYSLIB0050 // Type or member is obsolete
  54. if (!typeof(T).IsSerializable)
  55. {
  56. throw new ArgumentException("The type must be serializable.", "source");
  57. }
  58. #pragma warning restore SYSLIB0050 // Type or member is obsolete
  59. // Don't serialize a null object, simply return the default for that object
  60. if (source == null)
  61. {
  62. return default(T);
  63. }
  64. var json = JsonSerializer.Serialize(source, source.GetType());
  65. return string.IsNullOrWhiteSpace(json) ? default(T) : (T)JsonSerializer.Deserialize(json, source.GetType());
  66. }
  67. /// <summary>
  68. /// Simples the map.
  69. /// </summary>
  70. /// <typeparam name="TDest">The type of the dest.</typeparam>
  71. /// <param name="source">The source.</param>
  72. /// <param name="updateProperties">The update properties.</param>
  73. /// <param name="exceptProperties">The except properties.</param>
  74. /// <returns></returns>
  75. public static TDest SimpleMap<TDest>(this object source, string updateProperties = null, string exceptProperties = null)
  76. where TDest : class, new()
  77. {
  78. var dest = new TDest();
  79. return SimpleMap<TDest>(source, dest, updateProperties, exceptProperties);
  80. }
  81. /// <summary>
  82. /// Simples the map.
  83. /// </summary>
  84. /// <typeparam name="TDest">The type of the dest.</typeparam>
  85. /// <param name="source">The source.</param>
  86. /// <param name="destination">The destination.</param>
  87. /// <param name="updateProperties">The update properties.</param>
  88. /// <param name="exceptProperties">The except properties.</param>
  89. /// <returns></returns>
  90. public static TDest SimpleMap<TDest>(this object source, TDest destination, string updateProperties = null, string exceptProperties = null)
  91. where TDest : class
  92. {
  93. Type destType = destination.GetType();
  94. Span<PropertyInfo> sourceProperties = null;
  95. if(string.IsNullOrWhiteSpace(updateProperties))
  96. {
  97. sourceProperties = source.GetType().GetProperties().Where(x => x.CanRead).ToArray().AsSpan();
  98. }
  99. else
  100. {
  101. sourceProperties = (from sp in source.GetType().GetProperties().Where(x => x.CanRead)
  102. join propName in updateProperties.Split(',') on sp.Name equals propName
  103. select sp).ToArray().AsSpan();
  104. }
  105. Span<PropertyInfo> destinationProperties = null;
  106. if (string.IsNullOrWhiteSpace(exceptProperties))
  107. {
  108. destinationProperties = destType.GetProperties().Where(x => x.CanWrite).ToArray().AsSpan();
  109. }
  110. else
  111. {
  112. var exceptPropertiesArray = exceptProperties.Split(',');
  113. destinationProperties = destType.GetProperties().Where(x => exceptPropertiesArray.Contains(x.Name)).Where(x => x.CanWrite).ToArray().AsSpan();
  114. }
  115. for (int si = 0; si < sourceProperties.Length; si++)
  116. {
  117. PropertyInfo sourceProperty = sourceProperties[si];
  118. for (int di = 0; di < destinationProperties.Length; di++)
  119. {
  120. PropertyInfo destinationProperty = destinationProperties[di];
  121. if (sourceProperty.Name.Equals(destinationProperty.Name, StringComparison.InvariantCultureIgnoreCase))
  122. {
  123. var sourceValue = sourceProperty.GetValue(source);
  124. destinationProperty.SetValue(destination, sourceProperty.PropertyType == destinationProperty.PropertyType ? sourceValue : sourceValue.ChangeType(destType));
  125. break;
  126. }
  127. }
  128. }
  129. return destination;
  130. }
  131. /// <summary>
  132. /// Copies the specified source.
  133. /// </summary>
  134. /// <typeparam name="TSource">The type of the source.</typeparam>
  135. /// <typeparam name="TDest">The type of the dest.</typeparam>
  136. /// <param name="dest">The dest.</param>
  137. /// <param name="source">The source.</param>
  138. /// <param name="caseSensitive">if set to <c>true</c> [case sensitive].</param>
  139. /// <param name="predicate">The predicate.</param>
  140. /// <param name="setter">The setter.</param>
  141. /// <returns></returns>
  142. public static TDest Copy<TSource, TDest>(this TDest dest, TSource source, bool caseSensitive = false, Func<string, object, bool> predicate = null, Func<string, object, object> setter = null)
  143. where TSource : class
  144. where TDest : class
  145. {
  146. if (source.IsDictionary() && source.HasGenerictArgumentType<string>(0))
  147. {
  148. var props = TypeDescriptor.GetProperties(dest);
  149. var machingNames = from prop in props.OfType<PropertyDescriptor>()
  150. from val in (IDictionary<string, object>)source
  151. where prop.Name.Equals(val.Key, (caseSensitive ? StringComparison.InvariantCulture : StringComparison.InvariantCultureIgnoreCase))
  152. && (predicate == null || predicate(prop.Name, val.Value))
  153. select new { Property = prop, Value = val.Value };
  154. machingNames.ForEach(m => ReflectionHelper.SetPropertyValue(dest, m.Property, (setter == null ? m.Value : setter(m.Property.Name, m.Value))));
  155. }
  156. else
  157. {
  158. var sourceProps = TypeDescriptor.GetProperties(source);
  159. var destProps = TypeDescriptor.GetProperties(dest);
  160. var machingProps = from s in sourceProps.OfType<PropertyDescriptor>()
  161. from d in destProps.OfType<PropertyDescriptor>()
  162. where s.Name.Equals(d.Name, (caseSensitive ? StringComparison.InvariantCulture : StringComparison.InvariantCultureIgnoreCase))
  163. && (predicate == null || predicate(d.Name, s.GetValue(source)))
  164. select new { SourceProp = s, DestProp = d };
  165. machingProps.ForEach(m =>
  166. {
  167. var value = m.SourceProp.GetValue(source);
  168. ReflectionHelper.SetPropertyValue(dest, m.DestProp, (setter == null ? value : setter(m.DestProp.Name, value)));
  169. });
  170. }
  171. return dest;
  172. }
  173. }
  174. }