将.net 4.0语法转换为.net 3.5 [英] convert .net 4.0 syntax OfType to .net 3.5

查看:166
本文介绍了将.net 4.0语法转换为.net 3.5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个.NET 4.0代码:

I have this .NET 4.0 code:

var myTable = tables[1];
myTable = tables.Item["Table1"];
myTable = tables.OfType<Excel.ListObject>().FirstOrDefault(t => t.Name == "Table1");

我正在尝试使用.NET 3.5编译并收到一堆错误:

I am trying to compile with .NET 3.5 and getting a bunch of errors:


错误23
语言不支持属性,索引器或事件'Item'尝试直接调用访问器方法
'Microsoft.Office.Interop.Excel.ListObjects.get_Item(object)'

Error 23 Property, indexer, or event 'Item' is not supported by the language; try directly calling accessor method 'Microsoft.Office.Interop.Excel.ListObjects.get_Item(object)'


错误24'Microsoft.Office.Interop.Excel.ListObjects'不包含'OfType'的
a定义和最佳扩展名方法重载
'System.Linq.Queryable.OfType(System.Linq.IQueryable)'有
一些无效参数

Error 24 'Microsoft.Office.Interop.Excel.ListObjects' does not contain a definition for 'OfType' and the best extension method overload 'System.Linq.Queryable.OfType(System.Linq.IQueryable)' has some invalid arguments


错误25实例参数:无法从
'Microsoft.Office.Interop.Excel.ListObjects'转换为
'System.Linq.IQueryable'

Error 25 Instance argument: cannot convert from 'Microsoft.Office.Interop.Excel.ListObjects' to 'System.Linq.IQueryable'

你能指导我这些错误是什么意思,以便我可以尝试将其转换为.NET 3.5?

Can you please guide me on exactly what these errors mean so that I can try to convert this to .NET 3.5?

非常感谢您的建议。

推荐答案

这个问题是指针对此命令至少需要两行源数据;代码在代码中显示了访问表/ listobject的3种不同方式。

This question refers to an answer given for This command requires at least two rows of source data; the code is showing 3 different ways of accessing a table/listobject in code.

这是我发布的代码:

    var myTable = tables[1];
    var myTable = tables.Item["Table1"];
    var myTable = tables.OfType<Excel.ListObject>().FirstOrDefault(t => t.Name == "Table1");

我的意思是说明在代码中访问表的方式有所不同。

I meant to illustrate that there are different ways of accessing a table in code.

为了把它放在上下文中,我创建了一个带有一个名为TestButton的按钮。这是整个代码,应该使用3.5:

To put it in context, I have created a Ribbon with a single button called "TestButton". Here is the entire code, should work with 3.5:

using Excel = Microsoft.Office.Interop.Excel;


    private void TestButton_Click(object sender, RibbonControlEventArgs e)
    {
        var worksheet = (Excel.Worksheet) Globals.ThisAddIn.Application.ActiveSheet;
        var tables = worksheet.ListObjects;
        var table = tables.Item["Table1"]; // this is the line you are referring to

        //do something with table
    }

这篇关于将.net 4.0语法转换为.net 3.5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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