CopyHelper.cs 6.8 KB

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