gpt4 book ai didi

c# - 无法将带 [] 的索引应用于类型 'CaSTLe.Proxies.RangeProxy' 的表达式

转载 作者:太空宇宙 更新时间:2023-11-03 18:53:43 28 4
gpt4 key购买 nike

是什么导致了这个异常?我正在尝试使用 Moq 来模拟 Microsoft.Office.Interop.Excel.Range。我想在其中嵌套另一个模拟的 Range 。但是当我尝试访问嵌套异常时,抛出了异常。

异常

Cannot apply indexing with [] to an expression of type 'Castle.Proxies.RangeProxy'

代码

[TestMethod]
public void RangeProxyIndexTest()
{
// creating first range
var cell1 = new Moq.Mock<Range>();
cell1.Setup(c => c.Value2).Returns("1");

var range1Mock = new Moq.Mock<Range>();
range1Mock.SetupGet(r => r[1, Moq.It.IsAny<Object>()]).Returns(cell1.Object);
var range1 = range1Mock.Object;

// creating second range
var cell2 = new Moq.Mock<Range>();
cell2.Setup(c => c.Value2).Returns("2");

var range2Mock = new Moq.Mock<Range>();
range2Mock.SetupGet(r => r[1, Moq.It.IsAny<Object>()]).Returns(cell2.Object);
var range2 = range2Mock.Object;

// merging both ranges into 1
var range3Mock = new Moq.Mock<Range>();
range3Mock.SetupGet(r => r[1, Moq.It.IsAny<Object>()]).Returns(range1);
range3Mock.SetupGet(r => r[2, Moq.It.IsAny<Object>()]).Returns(range2);
var range3 = range3Mock.Object;

// testing succeeds
Assert.AreEqual("1", range1[1].Value2);
Assert.AreSame(range1, range3[1]);
Assert.AreEqual("2", range2[1].Value2);
Assert.AreSame(range2, range3[2]);

// exceptions are thrown here
Assert.AreEqual("1", range3[1][1].Value2);
Assert.AreEqual("2", range3[2][1].Value2);
}

最佳答案

虽然情况不一样,但是这个异常的原因和other question you have opened.是完全一样的

将您的验证部分更改为:

Assert.AreEqual("1", (range3[1] as Range)[1].Value2);
Assert.AreEqual("2", (range3[2] as Range)[1].Value2);

有关更多信息,请阅读 this answer.

关于c# - 无法将带 [] 的索引应用于类型 'CaSTLe.Proxies.RangeProxy' 的表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50640057/

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