CommonHelper.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Web;
  10. using GreenTree.Nachtragsmanagement.Core.ComponentModel;
  11. namespace GreenTree.Nachtragsmanagement.Core
  12. {
  13. public class CommonHelper
  14. {
  15. private static AspNetHostingPermissionLevel? _trustLevel;
  16. /// <summary>
  17. /// Finds the trust level of the running application (http://blogs.msdn.com/dmitryr/archive/2007/01/23/finding-out-the-current-trust-level-in-asp-net.aspx)
  18. /// </summary>
  19. /// <returns>The current trust level.</returns>
  20. public static AspNetHostingPermissionLevel GetTrustLevel()
  21. {
  22. if (!_trustLevel.HasValue)
  23. {
  24. //set minimum
  25. _trustLevel = AspNetHostingPermissionLevel.None;
  26. //determine maximum
  27. foreach (AspNetHostingPermissionLevel trustLevel in new[] {
  28. AspNetHostingPermissionLevel.Unrestricted,
  29. AspNetHostingPermissionLevel.High,
  30. AspNetHostingPermissionLevel.Medium,
  31. AspNetHostingPermissionLevel.Low,
  32. AspNetHostingPermissionLevel.Minimal
  33. })
  34. {
  35. try
  36. {
  37. new AspNetHostingPermission(trustLevel).Demand();
  38. _trustLevel = trustLevel;
  39. break; //we've set the highest permission we can
  40. }
  41. catch (System.Security.SecurityException)
  42. {
  43. continue;
  44. }
  45. }
  46. }
  47. return _trustLevel.Value;
  48. }
  49. /// <summary>
  50. /// Sets a property on an object to a valuae.
  51. /// </summary>
  52. /// <param name="instance">The object whose property to set.</param>
  53. /// <param name="propertyName">The name of the property to set.</param>
  54. /// <param name="value">The value to set the property to.</param>
  55. public static void SetProperty(object instance, string propertyName, object value)
  56. {
  57. if (instance == null) throw new ArgumentNullException("instance");
  58. if (propertyName == null) throw new ArgumentNullException("propertyName");
  59. Type instanceType = instance.GetType();
  60. PropertyInfo pi = instanceType.GetProperty(propertyName);
  61. if (pi == null)
  62. throw new Exception(
  63. String.Format("No property '{0}' found on the instance of type '{1}'.", propertyName, instanceType));
  64. if (!pi.CanWrite)
  65. throw new Exception(
  66. String.Format("The property '{0}' on the instance of type '{1}' does not have a setter.", propertyName, instanceType));
  67. if (value != null && !value.GetType().IsAssignableFrom(pi.PropertyType))
  68. value = To(value, pi.PropertyType);
  69. pi.SetValue(instance, value, new object[0]);
  70. }
  71. public static TypeConverter GetNopCustomTypeConverter(Type type)
  72. {
  73. //we can't use the following code in order to register our custom type descriptors
  74. //TypeDescriptor.AddAttributes(typeof(List<int>), new TypeConverterAttribute(typeof(GenericListTypeConverter<int>)));
  75. //so we do it manually here
  76. if (type == typeof(List<int>))
  77. return new GenericListTypeConverter<int>();
  78. if (type == typeof(List<decimal>))
  79. return new GenericListTypeConverter<decimal>();
  80. if (type == typeof(List<string>))
  81. return new GenericListTypeConverter<string>();
  82. return TypeDescriptor.GetConverter(type);
  83. }
  84. /// <summary>
  85. /// Converts a value to a destination type.
  86. /// </summary>
  87. /// <param name="value">The value to convert.</param>
  88. /// <param name="destinationType">The type to convert the value to.</param>
  89. /// <returns>The converted value.</returns>
  90. public static object To(object value, Type destinationType)
  91. {
  92. return To(value, destinationType, CultureInfo.InvariantCulture);
  93. }
  94. /// <summary>
  95. /// Converts a value to a destination type.
  96. /// </summary>
  97. /// <param name="value">The value to convert.</param>
  98. /// <param name="destinationType">The type to convert the value to.</param>
  99. /// <param name="culture">Culture</param>
  100. /// <returns>The converted value.</returns>
  101. public static object To(object value, Type destinationType, CultureInfo culture)
  102. {
  103. if (value != null)
  104. {
  105. var sourceType = value.GetType();
  106. TypeConverter destinationConverter = GetNopCustomTypeConverter(destinationType);
  107. TypeConverter sourceConverter = GetNopCustomTypeConverter(sourceType);
  108. if (destinationConverter != null && destinationConverter.CanConvertFrom(value.GetType()))
  109. return destinationConverter.ConvertFrom(null, culture, value);
  110. if (sourceConverter != null && sourceConverter.CanConvertTo(destinationType))
  111. return sourceConverter.ConvertTo(null, culture, value, destinationType);
  112. if (destinationType.IsEnum && value is int)
  113. return Enum.ToObject(destinationType, (int)value);
  114. if (!destinationType.IsInstanceOfType(value))
  115. return Convert.ChangeType(value, destinationType, culture);
  116. }
  117. return value;
  118. }
  119. /// <summary>
  120. /// Converts a value to a destination type.
  121. /// </summary>
  122. /// <param name="value">The value to convert.</param>
  123. /// <typeparam name="T">The type to convert the value to.</typeparam>
  124. /// <returns>The converted value.</returns>
  125. public static T To<T>(object value)
  126. {
  127. //return (T)Convert.ChangeType(value, typeof(T), CultureInfo.InvariantCulture);
  128. return (T)To(value, typeof(T));
  129. }
  130. /// <summary>
  131. /// Convert enum for front-end
  132. /// </summary>
  133. /// <param name="str">Input string</param>
  134. /// <returns>Converted string</returns>
  135. public static string ConvertEnum(string str)
  136. {
  137. string result = string.Empty;
  138. char[] letters = str.ToCharArray();
  139. foreach (char c in letters)
  140. if (c.ToString() != c.ToString().ToLower())
  141. result += " " + c.ToString();
  142. else
  143. result += c.ToString();
  144. return result;
  145. }
  146. }
  147. }