You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
4.1 KiB

  1. // Copyright (c) 2024 Tom Englert for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System;
  19. using System.Collections.Generic;
  20. using System.Diagnostics.CodeAnalysis;
  21. using System.Linq;
  22. using Microsoft.VisualStudio.Composition;
  23. using TomsToolbox.Composition;
  24. using TomsToolbox.Essentials;
  25. namespace ICSharpCode.ILSpy;
  26. #nullable enable
  27. /// <summary>
  28. /// Adapter for Microsoft.VisualStudio.Composition.<see cref="ExportProvider"/> to <see cref="IExportProvider"/>.
  29. /// </summary>
  30. public sealed class ExportProviderAdapter : IExportProvider
  31. {
  32. private static readonly Type DefaultMetadataType = typeof(Dictionary<string, object>);
  33. private readonly ExportProvider _exportProvider;
  34. /// <summary>
  35. /// Initializes a new instance of the <see cref="ExportProviderAdapter"/> class.
  36. /// </summary>
  37. /// <param name="exportProvider">The export provider.</param>
  38. public ExportProviderAdapter(ExportProvider exportProvider)
  39. {
  40. _exportProvider = exportProvider;
  41. }
  42. event EventHandler<EventArgs>? IExportProvider.ExportsChanged { add { } remove { } }
  43. T IExportProvider.GetExportedValue<T>(string? contractName) where T : class
  44. {
  45. return _exportProvider.GetExportedValue<T>(contractName) ?? throw new InvalidOperationException($"No export found for type {typeof(T).FullName} with contract '{contractName}'");
  46. }
  47. T? IExportProvider.GetExportedValueOrDefault<T>(string? contractName) where T : class
  48. {
  49. return _exportProvider.GetExportedValues<T>(contractName).SingleOrDefault();
  50. }
  51. bool IExportProvider.TryGetExportedValue<T>(string? contractName, [NotNullWhen(true)] out T? value) where T : class
  52. {
  53. value = _exportProvider.GetExportedValues<T>(contractName).SingleOrDefault();
  54. return !Equals(value, default(T));
  55. }
  56. IEnumerable<T> IExportProvider.GetExportedValues<T>(string? contractName) where T : class
  57. {
  58. return _exportProvider.GetExportedValues<T>(contractName);
  59. }
  60. IEnumerable<object> IExportProvider.GetExportedValues(Type contractType, string? contractName)
  61. {
  62. return _exportProvider
  63. .GetExports(contractType, DefaultMetadataType, contractName)
  64. .Select(item => item.Value)
  65. .ExceptNullItems();
  66. }
  67. IEnumerable<IExport<object>> IExportProvider.GetExports(Type contractType, string? contractName)
  68. {
  69. return _exportProvider
  70. .GetExports(contractType, DefaultMetadataType, contractName)
  71. .Select(item => new ExportAdapter<object>(() => item.Value, new MetadataAdapter((IDictionary<string, object?>)item.Metadata)));
  72. }
  73. IEnumerable<IExport<T>> IExportProvider.GetExports<T>(string? contractName) where T : class
  74. {
  75. return _exportProvider
  76. .GetExports(typeof(T), DefaultMetadataType, contractName)
  77. .Select(item => new ExportAdapter<T>(() => (T?)item.Value, new MetadataAdapter((IDictionary<string, object?>)item.Metadata)));
  78. }
  79. IEnumerable<IExport<T, TMetadataView>> IExportProvider.GetExports<T, TMetadataView>(string? contractName) where T : class where TMetadataView : class
  80. {
  81. return _exportProvider
  82. .GetExports<T, TMetadataView>(contractName)
  83. .Select(item => new ExportAdapter<T, TMetadataView>(() => item.Value, item.Metadata));
  84. }
  85. }