-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
8 changed files
with
180 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System.Composition; | ||
|
||
namespace VSMefSample | ||
{ | ||
[Export(typeof(Fruit))] | ||
[ExportMetadata("Type", "Apple")] | ||
public class Apple : Fruit | ||
{ | ||
public bool IsJuicy { get; set; } | ||
|
||
public override string ToString() => $"Apple (IsJuicy: {this.IsJuicy})"; | ||
} | ||
} |
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,12 @@ | ||
using System.Composition; | ||
|
||
namespace VSMefSample | ||
{ | ||
public class Fruit | ||
{ | ||
[Import] | ||
public Tree GrewFrom { get; set; } | ||
|
||
public int SeedCount { get; set; } | ||
} | ||
} |
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,16 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace VSMefSample | ||
{ | ||
public class FruitMetadata | ||
{ | ||
public FruitMetadata(IDictionary<string, object> metadata) | ||
{ | ||
this.Type = (string)metadata["Type"]; | ||
} | ||
|
||
public string Type { get; set; } | ||
} | ||
} |
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,13 @@ | ||
using System.Composition; | ||
|
||
namespace VSMefSample | ||
{ | ||
[Export(typeof(Fruit))] | ||
[ExportMetadata("Type", "Pear")] | ||
public class Pear : Fruit | ||
{ | ||
public bool IsDelicious { get; set; } | ||
|
||
public override string ToString() => $"Pear (IsDelicious: {this.IsDelicious})"; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,12 +1,49 @@ | ||
using System; | ||
using System.Reflection; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.VisualStudio.Composition; | ||
|
||
namespace VSMefSample | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
Console.WriteLine("Hello World!"); | ||
var exportProvider = CreateExportProviderAsync().GetAwaiter().GetResult(); | ||
Tree tree = exportProvider.GetExportedValue<Tree>(); | ||
var apple1 = (Apple)tree.CreateFruit("Apple"); | ||
apple1.IsJuicy = true; | ||
var apple2 = (Apple)tree.CreateFruit("Apple"); | ||
var pear = (Pear)tree.CreateFruit("Pear"); | ||
pear.IsDelicious = true; | ||
|
||
Console.WriteLine(tree); | ||
} | ||
|
||
static async Task<ExportProvider> CreateExportProviderAsync(CancellationToken cancellationToken = default(CancellationToken)) | ||
{ | ||
var discovery = new AttributedPartDiscovery(Resolver.DefaultInstance); | ||
var parts = await discovery.CreatePartsAsync(Assembly.GetExecutingAssembly(), cancellationToken); | ||
var catalog = ComposableCatalog.Create(Resolver.DefaultInstance) | ||
.AddParts(parts); | ||
var composition = CompositionConfiguration.Create(catalog); | ||
composition.CreateDgml().Save("mefgraph.dgml"); | ||
if (!composition.CompositionErrors.IsEmpty) | ||
{ | ||
foreach (var error in composition.CompositionErrors.Peek()) | ||
{ | ||
Console.Error.WriteLine(error.Message); | ||
foreach (var part in error.Parts) | ||
{ | ||
Console.Error.WriteLine(" " + part.Definition.Type.FullName); | ||
} | ||
} | ||
} | ||
|
||
composition.ThrowOnErrors(); | ||
var exportProviderFactory = composition.CreateExportProviderFactory(); | ||
return exportProviderFactory.CreateExportProvider(); | ||
} | ||
} | ||
} |
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,52 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Composition; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace VSMefSample | ||
{ | ||
[Export] | ||
public class Tree : IDisposable | ||
{ | ||
private readonly List<Export<Fruit>> createdFruit = new List<Export<Fruit>>(); | ||
|
||
[ImportMany] | ||
public List<ExportFactory<Fruit, FruitMetadata>> FruitFactories { get; set; } | ||
|
||
public Fruit CreateFruit(string type) | ||
{ | ||
ExportFactory<Fruit> fruitFactory = this.FruitFactories.Single(ff => ff.Metadata.Type == type); | ||
Export<Fruit> export = fruitFactory.CreateExport(); | ||
this.createdFruit.Add(export); | ||
return export.Value; | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
var sb = new StringBuilder(); | ||
sb.AppendLine("Tree"); | ||
foreach (var fruit in this.createdFruit) | ||
{ | ||
sb.AppendLine($" {fruit.Value}"); | ||
} | ||
|
||
return sb.ToString(); | ||
} | ||
|
||
public void Dispose() => this.Dispose(true); | ||
|
||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (disposing) | ||
{ | ||
foreach (Export<Fruit> export in this.createdFruit) | ||
{ | ||
export.Dispose(); | ||
} | ||
|
||
this.createdFruit.Clear(); | ||
} | ||
} | ||
} | ||
} |
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,30 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<DirectedGraph Layout="Sugiyama" GraphDirection="RightToLeft" xmlns="http://schemas.microsoft.com/vs/2009/dgml"> | ||
<Nodes> | ||
<Node Id="VSMefSample_Pear" Label="VSMefSample.Pear" /> | ||
<Node Id="VSMefSample_Tree" Label="VSMefSample.Tree" /> | ||
<Node Id="Microsoft_VisualStudio_Composition_MetadataViewClassProvider" Label="Microsoft.VisualStudio.Composition.MetadataViewClassProvider" Category="VsMEFBuiltIn" /> | ||
<Node Id="VSMefSample_Apple" Label="VSMefSample.Apple" /> | ||
<Node Id="Microsoft_VisualStudio_Composition_PassthroughMetadataViewProvider" Label="Microsoft.VisualStudio.Composition.PassthroughMetadataViewProvider" Category="VsMEFBuiltIn" /> | ||
<Node Id="Microsoft_VisualStudio_Composition_ExportProvider_ExportProviderAsExport" Label="Microsoft.VisualStudio.Composition.ExportProvider.ExportProviderAsExport" Category="VsMEFBuiltIn" /> | ||
</Nodes> | ||
<Links> | ||
<Link Source="VSMefSample_Tree" Target="VSMefSample_Pear" /> | ||
<Link Source="VSMefSample_Apple" Target="VSMefSample_Tree" Category="ExportFactory" /> | ||
<Link Source="VSMefSample_Pear" Target="VSMefSample_Tree" Category="ExportFactory" /> | ||
<Link Source="VSMefSample_Tree" Target="VSMefSample_Apple" /> | ||
</Links> | ||
<Categories> | ||
<Category Id="Contains" IsContainment="True" /> | ||
</Categories> | ||
<Styles> | ||
<Style TargetType="Link" GroupLabel="ExportFactory"> | ||
<Condition Expression="HasCategory('ExportFactory')" /> | ||
<Setter Property="StrokeDashArray" Value="2,2" /> | ||
</Style> | ||
<Style TargetType="Node" GroupLabel="VsMEFBuiltIn"> | ||
<Condition Expression="HasCategory('VsMEFBuiltIn')" /> | ||
<Setter Property="Visibility" Value="Hidden" /> | ||
</Style> | ||
</Styles> | ||
</DirectedGraph> |