-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Inject existing object into MEF2 (#66364)
- Loading branch information
1 parent
e34e8dd
commit 6fdd29a
Showing
5 changed files
with
155 additions
and
0 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
26 changes: 26 additions & 0 deletions
26
...Composition.TypedParts/src/System/Composition/Hosting/InstanceExportDescriptorProvider.cs
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,26 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Generic; | ||
using System.Composition.Hosting.Core; | ||
|
||
namespace System.Composition.Hosting | ||
{ | ||
internal class InstanceExportDescriptorProvider : SinglePartExportDescriptorProvider | ||
{ | ||
private readonly object _exportedInstance; | ||
|
||
public InstanceExportDescriptorProvider(object exportedInstance, Type contractType, string contractName, IDictionary<string, object> metadata) | ||
: base(contractType, contractName, metadata) | ||
{ | ||
_exportedInstance = exportedInstance; | ||
} | ||
|
||
public override IEnumerable<ExportDescriptorPromise> GetExportDescriptors(CompositionContract contract, DependencyAccessor descriptorAccessor) | ||
{ | ||
if (IsSupportedContract(contract)) | ||
yield return new ExportDescriptorPromise(contract, _exportedInstance.ToString(), true, NoDependencies, _ => | ||
ExportDescriptor.Create((c, o) => _exportedInstance, Metadata)); | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...mposition.TypedParts/src/System/Composition/Hosting/SinglePartExportDescriptorProvider.cs
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,43 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Generic; | ||
using System.Composition.Hosting.Core; | ||
using System.Linq; | ||
|
||
namespace System.Composition.Hosting | ||
{ | ||
internal abstract class SinglePartExportDescriptorProvider : ExportDescriptorProvider | ||
{ | ||
private readonly Type _contractType; | ||
private readonly string _contractName; | ||
|
||
protected SinglePartExportDescriptorProvider(Type contractType, string contractName, IDictionary<string, object> metadata) | ||
{ | ||
_contractType = contractType; | ||
_contractName = contractName; | ||
Metadata = metadata ?? new Dictionary<string, object>(); | ||
} | ||
|
||
protected bool IsSupportedContract(CompositionContract contract) | ||
{ | ||
if (contract.ContractType != _contractType || | ||
contract.ContractName != _contractName) | ||
return false; | ||
|
||
if (contract.MetadataConstraints != null) | ||
{ | ||
var subsetOfConstraints = contract.MetadataConstraints.Where(c => Metadata.ContainsKey(c.Key)).ToDictionary(c => c.Key, c => Metadata[c.Key]); | ||
var constrainedSubset = new CompositionContract(contract.ContractType, contract.ContractName, | ||
subsetOfConstraints.Count == 0 ? null : subsetOfConstraints); | ||
|
||
if (!contract.Equals(constrainedSubset)) | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
protected IDictionary<string, object> Metadata { get; } | ||
} | ||
} |
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