c# - 在设置先决条件导入之前无法调用 GetExportedValue

标签 c# mef

我们正在使用 美孚 WPF 应用。

我们得到了这个错误 :

GetExportedValue cannot be called before prerequisite import 'MyNamespace.MyMainClass..ctor (Parameter="myParameter", ContractName="IContractInterface")' has been set.



我是不使用线程 ,我读到这个错误有时会发生在多个线程上,但是以防万一我创建了组合容器,并在 true 中传递了“线程安全”参数
var container = new CompositionContainer(catalog, true);

代码 我有的是:

我简化的主类代码是:(想法是,如果 IMySubClass 没有导入,我将使用默认实现,这就是我在“OmImportsSatisfied”方法上创建它并满足其导入的原因。)

MyMainClass C#
[Export(typeof(IMyInterface)]
public class MyMainClass: IPartImportsSatisfiedNotification, IMyInterface
{
    [Import]
    public IContainer Container { get; private set; }

    [Import(AllowDefault = true)]
    public IMySubClass MySubClass { get; set; }

    [ImportingConstructor]
    public MyMainClass([Import(AllowDefault = true)] IContractInterface myParameter= null)
        : this()
    {
        if (myParameter!= null)
        {
            //Do Something with myParameter
        }            
    }

    public void OnImportsSatisfied()
    {
        if (MySubClass == null)
        {
            MySubClass = new MySubClass();
            Container.SatisfyImportsOnce(MySubClass);
        }

        //Some other stuff
     }

}

我的子类 C#
public class MySubClass : IPartImportsSatisfiedNotification, IMySubClass
{
    [ImportMany]
    public Lazy<IMyAttributeInterface, IMyAttributeInterfaceMetadata>[] Exports { get; set; }

    public void OnImportsSatisfied()
    {
        foreach (var export in Exports)
        {
            var value = export.Value; //Here it throws the Exception
            //Do something.
        }
    }

错误在 MySubClass 中引发,位于 行的 OnImportSatisfied 方法中:
var value = export.Value;

但是,当我调试成功调用 MyMainClass 的 ImportConstructor 时,我的参数 注入(inject)并使用。之后调用 OnImportsSatisfied 方法 我得到了错误。

我的子类中的导出列表来自具有属性“MyExportAttribute”的其他类中的属性。我没有太多创建导出属性的经验,所以这里是代码,以防问题来自它。

导出属性
public interface IMyAttributeInterfaceMetadata
{
    string Property1 { get; }
    string Property2 { get; }
}

[MetadataAttribute]
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Class, AllowMultiple=false, Inherited=false)]
public class MyExportAttribute : ExportAttribute, IMyAttributeInterfaceMetadata
{
    string Property1 { get; }
    string Property2 { get; }

    public MyExportAttribute(string property1, string property2)
        : base(typeof(IMyAttributeInterface))
    {
        Property1 = property1;
        Property2 = property2;
    }
}

最佳答案

由于在延迟导出时调用 export.Value 时会引发异常,因此您可以了解发生了什么问题。
直到那时才构建延迟导入的导出 - 它只是“发现”并获得了它的元数据。但是,一旦调用了它的值,就需要实际实例化惰性导出。
此时,它的 DLL 将被加载(因此,如果您在这里缺少任何依赖项,您可能会遇到异常,但这不是您的问题)并且导出的类将被构造以满足 MySubClass 中的导入。如果导出需要另一个导入(通过 ImportingConstructor 或单独的 Import),那么它将尝试解决/实例化它等等。
我认为您获得异常的最可能原因不是您的 MainClass 或 SubClass,而是您的 SubClass 中的导入。他们是否有任何要导入的依赖项?那些被正确解决了吗?尝试在那里进行调查。
也可以是一定要检查异常 中的 InnerExceptions你已经捕获了!它经常沿着整个导入链的兔子洞走下去,直到它到达实际失败的地步。

关于c# - 在设置先决条件导入之前无法调用 GetExportedValue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21309467/

相关文章:

c# - 如何计算两个引号之间的文本?

c# - 我可以判断另一个进程是否正在创建文件吗?

.net - 基本 MEF 工作流程/使用

asp.net-mvc - 如何将 MEF 与 ASP.NET MVC 4 和 ASP.NET Web API 集成

c# - System.Collections.Generic.List<T> 需要 '1' 类型参数

c# - 如何处理数据库中的字典表

c# - 捕获的 .NET 异常意外为空

c# - 如何在运行时使用 Ninject 的 DI 动态添加新绑定(bind)?

c# - mef 中的意外结果

c# - 如何反序列化 Json 对象 - C#