Как получить доступ к. NET Core 3.1 COM объекту из другого приложения? - PullRequest
4 голосов
/ 27 января 2020

Я сделал пример. NET Ядро 3.1 COM-объект, используя это руководство https://docs.microsoft.com/en-us/dotnet/core/native-interop/expose-components-to-com

using System;
using System.Runtime.InteropServices;
using System.Reflection;

[assembly: AssemblyKeyFileAttribute("xxx.snk")]
namespace authcore
{
    [ComVisible(true)]
    [Guid("8C097D17-69ED-417E-A357-0299D07F6B2B")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IServer
    {
        public int GetValue();
    }

    [ComVisible(true)]
    [Guid("7C046A5F-0AC9-4F82-B8AB-B6F1E2FA8DF9")]
    public class Server : IServer
    {
        public int GetValue()
        {
            return 335;
        }
    }
}

Я создал ® зарегистрированную библиотеку COM-объектов DLL. Затем я хотел получить доступ к методу GetValue из приложения. Я пытался использовать команды powershell:

PS D:\> $obj = new-object -comObject ‘authcore.Server’
PS D:\> $obj | get-member


   TypeName: System.__ComObject

Name                      MemberType Definition
----                      ---------- ----------
CreateObjRef              Method     System.Runtime.Remoting.ObjRef CreateObjRef(type requestedType)
Equals                    Method     bool Equals(System.Object obj)
GetHashCode               Method     int GetHashCode()
GetLifetimeService        Method     System.Object GetLifetimeService()
GetType                   Method     type GetType()
InitializeLifetimeService Method     System.Object InitializeLifetimeService()
ToString                  Method     string ToString()


PS D:\> $obj.GetValue()
Method not supported.
+ $obj.GetValue()
+ ~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], NotSupportedException
    + FullyQualifiedErrorId : System.NotSupportedException

Пожалуйста, скажите мне, что я пропустил?

...