本文整理汇总了C#中System.ComponentModel.Composition.Hosting.CompositionContainer.GetExports方法的典型用法代码示例。如果您正苦于以下问题:C# CompositionContainer.GetExports方法的具体用法?C# CompositionContainer.GetExports怎么用?C# CompositionContainer.GetExports使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.ComponentModel.Composition.Hosting.CompositionContainer
的用法示例。
在下文中一共展示了CompositionContainer.GetExports方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MainMenuViewModel
public MainMenuViewModel(CompositionContainer container)
{
_topLevelMenuItems = new ObservableCollection<MenuItemViewModel>();
_styleSelector = new MainMenuItemStyleSelector();
var menuItemImports = container.GetExports<IMenuItem, IMenuItemMetaData>();
var globalCommandImports = container.GetExports<IGlobalCommand>();
var menuBuilder = new MenuBuilder(globalCommandImports);
foreach (var import in menuItemImports.Where(x => x.Metadata.IsMainMenuItem))
{
menuBuilder.AddItem(import.Value, import.Metadata);
}
foreach (var rootItem in menuBuilder.Build().Children)
{
MenuItemViewModel viewModel;
if (rootItem is MenuSeparatorModel)
{
viewModel = new MenuSeparatorViewModel();
}
else
{
viewModel = new MenuItemViewModel(null, rootItem.HeaderText, rootItem.Command, rootItem.GestureText, rootItem.Children);
}
_topLevelMenuItems.Add(viewModel);
}
}
示例2: Main
/// <summary>
/// Mains the specified args.
/// </summary>
/// <param name="args">
/// The args.
/// </param>
public static void Main(string[] args)
{
WriteSignature();
using (AggregateCatalog aggregateCatalog = new AggregateCatalog())
{
RegistrationBuilder registrationBuilder = new RegistrationBuilder();
registrationBuilder.ForTypesDerivedFrom<ICommand>()
.Export(conf => conf.AsContractName(AttributedModelServices.GetContractName(typeof(ICommand))))
.SetCreationPolicy(CreationPolicy.NonShared);
aggregateCatalog.Catalogs.Add(new ApplicationCatalog(registrationBuilder));
string appPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string pluginPath = Path.Combine(appPath, "plugins");
if (Directory.Exists(pluginPath))
aggregateCatalog.Catalogs.Add(new DirectoryCatalog(pluginPath, registrationBuilder));
using (CompositionContainer container = new CompositionContainer(aggregateCatalog))
{
ICommandProvider[] providers = container.GetExports<ICommandProvider>().Select(l => l.Value).ToArray();
Type[] commands = providers.SelectMany(p => p.GetCommands()).ToArray();
Func<Type, object> mefActivator =
t =>
{
if (!typeof(ICommand).IsAssignableFrom(t))
return DefaultActivator.Instance.CreateInstance(t);
ImportDefinition importDefinition = new ImportDefinition(ed => (string)ed.Metadata[CompositionConstants.ExportTypeIdentityMetadataName] == AttributedModelServices.GetTypeIdentity(t),
AttributedModelServices.GetContractName(typeof(ICommand)),
ImportCardinality.ExactlyOne,
false,
true);
return container.GetExports(importDefinition).First().Value;
};
ArgumentParserSettings parserSettings = new ArgumentParserSettings
{
TypeActivator = new DelegateActivator(mefActivator)
};
ArgumentParser<ICommand> argumentParser = new ArgumentParser<ICommand>(parserSettings, commands);
ICommand command;
if (argumentParser.TryParse(args, out command))
{
command.Invoke(container);
}
}
}
}
示例3: AixmConverter
public AixmConverter()
{
List<Assembly> source = AppDomain.CurrentDomain.GetAssemblies().ToList<Assembly>();
foreach (var asm in source)
{
AssemblyCatalog catalog = new AssemblyCatalog(asm);
CompositionContainer mefContainer = new CompositionContainer(catalog, true);
var plugins = mefContainer.GetExports<Func<IAixmConverter, JObject, XElement, IEnumerable<JObject>>, IAixmConverterMetadata>().ToArray();
_elementReaders.AddRange(plugins);
_elementWriters.AddRange(mefContainer.GetExports<Action<IAixmConverter,int, JObject, XmlWriter>, IAixmConverterMetadata>().ToArray());
}
_elementWriters = _elementWriters.OrderBy(k => k.Metadata.WriteOrder).ToList();
}
示例4: Main
/// <summary>
/// Mains the specified args.
/// </summary>
/// <param name="args">
/// The args.
/// </param>
public static void Main(string[] args)
{
WriteSignature();
using (AggregateCatalog aggregateCatalog = new AggregateCatalog())
{
aggregateCatalog.Catalogs.Add(new ApplicationCatalog());
string appPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string pluginPath = Path.Combine(appPath, "plugins");
if (Directory.Exists(pluginPath))
aggregateCatalog.Catalogs.Add(new DirectoryCatalog(pluginPath));
using (CompositionContainer container = new CompositionContainer(aggregateCatalog))
{
ICommandProvider[] providers = container.GetExports<ICommandProvider>().Select(l => l.Value).ToArray();
var commands = providers.SelectMany(p => p.GetCommands()).ToArray();
ArgumentParser<ICommand> argumentParser = new ArgumentParser<ICommand>(commands);
ICommand command;
if (argumentParser.TryParse(args, out command))
{
command.Invoke(container);
}
}
}
}
示例5: LoadContainer
public static void LoadContainer(IUnityContainer container, string path, string pattern)
{
var directoryCatalog = new DirectoryCatalog(path, pattern);
var importDefinition = BuildImportDefinition();
try
{
using (var aggregateCatalog = new AggregateCatalog())
{
aggregateCatalog.Catalogs.Add(directoryCatalog);
using (var componsitionContainer = new CompositionContainer(aggregateCatalog))
{
IEnumerable<Export> exports = componsitionContainer.GetExports(importDefinition);
IEnumerable<IModule> modules = exports.Select(export => export.Value as IModule).Where(m => m != null);
foreach (IModule module in modules)
{
module.Initialize(container);
}
}
}
}
catch (ReflectionTypeLoadException typeLoadException)
{
var builder = new StringBuilder();
foreach (Exception loaderException in typeLoadException.LoaderExceptions)
{
builder.AppendFormat("{0}\n", loaderException.Message);
}
throw new TypeLoadException(builder.ToString(), typeLoadException);
}
}
示例6: GetValuesByType
public void GetValuesByType()
{
var cat = CatalogFactory.CreateDefaultAttributed();
var container = new CompositionContainer(cat);
string itestName = AttributedModelServices.GetContractName(typeof(ITest));
var e1 = container.GetExportedValues<ITest>();
var e2 = container.GetExports<ITest, object>(itestName);
Assert.IsInstanceOfType(e1.First(), typeof(T1), "First should be T1");
Assert.IsInstanceOfType(e1.Skip(1).First(), typeof(T2), "Second should be T2");
Assert.IsInstanceOfType(e2.First().Value, typeof(T1), "First should be T1");
Assert.IsInstanceOfType(e2.Skip(1).First().Value, typeof(T2), "Second should be T2");
CompositionContainer childContainer = new CompositionContainer(container);
CompositionBatch batch = new CompositionBatch();
batch.AddPart(new T1());
container.Compose(batch);
var t1 = childContainer.GetExportedValue<ITest>();
var t2 = childContainer.GetExport<ITest, object>(itestName);
Assert.IsInstanceOfType(t1, typeof(T1), "First (resolved) should be T1");
Assert.IsInstanceOfType(t2.Value, typeof(T1), "First (resolved) should be T1");
}
示例7: LoadCodecs
public static void LoadCodecs(string path = null, string search = null) {
if (path == null)
path = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().EscapedCodeBase).LocalPath);
if (search == null)
search = "Dicom.Native*.dll";
var log = LogManager.Default.GetLogger("Dicom.Imaging.Codec");
log.Debug("Searching {path}\\{wildcard} for Dicom codecs", path, search);
var foundAnyCodecs = false;
DirectoryCatalog catalog;
try {
catalog = new DirectoryCatalog(path, search);
}
catch (Exception ex) {
log.Error("Error encountered creating new DirectCatalog({path}, {search}) - {@exception}", path, search, ex);
throw;
}
var container = new CompositionContainer(catalog);
foreach (var lazy in container.GetExports<IDicomCodec>()) {
foundAnyCodecs = true;
var codec = lazy.Value;
log.Debug("Codec: {codecName}", codec.TransferSyntax.UID.Name);
_codecs[codec.TransferSyntax] = codec;
}
if (!foundAnyCodecs) {
log.Warn("No Dicom codecs were found after searching {path}\\{wildcard}", path, search);
}
}
示例8: TreatyHelper
public TreatyHelper()
{
var catalog = new AssemblyCatalog(this.GetType().Assembly);
var container = new CompositionContainer(catalog);
this.treaties = container.GetExports<ITreatyProvider>().ToList();
}
示例9: WhenExportIsFiltered_ThenPartIsAvailableButNotExport
public void WhenExportIsFiltered_ThenPartIsAvailableButNotExport()
{
var catalog = new TypeCatalog(typeof(Foo));
var filtered = new FilteringReflectionCatalog(catalog)
{
ExportFilter = export => !(export.ExportingMember.MemberType == System.Reflection.MemberTypes.Property),
};
var container = new CompositionContainer(filtered);
var exports = container.GetExports<IFoo>();
var barExports = container.GetExports<IBar>();
Assert.True(exports.Any());
Assert.False(barExports.Any());
}
示例10: LoadServers
/// <summary>
/// Loads all SharpShell servers from an assembly.
/// </summary>
/// <param name="path">The path to the assembly.</param>
/// <returns>A ServerEntry for each SharpShell server in the assembly.</returns>
public static IEnumerable<ServerEntry> LoadServers(string path)
{
// Storage for the servers.
var servers = new List<ServerEntry>();
try
{
// Create an assembly catalog for the assembly and a container from it.
var catalog = new AssemblyCatalog(Path.GetFullPath(path));
var container = new CompositionContainer(catalog);
// Get all exports of type ISharpShellServer.
var serverTypes = container.GetExports<ISharpShellServer>();
// Go through each servertype (creating the instance from the lazy).
foreach(var serverType in serverTypes)
{
ISharpShellServer server = null;
try
{
server = serverType.Value;
}
catch (Exception)
{
servers.Add(new ServerEntry
{
ServerName = "Invalid",
ServerPath = path,
ServerType = ServerType.None,
ClassId = new Guid(),
Server = null,
IsInvalid = true
});
continue;
}
// Yield a server entry for the server type.
servers.Add(new ServerEntry
{
ServerName = server.DisplayName,
ServerPath = path,
ServerType = server.ServerType,
ClassId = server.ServerClsid,
Server = server
});
}
}
catch (Exception)
{
// It's almost certainly not a COM server.
MessageBox.Show("The file '" + Path.GetFileName(path) + "' is not a SharpShell Server.", "Warning");
}
// Return the servers.
return servers;
}
示例11: FilterBasedOnMetadataUsingContainsMetadataTest
public void FilterBasedOnMetadataUsingContainsMetadataTest()
{
var catalog = new AssemblyCatalog(typeof(FilteringCatalogTests).Assembly);
var filteredCatalog = new FilteringCatalog(catalog, new ContainsMetadata("key", "value"));
var container = new CompositionContainer(filteredCatalog);
var components = container.GetExports<IMetadataComponent>();
Assert.That(components, Is.Not.Null);
Assert.That(components.Count(), Is.EqualTo(1));
}
示例12: Register
/// <summary>
/// The register.
/// </summary>
/// <param name="container">
/// The container.
/// </param>
public static void Register(IWindsorContainer container)
{
var catalog =
new CatalogBuilder().ForAssembly(typeof(IComponentRegistrarMarker).Assembly).ForMvcAssembly(
Assembly.GetExecutingAssembly()).ForMvcAssembliesInDirectory(HttpRuntime.BinDirectory, "Leatn*.dll")
.Build();
var compositionContainer = new CompositionContainer(catalog);
compositionContainer.GetExports<IComponentRegistrar>().ForeEach(e => e.Value.Register(container));
}
示例13: Main
public static int Main(string[] args)
{
if (args.Length != 1)
{
Console.WriteLine("File Watch Directory must be specifed");
Console.WriteLine("usage: engine.exe path");
return -1;
}
try
{
// Example of convention-based approach
var registration = new RegistrationBuilder();
registration.ForType<Logger>().Export<ILogger>();
var assemblyCatalog = new AssemblyCatalog(typeof(Logger).Assembly, registration);
var catalog = new AggregateCatalog(
assemblyCatalog,
new DirectoryCatalog(@".\addins"));
using (var container = new CompositionContainer(catalog))
{
var engine = new ProcessorEngine(new DirectoryInfo(args[0]));
// Bind exports to imports
container.ComposeParts(engine);
var exports = container.GetExports<IFileProcessor, IFileProcessorMetadata>().ToList();
Console.WriteLine("{0} File Processor(s) available", exports.Count);
if (exports.Count > 0)
{
foreach (var export in exports)
{
Console.WriteLine("{0} file type supported", export.Metadata.SupportedExtension);
}
engine.ProcessFiles();
}
else
{
Console.WriteLine("Add File Processors to the Add-in directory");
}
}
}
catch (Exception e)
{
Console.WriteLine(e);
Console.ReadLine();
return -1;
}
Console.ReadLine();
return 0;
}
示例14: Parts_are_filtered_based_on_shared_lifetime_using_HasCreationPolicy_filter
public void Parts_are_filtered_based_on_shared_lifetime_using_HasCreationPolicy_filter()
{
var catalog = new AssemblyCatalog(typeof(FilteringCatalogTests).Assembly);
var filteredCatalog = new FilteringCatalog(catalog, new HasCreationPolicy(CreationPolicy.Shared));
var container = new CompositionContainer(filteredCatalog);
var components = container.GetExports<ILifetimeComponent>();
Assert.That(components, Is.Not.Null);
Assert.That(components.Count(), Is.EqualTo(1));
Assert.That(components.First().Value.GetType(), Is.EqualTo(typeof(LifetimeComponent2)));
}
示例15: FilterBasedOnNonSharedLifetimeUsingHasCreationPolicyTest
public void FilterBasedOnNonSharedLifetimeUsingHasCreationPolicyTest()
{
var catalog = new AssemblyCatalog(typeof(FilteringCatalogTests).Assembly);
var filteredCatalog = new FilteringCatalog(catalog, new HasCreationPolicy(CreationPolicy.NonShared));
var container = new CompositionContainer(filteredCatalog);
var components = container.GetExports<ILifetimeComponent>();
Assert.That(components, Is.Not.Null);
Assert.That(components.Count(), Is.EqualTo(1));
Assert.That(components.First().Value.GetType(), Is.EqualTo(typeof(LifetimeComponent3)));
}