如何获取导入的MEF函数的汇编信息?
是否可以从导入的 MEF 函数中获取程序集信息?我需要知道包含该函数的插件控件的程序集版本和名称。尝试了以下方法,但它只返回 System.ComponentModel.Composition 版本。
foreach (Lazy<Func<int>, IMetadata> func in PluginFuncs)
{
// get assembly information of the Plugin control for the imported function
string version = func.GetType().Assembly.GetName().Version.ToString();
Console.WriteLine(version);
}
另一种选择是在元数据中使用硬编码值,但我认为这不可维护。当版本更改时,很容易忘记更改这些值。
Is it possible get the assembly information from an imported MEF function? I need to know the assembly version and name of the Plugin control that contains the function. Tried the following, but it just returns the System.ComponentModel.Composition version.
foreach (Lazy<Func<int>, IMetadata> func in PluginFuncs)
{
// get assembly information of the Plugin control for the imported function
string version = func.GetType().Assembly.GetName().Version.ToString();
Console.WriteLine(version);
}
Another alternative would be to use hardcoded values in the metadata, but I thought this would not be maintainable. It would be easy to forget to change those values when the version changed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要从
func.Value
内部检查类型,而不是包装它的Lazy
。尝试:但是,请意识到这将在此时评估
Lazy
- 但这是必需的,因为在评估该对象之前,您尝试获取类型的对象尚未构造完毕。You need to check the type from within
func.Value
, not theLazy<T,TMeta>
wrapping it. Try:However, realize that this will evaluate the
Lazy<T>
at this point - but this is required, because the object where you are trying to get the type hasn't be constructed until you evaluate that.