MEF [导入] C# 示例
我在扩展某些现有 C# 代码时遇到问题。
有一个从一个类导出的管理器类的实例。 它已成功导入到其他几个类中,使用:
[Import]
private Manager manager = null;
我已将相同的代码添加到新类中。它编译得很好,但在运行时引用始终为空。
我显然错过了一些东西。
我真正希望看到的是执行导入所需的最少代码(两个类)。 除了在一个类中创建和导出一个对象(最好不是字符串或简单值)并在导入到另一个类时显示该对象为非空之外,它不需要执行任何操作。 (我一直迷失在试图展示功能而不仅仅是可用语法的其他示例的细节中。)
请注意,我需要查看使用 [Import] 而不是 [Import(type)] 的示例。
谢谢。
I am having a problem with extending some existing C# code.
There is an instance of a manager class exported from one class.
It is successfully imported into several other classes using:
[Import]
private Manager manager = null;
I have added the same code to a new class. It compiles well, but at runtime the reference is always null.
I am obviously missing something.
What I'd really like to see is the minimum code (two classes) needed to do an import.
It doesn't have to do anything except create and export an object (preferably not a string nor a simple value) in one class and show it is non-null when imported to another class. (I've been getting lost in the details of other examples trying to show functionality rather than just a usable syntax.)
Please note that I need to see an example using [Import], not [Import(type)].
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我很确定这会起作用:
您还需要设置容器。有些框架可以更轻松地连接所有内容,但如果没有的话。这并不困难:
在应用程序开始时,您需要填写目录:
但是,您应该真正利用接口进行导入/导出。
现在,就说明名称和类型而言,这不是必需的,但如果您计划拥有需要区分的类似导出,那么这是很好的选择。
I'm pretty sure this will work:
You'll also need to setup your container. There are frameworks that make it easier to wireup everything, but if not. It's not difficult:
At the start of your application you'll need to fill in your catalog:
But, you should really take advantage of interfaces for importing/exporting.
Now, as far as stating a name and type, that is not necessary, but it's good if you plan on having similar exports that you need to distinguish.
经理必须是财产。尝试
<代码>[导入]
私人经理经理{获取;放; }
manager must be a property. try
[Import]
private Manager manager { get; set; }