/SolutionFramework/Microsoft.ServiceModel.DomainServices.Tools/Microsoft/ServiceModel/DomainServices/Tools/SharedTypes/SharedAssemblies.cs

# · C# · 236 lines · 219 code · 17 blank · 0 comment · 40 complexity · b9d2ebd6a5e4d7c975c6802a04566f76 MD5 · raw file

  1. namespace DomainServices.Tools.SharedTypes
  2. {
  3. using DomainServices.Tools;
  4. using System;
  5. using System.Collections.Concurrent;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.ServiceModel.DomainServices;
  10. internal class SharedAssemblies
  11. {
  12. private List<Assembly> _assemblies;
  13. private List<string> _assemblyFileNames;
  14. private IEnumerable<string> _assemblySearchPaths;
  15. private ILogger _logger;
  16. private ConcurrentDictionary<string, Type> _sharedTypeByName = new ConcurrentDictionary<string, Type>(StringComparer.OrdinalIgnoreCase);
  17. internal SharedAssemblies(IEnumerable<string> assemblyFileNames, IEnumerable<string> assemblySearchPaths, ILogger logger)
  18. {
  19. if (assemblyFileNames == null)
  20. {
  21. throw new ArgumentNullException("assemblyFileNames");
  22. }
  23. this._logger = logger;
  24. this._assemblyFileNames = new List<string>(assemblyFileNames);
  25. this._assemblySearchPaths = assemblySearchPaths ?? ((IEnumerable<string>) new string[0]);
  26. }
  27. private static Type EquivalentMsCorlibType(Type type)
  28. {
  29. if (!AssemblyUtilities.IsAssemblyMsCorlib(type.Assembly.GetName()))
  30. {
  31. return null;
  32. }
  33. if (!System.ServiceModel.DomainServices.TypeUtility.IsPredefinedType(type))
  34. {
  35. return null;
  36. }
  37. return type;
  38. }
  39. private MethodBase FindSharedMethodOrConstructor(Type sharedType, CodeMemberKey key)
  40. {
  41. Type[] sharedTypes = this.GetSharedTypes(key.ParameterTypeNames);
  42. if (sharedTypes != null)
  43. {
  44. bool isConstructor = key.IsConstructor;
  45. IEnumerable<MethodBase> enumerable = isConstructor ? sharedType.GetConstructors().Cast<MethodBase>() : sharedType.GetMethods().Cast<MethodBase>();
  46. foreach (MethodBase base2 in enumerable)
  47. {
  48. if (!isConstructor && !string.Equals(base2.Name, key.MemberName, StringComparison.OrdinalIgnoreCase))
  49. {
  50. continue;
  51. }
  52. ParameterInfo[] parameters = base2.GetParameters();
  53. if (parameters.Length == sharedTypes.Length)
  54. {
  55. int num = 0;
  56. for (int i = 0; i < parameters.Length; i++)
  57. {
  58. if (!string.Equals(parameters[i].ParameterType.FullName, sharedTypes[i].FullName, StringComparison.OrdinalIgnoreCase))
  59. {
  60. break;
  61. }
  62. num++;
  63. }
  64. if (num == parameters.Length)
  65. {
  66. return base2;
  67. }
  68. }
  69. }
  70. }
  71. return null;
  72. }
  73. private Type FindSharedType(string typeName)
  74. {
  75. Type type = Type.GetType(typeName, false);
  76. if (type != null)
  77. {
  78. return this.FindSharedType(type);
  79. }
  80. foreach (Assembly assembly in this.Assemblies)
  81. {
  82. foreach (Type type2 in AssemblyUtilities.GetExportedTypes(assembly, this._logger))
  83. {
  84. if (string.Equals(typeName, type2.FullName, StringComparison.OrdinalIgnoreCase))
  85. {
  86. return type2;
  87. }
  88. }
  89. }
  90. return null;
  91. }
  92. private Type FindSharedType(Type type)
  93. {
  94. foreach (Assembly assembly in this.Assemblies)
  95. {
  96. foreach (Type type2 in AssemblyUtilities.GetExportedTypes(assembly, this._logger))
  97. {
  98. if (IsSameType(type, type2))
  99. {
  100. return type2;
  101. }
  102. }
  103. }
  104. return EquivalentMsCorlibType(type);
  105. }
  106. internal string GetSharedAssemblyPath(CodeMemberKey key)
  107. {
  108. string location = null;
  109. Type sharedType = this.GetSharedType(key.TypeName);
  110. if (sharedType != null)
  111. {
  112. switch (key.KeyKind)
  113. {
  114. case CodeMemberKey.CodeMemberKeyKind.TypeKey:
  115. return sharedType.Assembly.Location;
  116. case CodeMemberKey.CodeMemberKeyKind.PropertyKey:
  117. {
  118. PropertyInfo property = sharedType.GetProperty(key.MemberName);
  119. if (property != null)
  120. {
  121. location = property.DeclaringType.Assembly.Location;
  122. }
  123. return location;
  124. }
  125. case CodeMemberKey.CodeMemberKeyKind.MethodKey:
  126. if (this.GetSharedTypes(key.ParameterTypeNames) != null)
  127. {
  128. MethodBase base2 = this.FindSharedMethodOrConstructor(sharedType, key);
  129. if (base2 != null)
  130. {
  131. location = base2.DeclaringType.Assembly.Location;
  132. }
  133. }
  134. return location;
  135. }
  136. }
  137. return location;
  138. }
  139. internal MethodBase GetSharedMethod(string typeName, string methodName, IEnumerable<string> parameterTypeNames)
  140. {
  141. MethodBase base2 = null;
  142. Type sharedType = this.GetSharedType(typeName);
  143. if (sharedType != null)
  144. {
  145. CodeMemberKey key = CodeMemberKey.CreateMethodKey(typeName, methodName, (parameterTypeNames == null) ? new string[0] : parameterTypeNames.ToArray<string>());
  146. base2 = this.FindSharedMethodOrConstructor(sharedType, key);
  147. }
  148. return base2;
  149. }
  150. internal PropertyInfo GetSharedProperty(string typeName, string propertyName)
  151. {
  152. PropertyInfo property = null;
  153. Type sharedType = this.GetSharedType(typeName);
  154. if (sharedType != null)
  155. {
  156. property = sharedType.GetProperty(propertyName);
  157. }
  158. return property;
  159. }
  160. internal Type GetSharedType(string typeName)
  161. {
  162. return this._sharedTypeByName.GetOrAdd(typeName, n => this.FindSharedType(n));
  163. }
  164. internal Type GetSharedType(Type type)
  165. {
  166. return this._sharedTypeByName.GetOrAdd(type.FullName, n => this.FindSharedType(type));
  167. }
  168. internal Type[] GetSharedTypes(IEnumerable<string> typeNames)
  169. {
  170. List<Type> list = new List<Type>();
  171. if (typeNames != null)
  172. {
  173. foreach (string str in typeNames)
  174. {
  175. Type item = this.FindSharedType(str);
  176. if (item == null)
  177. {
  178. return null;
  179. }
  180. list.Add(item);
  181. }
  182. }
  183. return list.ToArray();
  184. }
  185. private static bool IsSameType(Type type1, Type type2)
  186. {
  187. return type1.FullName.Equals(type2.FullName, StringComparison.Ordinal);
  188. }
  189. private void LoadAssemblies()
  190. {
  191. this._assemblies = new List<Assembly>();
  192. Dictionary<string, Assembly> loadedAssemblies = new Dictionary<string, Assembly>();
  193. foreach (string str in this._assemblyFileNames)
  194. {
  195. Assembly item = AssemblyUtilities.ReflectionOnlyLoadFrom(str, this._logger);
  196. if (item != null)
  197. {
  198. this._assemblies.Add(item);
  199. loadedAssemblies[item.FullName] = item;
  200. }
  201. }
  202. foreach (Assembly assembly2 in this._assemblies)
  203. {
  204. AssemblyUtilities.ReflectionOnlyLoadReferences(assembly2, this._assemblySearchPaths, loadedAssemblies, true, this._logger);
  205. }
  206. }
  207. private IEnumerable<Assembly> Assemblies
  208. {
  209. get
  210. {
  211. if (this._assemblies == null)
  212. {
  213. this.LoadAssemblies();
  214. }
  215. return this._assemblies;
  216. }
  217. }
  218. }
  219. }