Home

How does LinqPad 7 with .NET 6 support Office COM and LinqToSql?

edited July 2022

I noticed that the Add References window in Linqpad 7 has buttons specifically for adding references to Office COM interop assemblies and LinqToSql.

...which I thought shouldn't be possible because Microsoft has made it clear that you can only use .NET Framework 4.x to interop with Office' COM automation APIs, i.e. that we cannot use .NET Core nor .NET 5 or later.:

VSTO relies on the .NET Framework. COM add-ins can also be written with the .NET Framework. Office Add-ins cannot be created with .NET Core and .NET 5+, the latest versions of .NET. This is because .NET Core/.NET 5+ cannot work together with .NET Framework in the same process and may lead to add-in load failures.

...but LinqPad 7 seems to support it just fine and it works on my machine to control Word and Excel (I'm currently on Office 365, version 2109). How does it do this - and can we use whatever tricks it uses to use Office automation from normal .NET 6 programs?

As for LinqToSql, I see that LinqPad includes a private build of System.Data.Linq.dll to .NET 6 - I'm assuming this was rebuilt from the original .NET Framework-exclusive assemblies - but to what extent can we use it outside of LinqPad? (Not that I'd ever recommend it over EF Core, but I'm just curious)

Comments

  • The Office Interop feature adds NuGet package references that contain .NET COM wrappers for Office automation. I don't think this is the same as VSTO, which allows for the creation of add-ins.

    The LINQ-to-SQL assembly is a fork of the .NET Framework version with some enhancements. The source is available here:
    https://github.com/albahari/LinqToSQL2

  • The Office Interop sample code snippets that come with my Linqpad v7.6.6 (x64) stopped working for me.

    On line

    var excel = new Excel.Application();

    This error is trown
    InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Excel.Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{000208D5-0000-0000-C000-000000000046}' failed due to the following error: Fout bij laden van type DLL-bestand. (0x80029C4A (TYPE_E_CANTLOADLIBRARY)).

    Just for good measure, I imported the Nuget Microsoft.Office.Interop.Excel v.15.0.4795.1001

    <Query Kind="Statements">
      <NuGetReference>Microsoft.Office.Interop.Excel</NuGetReference>
      <Namespace>Excel = Microsoft.Office.Interop.Excel</Namespace>
      <Namespace>Microsoft.Office.Interop.Excel</Namespace>
      <DisableMyExtensions>true</DisableMyExtensions>
    </Query>
    
    var excel = new Excel.Application();
    
Sign In or Register to comment.