/// <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); } } } }
static ArgumentParserSettings() { Default = new ArgumentParserSettings(); }