Make a .NET Standard library COM-Visible?

只谈情不闲聊 提交于 2019-12-01 17:29:24

问题


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

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:

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?


回答1:


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




回答2:


@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.



来源:https://stackoverflow.com/questions/44861236/make-a-net-standard-library-com-visible

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!