How to read MEF Metadata without composing parts?

Viewed 12

Is there a way to read the ExportMetadataAttribute through reflection, without loading the component(s) into memory?

I am creating MEF components decorated with the ExportMetadataAttribute.

[Export(typeof(AEI.IAutomatable))]
[ExportMetadata("InstrumentClassName", "BioTek Instrument")]
[ExportMetadata("versionNumber", "0.0.1")]
public class Washer : IAutomatable
{
    ....

I would like to read these attributes without going through the process of creating a composition container and composing parts.

...in other words, I do not want to go through all this to read the attributes:

     [ImportMany]
        public System.Lazy<IAutomatable, IDictionary<string, object>>[] WorkcellModuleDrivers;
public void AssembleAutomationParts()
            {
                string sModuleDirectory = Settings_Manager.ModuleDirectory;
                var directoryCatalog = new DirectoryCatalog(sModuleDirectory, "*.dll");
    
                // An aggregate catalog that combines multiple catalogs.
                var catalog = new AggregateCatalog();
    
                // Adds all the parts found in the modules folder which were just downloaded from the server
                catalog.Catalogs.Add(directoryCatalog);
    
                // Create the CompositionContainer with the parts in the catalog.
                _container = new CompositionContainer(catalog);
    
                try
                {
                    _container.ComposeParts(this);
                }
                catch (Exception oEX)
                {
                    string sMsg = "\r\n**** IMPORTANT ****\r\n";
                    sMsg += string.Format("One or more of the Module drivers (*.dll files) in {0} could not be loaded by this AEI client.\r\n", sModuleDirectory);
                    sMsg += " This is usually because the driver and AEI are incompatible versions.\r\n";
                    sMsg += "Shut down and close the AEI client, insure it updates from server on restart.\r\n";
                    sMsg += "*******************\r\n";
                    throw new Exception(sMsg, oEX);
                }
            }
            
            public IAutomatable GetModule(string sInstrumentClassName)
            {
                foreach (var Module in WorkcellModuleDrivers)
                {
                    if ((string)Module.Metadata["InstrumentClassName"] == sInstrumentClassName)
                        return Module.Value;
                }
    
                throw new Exception("Module driver of type '" + sInstrumentClassName + "' could not be found.");
    
            }
0 Answers
Related