60 lines
1.6 KiB
C#
60 lines
1.6 KiB
C#
using System;
|
|
using System.Reflection;
|
|
using System.Runtime.ExceptionServices;
|
|
using ArchestrA.MxAccess;
|
|
|
|
namespace MxGateway.Worker.MxAccess;
|
|
|
|
public sealed class MxAccessComServer : IMxAccessServer
|
|
{
|
|
private readonly object mxAccessComObject;
|
|
|
|
public MxAccessComServer(object mxAccessComObject)
|
|
{
|
|
this.mxAccessComObject = mxAccessComObject ?? throw new ArgumentNullException(nameof(mxAccessComObject));
|
|
}
|
|
|
|
public int Register(string clientName)
|
|
{
|
|
if (mxAccessComObject is ILMXProxyServer mxAccessServer)
|
|
{
|
|
return mxAccessServer.Register(clientName);
|
|
}
|
|
|
|
return (int)Invoke(nameof(Register), clientName);
|
|
}
|
|
|
|
public void Unregister(int serverHandle)
|
|
{
|
|
if (mxAccessComObject is ILMXProxyServer mxAccessServer)
|
|
{
|
|
mxAccessServer.Unregister(serverHandle);
|
|
return;
|
|
}
|
|
|
|
Invoke(nameof(Unregister), serverHandle);
|
|
}
|
|
|
|
private object Invoke(
|
|
string methodName,
|
|
params object[] arguments)
|
|
{
|
|
try
|
|
{
|
|
return mxAccessComObject
|
|
.GetType()
|
|
.InvokeMember(
|
|
methodName,
|
|
BindingFlags.Instance | BindingFlags.Public | BindingFlags.InvokeMethod,
|
|
binder: null,
|
|
target: mxAccessComObject,
|
|
args: arguments);
|
|
}
|
|
catch (TargetInvocationException exception) when (exception.InnerException is not null)
|
|
{
|
|
ExceptionDispatchInfo.Capture(exception.InnerException).Throw();
|
|
throw;
|
|
}
|
|
}
|
|
}
|