Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a .NET Standard library COM-Visible?

For a full .NET project you can tick a box to make the project COM Visible in Project Properties > Application tab > Assembly Information..:

enter image description here

However, the Assembly Information.. button doesn't exist for .NET Standard projects, instead that data is entered on the Project Properties > Package tab, but that tab doesn't have a checkbox for Make Assembly COM-Visible:

enter image description here

Can you still make .NET Standard libraries COM Visible by some other method, or does it not make sense to do that for some reason?

like image 478
tomRedox Avatar asked Jul 01 '17 13:07

tomRedox


2 Answers

I commented on your issue at GitHub. You can definitely make .NET Standard libraries COM visible, provided the runtime you choose supports both .NET Standard and COM visibility.

Here's an example using Visual Studio 2017 and WiX: ComVisibleNetStandard

like image 90
ken Avatar answered Oct 20 '22 17:10

ken


@ZdeněkJelínek raises an interesting point in the comments on the question, namely that .NET Standard is supposed to be platform agnostic, whereas COM is inherently platform specific.

To test that theory I put the code from the example of COM interop in the C# documentation in a full .NET solution and a .NET Standard solution. This is the code:

using System.Runtime.InteropServices;

namespace project_name
{
    [Guid("EAA4976A-45C3-4BC5-BC0B-E474F4C3C83F")]
    public interface ComClass1Interface
    {
    }

    [Guid("7BD20046-DF8C-44A6-8F6B-687FAA26FA71"), 
        InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface ComClass1Events 
    {
    }

    [Guid("0D53A3E8-E51A-49C7-944E-E72A2064F938"),
        ClassInterface(ClassInterfaceType.None),
        ComSourceInterfaces(typeof(ComClass1Events))]
    public class ComClass1 : ComClass1Interface
    {
    }
}

In full .NET that compiles fine, in .NET standard it compiles fine, but triggers this warning:

'ComInterfaceType.InterfaceIsIDispatch' is obsolete: 'Support for IDispatch may be unavailable in future releases.'

So at first that appears to confirm it, currently you can add the COM interfaces, but they aren't supposed to be there anymore by design. However, that warning shows up in the dotnet standard github repo, and there doesn't appear to be a clear conclusion about whether these members really are obsolete.

Furthermore, System.Runtime.InteropServices.ComVisibleAttribute shows up in this check in for a .NET Stansard 1.6 check in for the System.Runtime.Forwards.cs class in the netstandard codebase, but I don't understand the codebase well enough to know why it's there. Accordingly, I've asked the question on the .NET Standard repo and report back the answer here.

like image 36
tomRedox Avatar answered Oct 20 '22 17:10

tomRedox