gpt4 book ai didi

c# - MEF 属性总是返回 Null

转载 作者:行者123 更新时间:2023-11-30 16:30:13 25 4
gpt4 key购买 nike

原始源代码

我的 BusinessObjects.dll 文件中有一个简单的业务对象:

namespace BusinessObjects
{
public class MyClass
{
public MyClass()
{
DateTime = DateTime.Now;
}

public DateTime DateTime { get; set; }
}
}

在我的 SharedUI.dll 中,我有这个“上下文提供者”类,我用它来保存对当前选定的 MyClass 的引用——记住这是一个简单的例子:)...

namespace SharedUI
{
public class AppContext
{
[Export]
public MyClass SelectedMyClass { get; private set; }

public void SetupContext(MyClass myClass)
{
SelectedMyClass = myClass;
}

public static AppContext Context
{
get
{
if (context == null)
{
context = new AppContext();
}
return context;
}
}

private static AppContext context;
}
}

我的 MefTest.exe 有这个类:

namespace MefTest
{
public class Program
{
[Import]
public MyClass MyClass { get; set; }

private void Compose()
{
var ventSystem = new MyClass();
AppContext.Context.SetupContext(ventSystem);

var executingAssembly = new AssemblyCatalog(Assembly.GetExecutingAssembly());
var contextAssembly = new AssemblyCatalog(Assembly.LoadFile(string.Format(@"{0}\SharedUI.dll", Environment.CurrentDirectory)));
var catalog = new AggregateCatalog(executingAssembly, contextAssembly);

var container = new CompositionContainer(catalog);

container.ComposeParts(this);
}

private void Run()
{
Compose();

// MyClass is always null in the next line?
Console.WriteLine(MyClass.DateTime.ToString());

Console.ReadKey();
}

private static void Main(string[] args)
{
var p = new Program();
p.Run();
}
}
}

我是 MEF 菜鸟所以请多多包涵 :)

根据 Daniel Plaisted 的建议更新了源代码

MyClass源码是一样的...

SharedUI.dll 现在看起来像这样:

namespace SharedUI
{
[Export]
public class AppContext
{
[Export(typeof(MyClass))]
public MyClass SelectedMyClass { get; private set; }

public void SetupContext(MyClass myClass)
{
SelectedMyClass = myClass;
}
}
}

MefTest.exe 现在看起来像这样:

namespace MefTest
{
public class Program
{
[Import]
public MyClass MyClass { get; set; }

[Import]
public AppContext AppContext { get; set; }

private void Compose()
{
var executingAssembly = new AssemblyCatalog(Assembly.GetExecutingAssembly());
var contextAssembly = new AssemblyCatalog(Assembly.LoadFile(string.Format(@"{0}\SharedUI.dll", Environment.CurrentDirectory)));
var catalog = new AggregateCatalog(executingAssembly, contextAssembly);

var container = new CompositionContainer(catalog);

container.ComposeParts(this);

var myClass = new MyClass();
AppContext.SetupContext(myClass);
}

private void Run()
{
Compose();

// AppContext.SelectedMyClass is NOT null in the next line... which is good I guess :)
Console.WriteLine(AppContext.SelectedMyClass.DateTime.ToString());

// MyClass is always null in the next line?
Console.WriteLine(MyClass.DateTime.ToString());

Console.ReadKey();
}

private static void Main(string[] args)
{
var p = new Program();
p.Run();
}
}
}

我做错了什么,因为我无法让它工作?

最佳答案

当 MEF 需要获取类属性上的 Export 时,它将创建该类的实例并调用属性 getter。所以 MEF 正在创建一个新的 AppContext 实例,不同于静态的 AppContext.Context 实例。 MEF 创建的实例没有在其上设置 SelectedMyClass 属性,这就是您的导入最终为空的原因。

关于c# - MEF 属性总是返回 Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5883015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com