CommonHelper.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 Autofac;
  11. using Autofac.Integration.Mvc;
  12. using GreenTree.Nachtragsmanagement.Core.ComponentModel;
  13. using GreenTree.Nachtragsmanagement.Core.Domain.User;
  14. using GreenTree.Nachtragsmanagement.Core.Authentication;
  15. namespace GreenTree.Nachtragsmanagement.Core
  16. {
  17. public static class CommonHelper
  18. {
  19. private static AspNetHostingPermissionLevel? _trustLevel;
  20. /// <summary>
  21. /// 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)
  22. /// </summary>
  23. /// <returns>The current trust level.</returns>
  24. public static AspNetHostingPermissionLevel GetTrustLevel()
  25. {
  26. if (!_trustLevel.HasValue)
  27. {
  28. //set minimum
  29. _trustLevel = AspNetHostingPermissionLevel.None;
  30. //determine maximum
  31. foreach (AspNetHostingPermissionLevel trustLevel in new[] {
  32. AspNetHostingPermissionLevel.Unrestricted,
  33. AspNetHostingPermissionLevel.High,
  34. AspNetHostingPermissionLevel.Medium,
  35. AspNetHostingPermissionLevel.Low,
  36. AspNetHostingPermissionLevel.Minimal
  37. })
  38. {
  39. try
  40. {
  41. new AspNetHostingPermission(trustLevel).Demand();
  42. _trustLevel = trustLevel;
  43. break; //we've set the highest permission we can
  44. }
  45. catch (System.Security.SecurityException)
  46. {
  47. continue;
  48. }
  49. }
  50. }
  51. return _trustLevel.Value;
  52. }
  53. #region Conversion
  54. /// <summary>
  55. /// Sets a property on an object to a valuae.
  56. /// </summary>
  57. /// <param name="instance">The object whose property to set.</param>
  58. /// <param name="propertyName">The name of the property to set.</param>
  59. /// <param name="value">The value to set the property to.</param>
  60. public static void SetProperty(object instance, string propertyName, object value)
  61. {
  62. if (instance == null) throw new ArgumentNullException("instance");
  63. if (propertyName == null) throw new ArgumentNullException("propertyName");
  64. Type instanceType = instance.GetType();
  65. PropertyInfo pi = instanceType.GetProperty(propertyName);
  66. if (pi == null)
  67. throw new Exception(
  68. String.Format("No property '{0}' found on the instance of type '{1}'.", propertyName, instanceType));
  69. if (!pi.CanWrite)
  70. throw new Exception(
  71. String.Format("The property '{0}' on the instance of type '{1}' does not have a setter.", propertyName, instanceType));
  72. if (value != null && !value.GetType().IsAssignableFrom(pi.PropertyType))
  73. value = To(value, pi.PropertyType);
  74. pi.SetValue(instance, value, new object[0]);
  75. }
  76. public static TypeConverter GetCustomTypeConverter(Type type)
  77. {
  78. //we can't use the following code in order to register our custom type descriptors
  79. //TypeDescriptor.AddAttributes(typeof(List<int>), new TypeConverterAttribute(typeof(GenericListTypeConverter<int>)));
  80. //so we do it manually here
  81. if (type == typeof(List<int>))
  82. return new GenericListTypeConverter<int>();
  83. if (type == typeof(List<decimal>))
  84. return new GenericListTypeConverter<decimal>();
  85. if (type == typeof(List<string>))
  86. return new GenericListTypeConverter<string>();
  87. return TypeDescriptor.GetConverter(type);
  88. }
  89. /// <summary>
  90. /// Converts a value to a destination type.
  91. /// </summary>
  92. /// <param name="value">The value to convert.</param>
  93. /// <param name="destinationType">The type to convert the value to.</param>
  94. /// <returns>The converted value.</returns>
  95. public static object To(object value, Type destinationType)
  96. {
  97. return To(value, destinationType, CultureInfo.InvariantCulture);
  98. }
  99. /// <summary>
  100. /// Converts a value to a destination type.
  101. /// </summary>
  102. /// <param name="value">The value to convert.</param>
  103. /// <param name="destinationType">The type to convert the value to.</param>
  104. /// <param name="culture">Culture</param>
  105. /// <returns>The converted value.</returns>
  106. public static object To(object value, Type destinationType, CultureInfo culture)
  107. {
  108. if (value != null)
  109. {
  110. var sourceType = value.GetType();
  111. TypeConverter destinationConverter = GetCustomTypeConverter(destinationType);
  112. TypeConverter sourceConverter = GetCustomTypeConverter(sourceType);
  113. if (destinationConverter != null && destinationConverter.CanConvertFrom(value.GetType()))
  114. return destinationConverter.ConvertFrom(null, culture, value);
  115. if (sourceConverter != null && sourceConverter.CanConvertTo(destinationType))
  116. return sourceConverter.ConvertTo(null, culture, value, destinationType);
  117. if (destinationType.IsEnum && value is int)
  118. return Enum.ToObject(destinationType, (int)value);
  119. if (!destinationType.IsInstanceOfType(value))
  120. return Convert.ChangeType(value, destinationType, culture);
  121. }
  122. return value;
  123. }
  124. /// <summary>
  125. /// Converts a value to a destination type.
  126. /// </summary>
  127. /// <param name="value">The value to convert.</param>
  128. /// <typeparam name="T">The type to convert the value to.</typeparam>
  129. /// <returns>The converted value.</returns>
  130. public static T To<T>(object value)
  131. {
  132. //return (T)Convert.ChangeType(value, typeof(T), CultureInfo.InvariantCulture);
  133. return (T)To(value, typeof(T));
  134. }
  135. /// <summary>
  136. /// Convert enum for front-end
  137. /// </summary>
  138. /// <param name="str">Input string</param>
  139. /// <returns>Converted string</returns>
  140. public static string ConvertEnum(string str)
  141. {
  142. string result = string.Empty;
  143. char[] letters = str.ToCharArray();
  144. foreach (char c in letters)
  145. if (c.ToString() != c.ToString().ToLower())
  146. result += " " + c.ToString();
  147. else
  148. result += c.ToString();
  149. return result;
  150. }
  151. #endregion
  152. #region User-Handling
  153. /// <summary>
  154. /// Evaluate the user context of the current logged in user
  155. /// </summary>
  156. public static UserContext UserContext()
  157. {
  158. var container = Singleton<Autofac.IContainer>.Instance;
  159. var userHelper = container.Resolve<IUserHelper>();
  160. return new UserContext
  161. {
  162. CurrentUser = userHelper.FromCookies()
  163. };
  164. }
  165. #endregion
  166. }
  167. }