首页 > 解决方案 > 使用动态方法参数实现接口时的 xUnit System.TypeLoadException

问题描述

我有一个引用的 nuget 包,其中包含一个接口:

public interface IMyInterface {
    bool Initialize(Dictionary<string, dynamic> initArguments);
}

在我的解决方案中,我有这个实现:

public class MyImplementation : IMyInterface 
{

    public bool Initialize(Dictionary<string, dynamic> initArguments) 
    {
        return true;
    }
}

如果我在控制台项目中使用它,它可以正常工作:

var myImplementation = new MyImplementation();

但是我如果尝试在单元测试项目中使用它会抛出这个:

System.TypeLoadException:来自程序集 'xxx,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null' 的类型 'xxx.MyImplementation' 中的方法 'Initialize' 没有实现。

这是我的单元测试:

    [Fact]
    public void Test1()
    {
        var myImplementation = new MyImplementation();

        Assert.NotNull(myImplementation);
    }

我认为这可能是因为Dictionary<string, dynamic>参数,与此相关dynamic但不确定。我有很多谷歌它没有运气。

更新

如果我检查从 nuget 包引用的接口,它是这样的:

public interface IMyInterface {
    bool Initialize([Dynamic(new[] { false, false, true })] Dictionary<string, dynamic> initArguments);
}

我不知道为什么nuget包的编译版本是这样的,Dynamicparam中有data属性。

标签: c#.net-coredynamicxunittypeloadexception

解决方案


推荐阅读