-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d0d60f9
commit ea2fc92
Showing
10 changed files
with
135 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Linq; | ||
|
||
using Microsoft.VisualStudio.Composition; | ||
|
||
using TomsToolbox.Composition; | ||
using TomsToolbox.Essentials; | ||
|
||
namespace ICSharpCode.ILSpy; | ||
|
||
#nullable enable | ||
|
||
/// <summary> | ||
/// Adapter for Microsoft.VisualStudio.Composition.<see cref="ExportProvider"/> to <see cref="IExportProvider"/>. | ||
/// </summary> | ||
public class ExportProviderAdapter : IExportProvider | ||
{ | ||
private static readonly Type DefaultMetadataType = typeof(Dictionary<string, object>); | ||
|
||
private readonly ExportProvider _exportProvider; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ExportProviderAdapter"/> class. | ||
/// </summary> | ||
/// <param name="exportProvider">The export provider.</param> | ||
public ExportProviderAdapter(ExportProvider exportProvider) | ||
{ | ||
_exportProvider = exportProvider; | ||
} | ||
|
||
event EventHandler<EventArgs>? IExportProvider.ExportsChanged { add { } remove { } } | ||
|
||
T IExportProvider.GetExportedValue<T>(string? contractName) where T : class | ||
{ | ||
return _exportProvider.GetExportedValue<T>(contractName) ?? throw new InvalidOperationException($"No export found for type {typeof(T).FullName} with contract '{contractName}'"); | ||
} | ||
|
||
T? IExportProvider.GetExportedValueOrDefault<T>(string? contractName) where T : class | ||
{ | ||
return _exportProvider.GetExportedValues<T>(contractName).SingleOrDefault(); | ||
} | ||
|
||
#pragma warning disable CS8769 // Nullability of reference types in type of parameter doesn't match implemented member (possibly because of nullability attributes). | ||
// can't apply NotNullWhen here, because ICSharpCode.Decompiler defines a duplicate attribute, and uses InternalsVisibleTo("ILSpy"), so this attribute is now ambiguous! | ||
bool IExportProvider.TryGetExportedValue<T>(string? contractName, /*[NotNullWhen(true)]*/ out T? value) where T : class | ||
#pragma warning restore CS8769 // Nullability of reference types in type of parameter doesn't match implemented member (possibly because of nullability attributes). | ||
{ | ||
value = _exportProvider.GetExportedValues<T>(contractName).SingleOrDefault(); | ||
|
||
return !Equals(value, default(T)); | ||
} | ||
|
||
IEnumerable<T> IExportProvider.GetExportedValues<T>(string? contractName) where T : class | ||
{ | ||
return _exportProvider.GetExportedValues<T>(contractName); | ||
} | ||
|
||
IEnumerable<object> IExportProvider.GetExportedValues(Type contractType, string? contractName) | ||
{ | ||
return _exportProvider | ||
.GetExports(contractType, DefaultMetadataType, contractName) | ||
.Select(item => item.Value) | ||
.ExceptNullItems(); | ||
} | ||
|
||
IEnumerable<IExport<object>> IExportProvider.GetExports(Type contractType, string? contractName) | ||
{ | ||
return _exportProvider | ||
.GetExports(contractType, DefaultMetadataType, contractName) | ||
.Select(item => new ExportAdapter<object>(() => item.Value, new MetadataAdapter((IDictionary<string, object?>)item.Metadata))); | ||
} | ||
|
||
IEnumerable<IExport<T>> IExportProvider.GetExports<T>(string? contractName) where T : class | ||
{ | ||
return _exportProvider | ||
.GetExports(typeof(T), DefaultMetadataType, contractName) | ||
.Select(item => new ExportAdapter<T>(() => (T?)item.Value, new MetadataAdapter((IDictionary<string, object?>)item.Metadata))); | ||
} | ||
|
||
IEnumerable<IExport<T, TMetadataView>> IExportProvider.GetExports<T, TMetadataView>(string? contractName) where T : class where TMetadataView : class | ||
{ | ||
return _exportProvider | ||
.GetExports<T, TMetadataView>(contractName) | ||
.Select(item => new ExportAdapter<T, TMetadataView>(() => item.Value, item.Metadata)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters