/Apworks/Utils.cs

# · C# · 178 lines · 119 code · 7 blank · 52 comment · 16 complexity · d3e614d20922f7abd1b9fbde86785e71 MD5 · raw file

  1. // ==================================================================================================================
  2. // ,::i BBB
  3. // BBBBBi EBBB
  4. // MBBNBBU BBB,
  5. // BBB. BBB BBB,BBBBM BBB UBBB MBB, LBBBBBO, :BBG,BBB :BBB .BBBU kBBBBBF
  6. // BBB, BBB 7BBBBS2BBBO BBB iBBBB YBBJ :BBBMYNBBB: FBBBBBB: OBB: 5BBB, BBBi ,M,
  7. // MBBY BBB. 8BBB :BBB BBB .BBUBB BB1 BBBi kBBB BBBM BBBjBBBr BBB1
  8. // BBBBBBBBBBBu BBB FBBP MBM BB. BB BBM 7BBB MBBY .BBB 7BBGkBB1 JBBBBi
  9. // PBBBFE0GkBBBB 7BBX uBBB MBBMBu .BBOBB rBBB kBBB ZBBq BBB: BBBJ . iBBB
  10. //BBBB iBBB BBBBBBBBBE EBBBB ,BBBB MBBBBBBBM BBB, iBBB .BBB2 :BBBBBBB7
  11. //vr7 777 BBBu8O5: .77r Lr7 .7EZk; L77 .Y7r irLY JNMMF:
  12. // LBBj
  13. //
  14. // Apworks Application Development Framework
  15. // Copyright (C) 2010-2011 apworks.codeplex.com.
  16. // Licensed under the Apache License, Version 2.0 (the "License");
  17. // you may not use this file except in compliance with the License.
  18. // You may obtain a copy of the License at
  19. // http://www.apache.org/licenses/LICENSE-2.0
  20. // Unless required by applicable law or agreed to in writing, software
  21. // distributed under the License is distributed on an "AS IS" BASIS,
  22. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. // See the License for the specific language governing permissions and
  24. // limitations under the License.
  25. // ==================================================================================================================
  26. using System;
  27. using System.Reflection;
  28. using System.Security.Cryptography;
  29. using System.Text;
  30. namespace Apworks
  31. {
  32. /// <summary>
  33. /// Represents the utility class used by Apworks.
  34. /// </summary>
  35. public static class Utils
  36. {
  37. #region Private Constants
  38. private const int InitialPrime = 23;
  39. private const int FactorPrime = 29;
  40. #endregion
  41. #region Internal Extension Methods
  42. /// <summary>
  43. /// Gets the signature string.
  44. /// </summary>
  45. /// <param name="type">The type.</param>
  46. /// <returns>The signature string.</returns>
  47. public static string GetSignature(this Type type)
  48. {
  49. StringBuilder sb = new StringBuilder();
  50. if (type.IsGenericType)
  51. {
  52. sb.Append(type.GetGenericTypeDefinition().FullName);
  53. sb.Append("[");
  54. int i = 0;
  55. var genericArgs = type.GetGenericArguments();
  56. foreach (var genericArg in genericArgs)
  57. {
  58. sb.Append(genericArg.GetSignature());
  59. if (i != genericArgs.Length - 1)
  60. sb.Append(", ");
  61. i++;
  62. }
  63. sb.Append("]");
  64. }
  65. else
  66. {
  67. if (!string.IsNullOrEmpty(type.FullName))
  68. sb.Append(type.FullName);
  69. else if (!string.IsNullOrEmpty(type.Name))
  70. sb.Append(type.Name);
  71. else
  72. sb.Append(type.ToString());
  73. }
  74. return sb.ToString();
  75. }
  76. /// <summary>
  77. /// Gets the signature string.
  78. /// </summary>
  79. /// <param name="method">The method.</param>
  80. /// <returns>The signature string.</returns>
  81. public static string GetSignature(this MethodInfo method)
  82. {
  83. StringBuilder sb = new StringBuilder();
  84. Type returnType = method.ReturnType;
  85. sb.Append(method.ReturnType.GetSignature());
  86. sb.Append(" ");
  87. sb.Append(method.Name);
  88. if (method.IsGenericMethod)
  89. {
  90. sb.Append("[");
  91. var genericTypes = method.GetGenericArguments();
  92. int i = 0;
  93. foreach (var genericType in genericTypes)
  94. {
  95. sb.Append(genericType.GetSignature());
  96. if (i != genericTypes.Length - 1)
  97. sb.Append(", ");
  98. i++;
  99. }
  100. sb.Append("]");
  101. }
  102. sb.Append("(");
  103. var parameters = method.GetParameters();
  104. if (parameters != null && parameters.Length > 0)
  105. {
  106. int i = 0;
  107. foreach (var parameter in parameters)
  108. {
  109. sb.Append(parameter.ParameterType.GetSignature());
  110. if (i != parameters.Length - 1)
  111. sb.Append(", ");
  112. i++;
  113. }
  114. }
  115. sb.Append(")");
  116. return sb.ToString();
  117. }
  118. #endregion
  119. #region Public Methods
  120. /// <summary>
  121. /// Gets the hash code for an object based on the given array of hash
  122. /// codes from each property of the object.
  123. /// </summary>
  124. /// <param name="hashCodesForProperties">The array of the hash codes
  125. /// that are from each property of the object.</param>
  126. /// <returns>The hash code.</returns>
  127. public static int GetHashCode(params int[] hashCodesForProperties)
  128. {
  129. unchecked
  130. {
  131. int hash = InitialPrime;
  132. foreach (var code in hashCodesForProperties)
  133. hash = hash * FactorPrime + code;
  134. return hash;
  135. }
  136. }
  137. /// <summary>
  138. /// Generates a unique identifier represented by a <see cref="System.String"/> value
  139. /// with the specified length.
  140. /// </summary>
  141. /// <param name="length">The length of the identifier to be generated.</param>
  142. /// <returns>The unique identifier represented by a <see cref="System.String"/> value.</returns>
  143. public static string GetUniqueIdentifier(int length)
  144. {
  145. int maxSize = length;
  146. char[] chars = new char[62];
  147. string a;
  148. a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
  149. chars = a.ToCharArray();
  150. int size = maxSize;
  151. byte[] data = new byte[1];
  152. RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
  153. crypto.GetNonZeroBytes(data);
  154. size = maxSize;
  155. data = new byte[size];
  156. crypto.GetNonZeroBytes(data);
  157. StringBuilder result = new StringBuilder(size);
  158. foreach (byte b in data)
  159. {
  160. result.Append(chars[b % (chars.Length - 1)]);
  161. }
  162. // Unique identifiers cannot begin with 0-9
  163. if (result[0] >= '0' && result[0] <= '9')
  164. {
  165. return GetUniqueIdentifier(length);
  166. }
  167. return result.ToString();
  168. }
  169. #endregion
  170. }
  171. }