-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
- Loading branch information
There are no files selected for viewing
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 | ||
Check warning on line 35 in ILSpy/ExportProviderAdapter.cs GitHub Actions / Build (Release)
|
||
{ | ||
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 | ||
Check warning on line 40 in ILSpy/ExportProviderAdapter.cs GitHub Actions / Build (Debug)
Check warning on line 40 in ILSpy/ExportProviderAdapter.cs GitHub Actions / Build (Release)
|
||
{ | ||
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 | ||
Check warning on line 47 in ILSpy/ExportProviderAdapter.cs GitHub Actions / Build (Debug)
Check warning on line 47 in ILSpy/ExportProviderAdapter.cs GitHub Actions / Build (Release)
|
||
#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 | ||
Check warning on line 55 in ILSpy/ExportProviderAdapter.cs GitHub Actions / Build (Debug)
Check warning on line 55 in ILSpy/ExportProviderAdapter.cs GitHub Actions / Build (Release)
|
||
{ | ||
return _exportProvider.GetExportedValues<T>(contractName); | ||
} | ||
|
||
IEnumerable<object> IExportProvider.GetExportedValues(Type contractType, string? contractName) | ||
Check warning on line 60 in ILSpy/ExportProviderAdapter.cs GitHub Actions / Build (Debug)
|
||
{ | ||
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)); | ||
} | ||
} |