C#Interop不可发音成员'Microsoft.Office.Interop.Excel.Range.End'不能像方法一样使用 [英] C# Interop Non-invocable member 'Microsoft.Office.Interop.Excel.Range.End' cannot be used like a method

查看:109
本文介绍了C#Interop不可发音成员'Microsoft.Office.Interop.Excel.Range.End'不能像方法一样使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#Interop从工作表中获取一些值,并且出现以下错误:

I'm using C# Interop to get some values from a Worksheet and I get the following error:

不可发音的成员'Microsoft.Office.Interop.Excel.Range.End'不能像方法一样使用.

Non-invocable member 'Microsoft.Office.Interop.Excel.Range.End' cannot be used like a method.

这是我的代码:

var wb = (Excel.Workbook)Globals.ThisAddIn.Application.ActiveWorkbook;
var wsEvars = wb.Sheets["Evars"];
var wsProps = wb.Sheets["Props"];
var wsEvents = wb.Sheets["Events"];
var wsListVars = wb.Sheets["List Vars"];

var sheetList = new Excel.Worksheet[] { wsEvars, wsProps, wsEvents, wsListVars };

for (var i = 0; i < sheetList.Length; i++)
{
    // I get the error on the line below
    var rowLast = sheetList[i].Range["I" + sheetList[i].Rows.Count].End(Excel.XlDirection.xlUp).Row;
}

如果我尝试如下操作,那是可行的:

The thing is that is works if I try as follows:

for (var i = 0; i < sheetList.Length; i++)
{
    var rowLast = wsEvars .Range["I" + wsEvars .Rows.Count].End(Excel.XlDirection.xlUp).Row;
}

我想念什么吗?

推荐答案

好像您在C#编译器中发现了一个错误.该错误实际上存在于解决方法中,出于第一个代码片段不存在的相同原因,应该不进行编译.尽管很难肯定地说这是一个错误,但C#语言规范并未描述这种情况下可接受的内容.

Looks like you found a bug in the C# compiler. The bug is actually present in the workaround, it ought to not compile for the same reasons the first snippet did not. Albeit that it is difficult to definitely claim that this is a bug, the C# language spec does not describe what is acceptable in this case.

Range.End属性是索引属性. C#不正式支持此类属性,该语言仅允许类索引器(aka this[])成为类的唯一索引属性.但是在C#版本4中取消了该限制,特别是为了简化与COM服务器的互操作.像Excel一样,索引属性在COM对象模型中非常常见.

The Range.End property is an indexed property. Such properties are not formally supported in C#, the language permits only the class indexer (aka this[]) to be the one-and-only indexed property of a class. But that restriction was lifted in C# version 4, specifically to make interop with COM servers easier. Like Excel, indexed properties are very common in COM object models.

与普通索引器一样,您必须使用方括号.修复:

Like the normal indexer, you have to use square brackets. Fix:

    var rowLast = sheetList[i].Range["I" + sheetList[i].Rows.Count]
                              .End[Excel.XlDirection.xlUp].Row;

仍然可以使用在较旧的C#版本中必须使用的解决方法:

And the workaround you had to use in older C# versions is still available:

    var rowLast = sheetList[i].Range["I" + sheetList[i].Rows.Count]
                              .get_End(Excel.XlDirection.xlUp).Row;

很难猜测为什么它会在第二个代码段中找到可接受的括号.它看起来像个虫子,像个虫子一样游动,像个虫子一样嘎嘎叫,所以很可能是个虫子.单击新问题"按钮,让他们知道这一点.我怀疑他们会解决这个问题,但是可能有比目睹更多的错误.

Hard to guess why it finds () parentheses acceptable in the second snippet. It looks like a bug, swims like a bug and quacks like a bug, so it is probably a bug. Let them know about it by clicking the New Issue button. I doubt they'll fix it but there might be more wrong than meets the eye.

这篇关于C#Interop不可发音成员'Microsoft.Office.Interop.Excel.Range.End'不能像方法一样使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆