0

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!

4

1 回答 1

1

这是因为导出的元数据是一个视图,而不是元数据类的实例。它是一个实现元数据接口 ( IMyTest) 的类,由 MEF 自动生成。所以,它没有ToString.MyTestAttribute

于 2013-05-02T12:12:16.873 回答