简体   繁体   中英

Overriding ToString() on MEF MetadataAttribute

I have a MEF MetadataAttribute:

[MetadataAttribute]
[AttributeUsage]
public class MyTestAttribute : ExportAttribute, IMyTest
{
    public MyTestAttribute(string pluginInfo)
    {
        _pluginInfo = pluginInfo;
    }

    string _pluginInfo;
    public string PluginInfo{get{return _pluginInfo;}}

    public override string ToString()
    {
         return PluginInfo;
    }
}

public interface IMyTest
{
    string PluginInfo{get;}
}

I have some other metadata attributes which inherits from other interfaces. All my attributes overrides ToString() . I am using the method below to get plugins:

            public void GetPlugins<TSender, TEventArgs, TAttributeMetadata>()
            {
                var importedPlugins = _container.GetExports<Action<TSender, TEventArgs>, TAttributeMetadata>(contract);
                foreach(var plugin in importedPlugins)
                {
                      string pluginInfo = plugin.Metadata.ToString();//this here is not
                      //returning plugin info. It is returning some kind of Guid and the interface name
                      //I could retrieve the pluginInfo via reflection but I don't want.
                }
            }

My question is why ToString() is not returning the pluginInfo? What should I do to get pluginInfo? is it possible without reflection or dynamics?

Any help appreciated!

This happens because the exported metadata is a view and not an instance of your metadata class. It is a class, implementing your metadata interface ( IMyTest ), that is automatically generated by MEF. So, it doesn't have the ToString implementation of MyTestAttribute .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM