C# 通过传递XMl节点调用私有方法

C# 通过传递XMl节点调用私有方法,c#,xml,C#,Xml,我正在编写一个单元测试,代码如下所示 TestAbstractionLib.UnitTestUtilsClass.UnitTestFileName = TestAbstractionLib.UnitTestUtilsClass.VisualDumpAfterValueSet; object[] prop = { "Patient", false }; var methodUnderTest = Helper.GetStaticMethod(typeof(TestAbstra

我正在编写一个单元测试,代码如下所示

   TestAbstractionLib.UnitTestUtilsClass.UnitTestFileName = TestAbstractionLib.UnitTestUtilsClass.VisualDumpAfterValueSet;
    object[] prop = { "Patient", false };
    var methodUnderTest = Helper.GetStaticMethod(typeof(TestAbstractionLib), "GetListOfDesiredNodes");
    var labelNode = methodUnderTest.Invoke(this, prop);


    object[] prop1 = {labelNode, "BIPOLAR", "Chamber", true, 10, 10, false};
    methodUnderTest = Helper.GetStaticMethod(typeof(TestAbstractionLib), "CheckValueIsSetAlready");
    var result = methodUnderTest.Invoke(this, prop1);

    Assert.AreEqual(result, false);
    TestAbstractionLib.UnitTestUtilsClass.CheckerrorinLogFile(true);
在上面的代码片段中,我依次调用了两个私有方法。 第一个私有方法“GetListOfDesiredNodes”将返回一个XMLNode

我需要将该XMLNode作为参数传递给另一个私有方法

正如您所看到的,“labelNode”是一种对象类型,而不是“XMlNode”类型

当我将这个对象类型作为参数传递给下一个方法时,它抛出了一个异常,如下所示

"Object of type 'System.Collections.Generic.List 1[System.Xml.XmlNode]' cannot be converted to type 'System.Xml.XmlNode'."
请给我任何解决方案,我如何将此对象类型转换为XMLNode并调用其他方法


提前感谢大家。

未测试,但这应该可以:

TestAbstractionLib.UnitTestUtilsClass.UnitTestFileName = TestAbstractionLib.UnitTestUtilsClass.VisualDumpAfterValueSet;
object[] prop = { "Patient", false };
var methodUnderTest = Helper.GetStaticMethod(typeof(TestAbstractionLib), "GetListOfDesiredNodes");
var labelNode = methodUnderTest.Invoke(this, prop) as List<XmlNode>;


object[] prop1 = {labelNode.FirstOrDefault(), "BIPOLAR", "Chamber", true, 10, 10, false};
methodUnderTest = Helper.GetStaticMethod(typeof(TestAbstractionLib), "CheckValueIsSetAlready");
var result = methodUnderTest.Invoke(this, prop1);

Assert.AreEqual(result, false);
TestAbstractionLib.UnitTestUtilsClass.CheckerrorinLogFile(true);
TestAbstrationLib.UnitTestUtilClass.UnitTestFileName=TestAbstrationLib.UnitTestUtilClass.VisualDumpAfterValueSet;
object[]prop={“Patient”,false};
var methodUnderTest=Helper.GetStaticMethod(typeof(TestAbstrationLib),“GetListOfDesiredNodes”);
var labelNode=methodUnderTest.Invoke(this,prop)作为列表;
object[]prop1={labelNode.FirstOrDefault(),“BIPOLAR”,“Chamber”,true,10,10,false};
methodUnderTest=Helper.GetStaticMethod(typeof(TestAbstrationLib),“CheckValueIsSetAlready”);
var result=methodUnderTest.Invoke(this,prop1);
Assert.AreEqual(结果,false);
TestAbstrationLib.UnitTestUtilsClass.CheckerrorinLogFile(true);

在没有看到代码的情况下,但从错误判断,您需要传入单个XmlNode,而不是列表。上述操作将第一个方法的结果强制转换为一个列表,然后将第一个元素传递给第二个方法。

未测试,但这应该可以:

TestAbstractionLib.UnitTestUtilsClass.UnitTestFileName = TestAbstractionLib.UnitTestUtilsClass.VisualDumpAfterValueSet;
object[] prop = { "Patient", false };
var methodUnderTest = Helper.GetStaticMethod(typeof(TestAbstractionLib), "GetListOfDesiredNodes");
var labelNode = methodUnderTest.Invoke(this, prop) as List<XmlNode>;


object[] prop1 = {labelNode.FirstOrDefault(), "BIPOLAR", "Chamber", true, 10, 10, false};
methodUnderTest = Helper.GetStaticMethod(typeof(TestAbstractionLib), "CheckValueIsSetAlready");
var result = methodUnderTest.Invoke(this, prop1);

Assert.AreEqual(result, false);
TestAbstractionLib.UnitTestUtilsClass.CheckerrorinLogFile(true);
TestAbstrationLib.UnitTestUtilClass.UnitTestFileName=TestAbstrationLib.UnitTestUtilClass.VisualDumpAfterValueSet;
object[]prop={“Patient”,false};
var methodUnderTest=Helper.GetStaticMethod(typeof(TestAbstrationLib),“GetListOfDesiredNodes”);
var labelNode=methodUnderTest.Invoke(this,prop)作为列表;
object[]prop1={labelNode.FirstOrDefault(),“BIPOLAR”,“Chamber”,true,10,10,false};
methodUnderTest=Helper.GetStaticMethod(typeof(TestAbstrationLib),“CheckValueIsSetAlready”);
var result=methodUnderTest.Invoke(this,prop1);
Assert.AreEqual(结果,false);
TestAbstrationLib.UnitTestUtilsClass.CheckerrorinLogFile(true);

在没有看到代码的情况下,但从错误判断,您需要传入单个XmlNode,而不是列表。上述操作将第一个方法的结果强制转换为列表,然后将第一个元素传递给第二个方法。

这是因为您传递的是节点列表,而不是单个节点。这是因为您传递的是节点列表,而不是单个节点。您应该将labelNode强制转换为列表。。编辑:我看到您已经在这样做了,很抱歉…您应该首先将labelNode转换为List。。编辑:我看到你已经在做了,对不起。。。