【问题标题】:Using Reflection Causing MissingMethodException in Xamarin UITest在 Xamarin UITest 中使用反射导致 MissingMethodException
【发布时间】:2015-07-22 21:08:05
【问题描述】:

我的 Xamarin PCL 中有一个类,它调用 System.Reflection.GetRuntimeProperties。例如,假设我的 PCL 类有这个方法:

public string ExampleMethod(string arg) {
    if(arg == null) return null;
    IEnumerable<PropertyInfo> infos = this.GetType().GetRuntimeProperties();
    return infos[0].Name;
}

然后我有一个 Xamarin.UITest 项目,它引用 PCL 项目并测试这个类。到目前为止,我的 TestFixture 中有两个测试用例,在我们的示例中如下所示:

    [Test]
    public void TestExampleMethod_ArgNull_Null(){
        Assert.That (exampleInstance.ExampleMethod(null), Is.Null);
    } 

    [Test]
    public void TestExampleMethod_ArgNotNull_NotNull(){
        Assert.That (exampleInstance.ExampleMethod("testValue"), Is.NotNull);
    } 

当我运行 Xamarin.UITest 项目时,它会编译、运行测试并在 Android 和 iOS 平台上正常完成。 TestExampleMethod_ArgNull_Null 测试通过,因为它提前返回。但是,TestExampleMethod_ArgNotNull_NotNull 测试失败:

System.MissingMethodException:找不到方法“RuntimeReflectionExtensions.GetRuntimeProperties”。

所以看起来即使一切都编译得很好,并且我能够很好地运行其他测试用例,但 Xamarin.UITest 项目无法使用 System.Reflection 中的任何内容。有谁知道我如何调试这个?

【问题讨论】:

    标签: unit-testing xamarin nunit missingmethodexception xamarin-test-cloud


    【解决方案1】:

    就我而言,使用以下方法构建失败:

    IEnumerable<PropertyInfo> infos = this.GetType().GetRuntimeProperties();
    return infos[0].Name;

    由于无法对 IEnumerable 进行括号索引。我改成这样:

    List<PropertyInfo> infos = this.GetType().GetRuntimeProperties().ToList();
    return infos[0].Name;

    项目构建成功,测试运行成功。

    具有使用反射的方法的类位于从 UI 测试项目引用的 PCL 中。

    所以基本上我无法重现您遇到的错误。

    【讨论】:

    【解决方案2】:

    我也将此发布到 Xamarin 支持(感谢 @jgoldberger),我们能够确定这是由于项目设置问题造成的。这是一个使用 Couchbase Lite 的项目,它需要特定版本的 Json.Net(截至本文为 6.0.4)。我一定是不小心更新了某些项目的包,因为所有项目(PCL、Android、iOS 和 UITest)都没有使用相同版本的 Json.Net。我最终从头开始重新创建该项目,并且处理了它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-31
      • 1970-01-01
      • 2017-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多