CopyHelper.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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="TSource">The type of the source.</typeparam>
  71. /// <typeparam name="TDest">The type of the dest.</typeparam>
  72. /// <param name="source">The source.</param>
  73. /// <param name="destination">The destination.</param>
  74. /// <param name="updateProperties">The update properties (seperator by comma).</param>
  75. /// <returns></returns>
  76. public static TDest SimpleMap<TSource, TDest>(this TSource source, TDest destination = null, string updateProperties = null)
  77. where TSource : class
  78. where TDest : class
  79. {
  80. Type destType = typeof(TDest);
  81. if (destination == null)
  82. {
  83. destination = (TDest)Activator.CreateInstance(destType);
  84. }
  85. Span<PropertyInfo> sourceProperties = null;
  86. if(string.IsNullOrWhiteSpace(updateProperties))
  87. {
  88. sourceProperties = source.GetType().GetProperties().AsSpan();
  89. }
  90. else
  91. {
  92. sourceProperties = (from sp in source.GetType().GetProperties()
  93. join propName in updateProperties.SplitCommaString() on sp.Name equals propName
  94. select sp).ToArray().AsSpan();
  95. }
  96. var destinationProperties = destType.GetProperties().AsSpan();
  97. for (int si = 0; si < sourceProperties.Length; si++)
  98. {
  99. PropertyInfo sourceProperty = sourceProperties[si];
  100. for (int di = 0; di < destinationProperties.Length; di++)
  101. {
  102. PropertyInfo destinationProperty = destinationProperties[si];
  103. if (sourceProperty.Name.Equals(destinationProperty.Name, StringComparison.InvariantCultureIgnoreCase) && destinationProperty.CanWrite)
  104. {
  105. var sourceValue = sourceProperty.GetValue(source);
  106. destinationProperty.SetValue(destination, sourceProperty.PropertyType == destinationProperty.PropertyType ? sourceValue : sourceValue.ChangeType(destType));
  107. break;
  108. }
  109. }
  110. }
  111. return destination;
  112. }
  113. /// <summary>
  114. /// Copies the specified source.
  115. /// </summary>
  116. /// <typeparam name="TSource">The type of the source.</typeparam>
  117. /// <typeparam name="TDest">The type of the dest.</typeparam>
  118. /// <param name="dest">The dest.</param>
  119. /// <param name="source">The source.</param>
  120. /// <param name="caseSensitive">if set to <c>true</c> [case sensitive].</param>
  121. /// <param name="predicate">The predicate.</param>
  122. /// <param name="setter">The setter.</param>
  123. /// <returns></returns>
  124. 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)
  125. where TSource : class
  126. where TDest : class
  127. {
  128. if (source.IsDictionary() && source.HasGenerictArgumentType<string>(0))
  129. {
  130. var props = TypeDescriptor.GetProperties(dest);
  131. var machingNames = from prop in props.OfType<PropertyDescriptor>()
  132. from val in (IDictionary<string, object>)source
  133. where prop.Name.Equals(val.Key, (caseSensitive ? StringComparison.InvariantCulture : StringComparison.InvariantCultureIgnoreCase))
  134. && (predicate == null || predicate(prop.Name, val.Value))
  135. select new { Property = prop, Value = val.Value };
  136. machingNames.ForEach(m => ReflectionHelper.SetPropertyValue(dest, m.Property, (setter == null ? m.Value : setter(m.Property.Name, m.Value))));
  137. }
  138. else
  139. {
  140. var sourceProps = TypeDescriptor.GetProperties(source);
  141. var destProps = TypeDescriptor.GetProperties(dest);
  142. var machingProps = from s in sourceProps.OfType<PropertyDescriptor>()
  143. from d in destProps.OfType<PropertyDescriptor>()
  144. where s.Name.Equals(d.Name, (caseSensitive ? StringComparison.InvariantCulture : StringComparison.InvariantCultureIgnoreCase))
  145. && (predicate == null || predicate(d.Name, s.GetValue(source)))
  146. select new { SourceProp = s, DestProp = d };
  147. machingProps.ForEach(m =>
  148. {
  149. var value = m.SourceProp.GetValue(source);
  150. ReflectionHelper.SetPropertyValue(dest, m.DestProp, (setter == null ? value : setter(m.DestProp.Name, value)));
  151. });
  152. }
  153. return dest;
  154. }
  155. }
  156. }