gpt4 book ai didi

c# "as"与 "()"转换

转载 作者:行者123 更新时间:2023-12-02 05:22:29 26 4
gpt4 key购买 nike

<分区>

为什么兼容引用类型之间的转换在这种情况下会编译(Excel 2010、.Net 4.5)

using Excel = Microsoft.Office.Interop.Excel;
Excel.Application excelApplication = null;
excelApplication = new Excel.Application();

Excel.Worksheet worksheet = workbook.Worksheets[1] as Excel.Worksheet;

在下面的例子中它不会,尽管我看到了这样的例子:

Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];

在这种情况下,我得到以下编译错误:

> CSC : error CS0518: Predefined type 'Microsoft.CSharp.RuntimeBinder.Binder' is not 
defined or imported

> error CS1969: One or more types required to compile a dynamic expression cannot be
found. Are you missing a reference?

最好的,

编辑:感谢下面的两位回答者,以下解释听起来很合理:

without including Microsoft.CSharp in the Project References for projects with .Net Version >= 4.0, support for inter operation between the Dynamic Language Runtime (DLR) and C# is not possible, i.e. no dynamic cast is possible.

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