gpt4 book ai didi

c# - 模拟对象不具有 Intellisense 中显示的所有属性 - 在一个项目中但在另一个项目中具有它们

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

我正在模拟 VSTO 对象,在一个项目中(我没有写)它有这样的代码:

var listOfSheets = new List<Worksheet>();
var mockSheets = Substitute.For<Sheets>();
mockSheets.Count.Returns(listOfSheets.Count);

mockSheets 的 Intellisense 工具提示显示 6 个属性:

NSubstitute All Properties

带有断点的行在这个项目中有效。

但是我在不同的项目中有相同的代码(相同的引用、命名空间等),但是 mockSheets 的 Intellisense 工具提示只显示 1 个属性:

NSubstitute Only one Property

我知道这是我要解决的根本原因,但实际问题是:

Cannot perform runtime binding on a null reference

Cannot perform runtime binding on a null reference

编辑:

Sheet 对象被模拟:

public static Worksheet Sheet
{
get
{
var mockSheet = Substitute.For<Worksheet>();
mockSheet.Name = MockSheetName;
mockSheet.Visible = XlSheetVisibility.xlSheetVisible;

return mockSheet;
}
}

public static Workbook Workbook()
{
return Workbook(1);
}

最佳答案

这是一个大胆的猜测,但 Office Interop 数组是基于 1 的,而不是基于 0 的。我没有调查过,但这可能在元数据中定义。试试这个:

for (int i = 0; i < numSheets; i++)
{
listOfSheets.Add(Sheet);
listOfSheets[i].Name = MockSheetName + (i + 1);
`mockSheets[i + 1].Returns(listOfSheets[i]);`
}

关于c# - 模拟对象不具有 Intellisense 中显示的所有属性 - 在一个项目中但在另一个项目中具有它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9816243/

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