这可能是一个非常简单的问题,但是由于Google很难给我答案,所以您可以选择!
我想知道MEF中的一部分是否有可能掌握其自己的ExportMetadata中定义的值?
可以说我得到了这段代码:
[ExportMetadata("name", "A Template Plugin")]
[ExportMetadata("guid", "0db79a169xy741229a1b558a07867d60")]
[ExportMetadata("description", "A template for a new plugin")]
[ExportMetadata("version", "1.0.0.43")]
[Export(typeof(IPlugin)), PartCreationPolicy(CreationPolicy.NonShared)]
public class PluginExport : IPlugin, IDisposable
{
... code goes here...
... can I get hold of metadata, ie the "guid" key ??? ...
}
如果有人质疑它的合理性,那是因为我为3pp开发人员制作了一个插件模板,并且还需要在插件内部使用一些值(在上面的示例中未显示),我认为不用他们设置太多会很好数据放在两个不同的位置。
您可以使用反射而不考虑MEF来获取属性值:
[ExportMetadata("guid", "0db79a169xy741229a1b558a07867d60")]
class PluginExport
{
void PrintGuid()
{
var guid = this.GetType()
.GetCustomAttributes(false)
.OfType<ExportMetadataAttribute>()
.Single(attribute => attribute.Name == "guid").Value;
Console.WriteLine(guid); // Prints your value.
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句