从 MEF 部分引用 ExportMetaData?
Referencing ExportMetaData from within MEF-part?
这可能是一个超级简单的问题,但由于 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.
}
}
这可能是一个超级简单的问题,但由于 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.
}
}