MEF [导入] C# 示例

发布于 2024-11-16 11:00:52 字数 406 浏览 1 评论 0原文

我在扩展某些现有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉酒的小男人 2024-11-23 11:00:52

我很确定这会起作用:

public class SampleClass
{
    [Import]
    private Manager manager; //Setting it to null is redundant.
}


[Export]
public class Manager
{
}

您还需要设置容器。有些框架可以更轻松地连接所有内容,但如果没有的话。这并不困难:

在应用程序开始时,您需要填写目录:

//http://mef.codeplex.com/wikipage?title=Using%20Catalogs
var catalog = new AggregateCatalog();

//Add AssemblyCatalogs (Single) or DirectoryCatalogs (Multiple)
catalog.Catalogs.Add(new AssemblyCatalog(typeof(IManager).Assembly));
catalog.Catalogs.Add(new DirectoryCatalog(@"myimports\"));

//Don't do this (including the same assembly twice)
//catalog.Catalogs.Add(new AssemblyCatalog(typeof(Manager).Assembly));

var container = new CompositionContainer(catalog);
container.composeParts(this);

但是,您应该真正利用接口进行导入/导出。

public class SampleClass
{
    [Import]
    private IManager Manager;
}

[InheritedExport]
public interface IManager { }

public class Manager : IManager { }

现在,就说明名称和类型而言,这不是必需的,但如果您计划拥有需要区分的类似导出,那么这是很好的选择。

public class SomeClass
{
    //This is for the specific AdvancedManager
    [Import("AdvancedManager", typeof(IManager))]
    private IManager AdvancedManager;

    //This is for the specific SimpleManager
    [Import("SimpleManager", typeof(IManager))]
    private IManager SimpleManager;

    //If you don't mark it as ImportMany this will fail because now there
    //are 2 non-specific IManager exports.
    [ImportMany]
    private ICollection<IManager> AllManagers;
}

[InheritedExport]
public interface IManager { }

//This will export out as an IManager and a specifically named IManager
[Export("AdvancedManager", typeof(IManager))]
public class AdvancedManager : IManager { }

//This will export out as an IManager and a specifically named IManager
[Export("SimpleManager", typeof(IManager))]
public class SimpleManager : IManager { }

I'm pretty sure this will work:

public class SampleClass
{
    [Import]
    private Manager manager; //Setting it to null is redundant.
}


[Export]
public class Manager
{
}

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:

//http://mef.codeplex.com/wikipage?title=Using%20Catalogs
var catalog = new AggregateCatalog();

//Add AssemblyCatalogs (Single) or DirectoryCatalogs (Multiple)
catalog.Catalogs.Add(new AssemblyCatalog(typeof(IManager).Assembly));
catalog.Catalogs.Add(new DirectoryCatalog(@"myimports\"));

//Don't do this (including the same assembly twice)
//catalog.Catalogs.Add(new AssemblyCatalog(typeof(Manager).Assembly));

var container = new CompositionContainer(catalog);
container.composeParts(this);

But, you should really take advantage of interfaces for importing/exporting.

public class SampleClass
{
    [Import]
    private IManager Manager;
}

[InheritedExport]
public interface IManager { }

public class Manager : IManager { }

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.

public class SomeClass
{
    //This is for the specific AdvancedManager
    [Import("AdvancedManager", typeof(IManager))]
    private IManager AdvancedManager;

    //This is for the specific SimpleManager
    [Import("SimpleManager", typeof(IManager))]
    private IManager SimpleManager;

    //If you don't mark it as ImportMany this will fail because now there
    //are 2 non-specific IManager exports.
    [ImportMany]
    private ICollection<IManager> AllManagers;
}

[InheritedExport]
public interface IManager { }

//This will export out as an IManager and a specifically named IManager
[Export("AdvancedManager", typeof(IManager))]
public class AdvancedManager : IManager { }

//This will export out as an IManager and a specifically named IManager
[Export("SimpleManager", typeof(IManager))]
public class SimpleManager : IManager { }
只是一片海 2024-11-23 11:00:52

经理必须是财产。尝试
<代码>[导入]
私人经理经理{获取;放; }

manager must be a property. try
[Import]
private Manager manager { get; set; }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文