PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/Libraries/MEF35/ComponentModel/System/ComponentModel/Composition/ReflectionModel/ReflectionModelServices.cs

#
C# | 351 lines | 299 code | 49 blank | 3 comment | 32 complexity | fc7a210cb0306694d1df710da587da9a MD5 | raw file
  1. // -----------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. // -----------------------------------------------------------------------
  4. using System;
  5. using System.ComponentModel.Composition.Hosting;
  6. using System.ComponentModel.Composition.Primitives;
  7. using System.Reflection;
  8. using Microsoft.Internal;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Threading;
  12. using System.Globalization;
  13. namespace System.ComponentModel.Composition.ReflectionModel
  14. {
  15. public static class ReflectionModelServices
  16. {
  17. public static Lazy<Type> GetPartType(ComposablePartDefinition partDefinition)
  18. {
  19. Requires.NotNull(partDefinition, "partDefinition");
  20. ReflectionComposablePartDefinition reflectionPartDefinition = partDefinition as ReflectionComposablePartDefinition;
  21. if (reflectionPartDefinition == null)
  22. {
  23. throw new ArgumentException(
  24. string.Format(CultureInfo.CurrentCulture, Strings.ReflectionModel_InvalidPartDefinition, partDefinition.GetType()),
  25. "partDefinition");
  26. }
  27. return reflectionPartDefinition.GetLazyPartType();
  28. }
  29. public static bool IsDisposalRequired(ComposablePartDefinition partDefinition)
  30. {
  31. Requires.NotNull(partDefinition, "partDefinition");
  32. ReflectionComposablePartDefinition reflectionPartDefinition = partDefinition as ReflectionComposablePartDefinition;
  33. if (reflectionPartDefinition == null)
  34. {
  35. throw new ArgumentException(
  36. string.Format(CultureInfo.CurrentCulture, Strings.ReflectionModel_InvalidPartDefinition, partDefinition.GetType()),
  37. "partDefinition");
  38. }
  39. return reflectionPartDefinition.IsDisposalRequired;
  40. }
  41. public static LazyMemberInfo GetExportingMember(ExportDefinition exportDefinition)
  42. {
  43. Requires.NotNull(exportDefinition, "exportDefinition");
  44. ReflectionMemberExportDefinition reflectionExportDefinition = exportDefinition as ReflectionMemberExportDefinition;
  45. if (reflectionExportDefinition == null)
  46. {
  47. throw new ArgumentException(
  48. string.Format(CultureInfo.CurrentCulture, Strings.ReflectionModel_InvalidExportDefinition, exportDefinition.GetType()),
  49. "exportDefinition");
  50. }
  51. return reflectionExportDefinition.ExportingLazyMember;
  52. }
  53. public static LazyMemberInfo GetImportingMember(ImportDefinition importDefinition)
  54. {
  55. Requires.NotNull(importDefinition, "importDefinition");
  56. ReflectionMemberImportDefinition reflectionMemberImportDefinition = importDefinition as ReflectionMemberImportDefinition;
  57. if (reflectionMemberImportDefinition == null)
  58. {
  59. throw new ArgumentException(
  60. string.Format(CultureInfo.CurrentCulture, Strings.ReflectionModel_InvalidMemberImportDefinition, importDefinition.GetType()),
  61. "importDefinition");
  62. }
  63. return reflectionMemberImportDefinition.ImportingLazyMember;
  64. }
  65. public static Lazy<ParameterInfo> GetImportingParameter(ImportDefinition importDefinition)
  66. {
  67. Requires.NotNull(importDefinition, "importDefinition");
  68. ReflectionParameterImportDefinition reflectionParameterImportDefinition = importDefinition as ReflectionParameterImportDefinition;
  69. if (reflectionParameterImportDefinition == null)
  70. {
  71. throw new ArgumentException(
  72. string.Format(CultureInfo.CurrentCulture, Strings.ReflectionModel_InvalidParameterImportDefinition, importDefinition.GetType()),
  73. "importDefinition");
  74. }
  75. return reflectionParameterImportDefinition.ImportingLazyParameter;
  76. }
  77. public static bool IsImportingParameter(ImportDefinition importDefinition)
  78. {
  79. Requires.NotNull(importDefinition, "importDefinition");
  80. ReflectionImportDefinition reflectionImportDefinition = importDefinition as ReflectionImportDefinition;
  81. if (reflectionImportDefinition == null)
  82. {
  83. throw new ArgumentException(
  84. string.Format(CultureInfo.CurrentCulture, Strings.ReflectionModel_InvalidImportDefinition, importDefinition.GetType()),
  85. "importDefinition");
  86. }
  87. return (importDefinition is ReflectionParameterImportDefinition);
  88. }
  89. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
  90. public static ComposablePartDefinition CreatePartDefinition(
  91. Lazy<Type> partType,
  92. bool isDisposalRequired,
  93. Lazy<IEnumerable<ImportDefinition>> imports,
  94. Lazy<IEnumerable<ExportDefinition>> exports,
  95. Lazy<IDictionary<string, object>> metadata,
  96. ICompositionElement origin)
  97. {
  98. Requires.NotNull(partType, "partType");
  99. return new ReflectionComposablePartDefinition(
  100. new ReflectionPartCreationInfo(
  101. partType,
  102. isDisposalRequired,
  103. imports,
  104. exports,
  105. metadata,
  106. origin));
  107. }
  108. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
  109. public static ExportDefinition CreateExportDefinition(
  110. LazyMemberInfo exportingMember,
  111. string contractName,
  112. Lazy<IDictionary<string, object>> metadata,
  113. ICompositionElement origin)
  114. {
  115. Requires.NotNullOrEmpty(contractName, "contractName");
  116. Requires.IsInMembertypeSet(exportingMember.MemberType, "exportingMember", MemberTypes.Field | MemberTypes.Property | MemberTypes.NestedType | MemberTypes.TypeInfo | MemberTypes.Method);
  117. return new ReflectionMemberExportDefinition(
  118. exportingMember,
  119. new LazyExportDefinition(contractName, metadata),
  120. origin);
  121. }
  122. public static ContractBasedImportDefinition CreateImportDefinition(
  123. LazyMemberInfo importingMember,
  124. string contractName,
  125. string requiredTypeIdentity,
  126. IEnumerable<string> requiredMetadata,
  127. ImportCardinality cardinality,
  128. bool isRecomposable,
  129. CreationPolicy requiredCreationPolicy,
  130. ICompositionElement origin)
  131. {
  132. Requires.NotNullOrEmpty(contractName, "contractName");
  133. Requires.NotNullOrNullElements(requiredMetadata, "requiredMetadata");
  134. Requires.IsInMembertypeSet(importingMember.MemberType, "importingMember", MemberTypes.Property | MemberTypes.Field);
  135. return new ReflectionMemberImportDefinition(
  136. importingMember,
  137. contractName,
  138. requiredTypeIdentity,
  139. requiredMetadata,
  140. cardinality,
  141. isRecomposable,
  142. requiredCreationPolicy,
  143. origin);
  144. }
  145. public static ContractBasedImportDefinition CreateImportDefinition(
  146. Lazy<ParameterInfo> parameter,
  147. string contractName,
  148. string requiredTypeIdentity,
  149. IEnumerable<string> requiredMetadata,
  150. ImportCardinality cardinality,
  151. CreationPolicy requiredCreationPolicy,
  152. ICompositionElement origin)
  153. {
  154. Requires.NotNull(parameter, "parameter");
  155. Requires.NotNullOrEmpty(contractName, "contractName");
  156. Requires.NotNullOrNullElements(requiredMetadata, "requiredMetadata");
  157. return new ReflectionParameterImportDefinition(
  158. parameter,
  159. contractName,
  160. requiredTypeIdentity,
  161. requiredMetadata,
  162. cardinality,
  163. requiredCreationPolicy,
  164. origin);
  165. }
  166. private class ReflectionPartCreationInfo : IReflectionPartCreationInfo
  167. {
  168. private readonly Lazy<Type> _partType;
  169. private readonly Lazy<IEnumerable<ImportDefinition>> _imports;
  170. private readonly Lazy<IEnumerable<ExportDefinition>> _exports;
  171. private readonly Lazy<IDictionary<string, object>> _metadata;
  172. private readonly ICompositionElement _origin;
  173. private ConstructorInfo _constructor;
  174. private bool _isDisposalRequired;
  175. public ReflectionPartCreationInfo(
  176. Lazy<Type> partType,
  177. bool isDisposalRequired,
  178. Lazy<IEnumerable<ImportDefinition>> imports,
  179. Lazy<IEnumerable<ExportDefinition>> exports,
  180. Lazy<IDictionary<string, object>> metadata,
  181. ICompositionElement origin)
  182. {
  183. Assumes.NotNull(partType);
  184. this._partType = partType;
  185. this._isDisposalRequired = isDisposalRequired;
  186. this._imports = imports;
  187. this._exports = exports;
  188. this._metadata = metadata;
  189. this._origin = origin;
  190. }
  191. public Type GetPartType()
  192. {
  193. return this._partType.GetNotNullValue("type");
  194. }
  195. public Lazy<Type> GetLazyPartType()
  196. {
  197. return this._partType;
  198. }
  199. public ConstructorInfo GetConstructor()
  200. {
  201. if (this._constructor == null)
  202. {
  203. ConstructorInfo[] constructors = null;
  204. constructors = this.GetImports()
  205. .OfType<ReflectionParameterImportDefinition>()
  206. .Select(parameterImport => parameterImport.ImportingLazyParameter.Value.Member)
  207. .OfType<ConstructorInfo>()
  208. .Distinct()
  209. .ToArray();
  210. if (constructors.Length == 1)
  211. {
  212. this._constructor = constructors[0];
  213. }
  214. else if (constructors.Length == 0)
  215. {
  216. this._constructor = this.GetPartType().GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, null);
  217. }
  218. }
  219. return this._constructor;
  220. }
  221. public bool IsDisposalRequired
  222. {
  223. get
  224. {
  225. return this._isDisposalRequired;
  226. }
  227. }
  228. public IDictionary<string, object> GetMetadata()
  229. {
  230. return (this._metadata != null) ? this._metadata.Value : null;
  231. }
  232. public IEnumerable<ExportDefinition> GetExports()
  233. {
  234. if (this._exports == null)
  235. {
  236. yield break;
  237. }
  238. IEnumerable<ExportDefinition> exports = this._exports.Value;
  239. if (exports == null)
  240. {
  241. yield break;
  242. }
  243. foreach (ExportDefinition export in exports)
  244. {
  245. ReflectionMemberExportDefinition reflectionExport = export as ReflectionMemberExportDefinition;
  246. if (reflectionExport == null)
  247. {
  248. throw new InvalidOperationException(
  249. string.Format(CultureInfo.CurrentCulture, Strings.ReflectionModel_InvalidExportDefinition, export.GetType()));
  250. }
  251. yield return reflectionExport;
  252. }
  253. }
  254. public IEnumerable<ImportDefinition> GetImports()
  255. {
  256. if (this._imports == null)
  257. {
  258. yield break;
  259. }
  260. IEnumerable<ImportDefinition> imports = this._imports.Value;
  261. if (imports == null)
  262. {
  263. yield break;
  264. }
  265. foreach (ImportDefinition import in imports)
  266. {
  267. ReflectionImportDefinition reflectionImport = import as ReflectionImportDefinition;
  268. if (reflectionImport == null)
  269. {
  270. throw new InvalidOperationException(
  271. string.Format(CultureInfo.CurrentCulture, Strings.ReflectionModel_InvalidMemberImportDefinition, import.GetType()));
  272. }
  273. yield return reflectionImport;
  274. }
  275. }
  276. public string DisplayName
  277. {
  278. get { return this.GetPartType().GetDisplayName(); }
  279. }
  280. public ICompositionElement Origin
  281. {
  282. get { return this._origin; }
  283. }
  284. }
  285. private class LazyExportDefinition : ExportDefinition
  286. {
  287. private readonly Lazy<IDictionary<string, object>> _metadata;
  288. public LazyExportDefinition(string contractName, Lazy<IDictionary<string, object>> metadata)
  289. : base(contractName, (IDictionary<string, object>)null)
  290. {
  291. this._metadata = metadata;
  292. }
  293. public override IDictionary<string, object> Metadata
  294. {
  295. get
  296. {
  297. return this._metadata.Value;
  298. }
  299. }
  300. }
  301. }
  302. }