How to: 解决 Object does not contain a definition for get_Range
2010-10-13 10:58
453 查看
在VS2010中调用COM Interop DLL操作Excel通过get_Range去获取Range时,会发生Object does not contain a definition for get_Range的错误。其原因和解决方案:
Misha's explanation is correct - when using No PIA, methods returning object are treated as if they return dynamic in order to simulate the VBA semantics of COM Variants. Because the return value of sh.Cells is Object, sh.get_Range is dispatched dynamically, and the dynamic COM binder does not support the get_Range syntax exposed in C# before indexed properties were supported. We've tried to maintain backwards compatibility wherever possible when you turn on Embed Interop Types for a COM reference, but this is one place where some further tweaking is required.
The workaround you proposed will work - a cleaner workaround is the one Mike Rosenblum pointed out to use C# 4.0's new indexed properties syntax, which the dynamic COM binder does understand. You can then represent the operation with the following code:
Excel.Range r = sh.Range[sh.Cells[1, 1], sh.Cells[2, 2]];
See Also
get_Range method missing with embedded interop assembly
Misha's explanation is correct - when using No PIA, methods returning object are treated as if they return dynamic in order to simulate the VBA semantics of COM Variants. Because the return value of sh.Cells is Object, sh.get_Range is dispatched dynamically, and the dynamic COM binder does not support the get_Range syntax exposed in C# before indexed properties were supported. We've tried to maintain backwards compatibility wherever possible when you turn on Embed Interop Types for a COM reference, but this is one place where some further tweaking is required.
The workaround you proposed will work - a cleaner workaround is the one Mike Rosenblum pointed out to use C# 4.0's new indexed properties syntax, which the dynamic COM binder does understand. You can then represent the operation with the following code:
Excel.Range r = sh.Range[sh.Cells[1, 1], sh.Cells[2, 2]];
See Also
get_Range method missing with embedded interop assembly
相关文章推荐
- object does not contain a definition for get_range
- 在VS2010中调用COM Interop DLL操作Excel通过get_Range去获取Range时,会发生Object does not contain a definition for get_Range的错误。其原因和解决方案
- 'Microsoft.Reporting.WebForms.ReportViewer' does not contain a definition for 'Reset'解决方法(转)
- error CS1061: Type `UnityEngine.Mesh' does not contain a definition for `GetTriangleStrip' and no ex
- MyBatis:Parameter Maps collection does not contain value for 的问题解决
- Mapped Statements collection does not contain value for com.atsig.uac.persistence.AccountMapper.getAllAccounts
- Mapped Statements collection does not contain value for的解决方法
- System.IO.File does not contain a definition for ReadAllBytes
- Mapped Statements collection does not contain value for 解决方法
- Mapped Statements collection does not contain value for真正的解决办法
- eclipse有关调试问题的解决The source attachment does not contain the source for the file
- Mapped Statements collection does not contain value for错误解决方法
- C#编译问题'System.Collections.Generic.IEnumerable' does not contain a definition for 'Where' and no extension method 'Where' accepting a first argument
- python 报错TypeError: 'range' object does not support item assignment,解决方法
- MyBatis:Parameter Maps collection does not contain value for 的问题解决
- error CS0117: `System.IO.File' does not contain a definition for `WriteAllBytes'
- Custom control-- Error:System.Web.UI.UserControl' does not contain a definition for 'LoadCatalog ' a
- Mapped Statements collection does not contain value for 解决方法
- eclipse有关调试问题的解决The source attachment does not contain the source for the file
- Result map does not contain a value for Java.lang.Integer 报错解决办法