question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"dynamic" data type is recognized as "object" when using Excel COM

See original GitHub issue

I have the following simple code which defines last cell with non-empty data, defines its row and gets its value:

using Excel = Microsoft.Office.Interop.Excel;

Excel.Application xlApp = new Excel.Application { Visible = true };
Excel.Workbook xlBook = xlApp.Workbooks[1];
Excel.Worksheet sheet = xlBook.Sheets[1] as Excel.Worksheet;
int lastRow = sheet.Cells[sheet.Rows.Count, "Q"].End[Excel.XlDirection.xlUp].Row;
dynamic value = sheet.Cells[lastRow, "Q"].Value;

When I’m building the project using .NET 5 (5.0.101 or 5.0.200-preview.20601.7 SDK), I get the following two errors in Output window:

error CS1061: ‘object’ does not contain a definition for ‘End’ and no accessible extension method ‘End’ accepting a first argument of type ‘object’ could be found (are you missing a using directive or an assembly reference?)

error CS1061: ‘object’ does not contain a definition for ‘Value’ and no accessible extension method ‘Value’ accepting a first argument of type ‘object’ could be found (are you missing a using directive or an assembly reference?)

Here are problems:

  1. When I’m using .NET Framework 4.8, the project is built fine without any errors and code executes perfectly. In .NET 5, however, the dynamic data type is somehow recognized as object.
  2. I emphasized “in Output window”, because now errors do not show up in “Error List” anymore - they are now seen in “Output” window. For instance, if I remove last semicolon in my code, no errors appear in “Error List”. Instead, I should build project first, and only then error appears, but in “Output” window rather than “Error List” window.

Visual Studio Version: 16.9.0 Preview 2.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:17 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
jaredparcommented, Feb 22, 2021

@terrajobst helpfully figured out what I was missing and created a repro. The part I was missing is that it needs to be a WPF project. In that case the reference is not passed as EmbedInteropTypes.

image

Moving to the WPF repo for triage.

1reaction
ryalanmscommented, Feb 23, 2021

Thank you @JohnyL and @jaredpar for the report and investigation. Capitalizing the ‘T’ worked for me locally.

    <ItemGroup>
             ...
            <EmbedInteropTypes>True</EmbedInteropTypes>
        </COMReference>
    </ItemGroup>
Read more comments on GitHub >

github_iconTop Results From Across the Web

"dynamic" data type is recognized as "object" when using ...
I have the following simple code which defines last cell with non-empty data, defines its row and gets its value: using Excel =...
Read more >
Excel COM interop - why are return types often dynamic?
Many COM methods allow for variation in argument types and return type by designating the types as object. This has necessitated explicit ...
Read more >
Using type dynamic
Learn how to use the dynamic type. The dynamic type is a static type, but dynamic objects bypass static type checking.
Read more >
Excel data types: Stocks and geography
Get stock prices and geographic-based data using the Stocks and Geography data types. These two data types are new, and they are considered...
Read more >
Solved: Writing to a dynamically created excel file
I have an Excel template already stored in a folder in OneDrive and I am using "Get File contents" from the template excel...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found