public void Intercept(IInvocation invocation)
        {
            Raise.ObjectDisposedException.If(_isDisposed, nameof(WrapperClientInterceptor));

            // Early out if it's a call to Dispose()
            if (invocation.Method.Name == nameof(IDisposable.Dispose))
            {
                Dispose();
                return;
            }

            Type[] parameterTypes = GetParameterTypesFromInvocation(invocation);
            Type   returnType     = invocation.Method.ReturnType;

            LegacyDllImportAttribute dllImportAttribute = GetLegacyAttribute <LegacyDllImportAttribute>(_interfaceType);
            LegacyDllMethodAttribute dllMethodAttribute = GetLegacyAttribute <LegacyDllMethodAttribute>(invocation.Method);

            string libraryName = _libraryNameProvider.GetLibraryName(dllImportAttribute);

            var callData = new CallData
            {
                LibraryName       = libraryName,
                ProcedureName     = invocation.Method.Name,
                Parameters        = invocation.Arguments,
                ParameterTypes    = parameterTypes,
                ReturnType        = returnType,
                CallingConvention = dllMethodAttribute.CallingConvention,
                CharSet           = dllMethodAttribute.CharSet,
            };

            invocation.ReturnValue = _wrapperClient.InvokeInternal(callData);
        }
        public void Intercept(IInvocation invocation)
        {
            AssertIsNotDesposed();

            string methodName = invocation.Method.Name;

            // Early out if it'a call to Dispose()
            if (methodName == nameof(IDisposable.Dispose))
            {
                _wrapperClient.Dispose();
                _isDisposed = true;
                return;
            }

            object[] parameters     = invocation.Arguments;
            Type[]   parameterTypes = invocation.Method.GetParameters().Select(x => x.ParameterType).ToArray();
            Type     returnType     = invocation.Method.ReturnType;

            var dllImportAttribute = GetLegacyAttribute <LegacyDllImportAttribute>(_interfaceType);
            var dllMethodAttribute = GetLegacyAttribute <LegacyDllMethodAttribute>(invocation.Method);

            string libraryName = dllImportAttribute.LibraryName;

            if (OverrideLibraryName != null)
            {
                libraryName = OverrideLibraryName;
            }

            invocation.ReturnValue = _wrapperClient.InvokeInternal(libraryName, methodName, parameters, parameterTypes, returnType, dllMethodAttribute);
        }
Esempio n. 3
0
        public void TestWrapperClientMakesOneSendReceiveCycle()
        {
            Mock <IPipeConnector> pipeConnectorMock = new Mock <IPipeConnector>();

            pipeConnectorMock.Setup(m => m.ReceiveCallResponse())
            .Returns(new CallResult()
            {
                Parameters = new object[0]
            });
            WrapperClient wrapperClient = new WrapperClient(pipeConnectorMock.Object);

            wrapperClient.InvokeInternal(new CallData()
            {
                Parameters = new object[0]
            });

            pipeConnectorMock.Verify(m => m.SendCallRequest(It.IsAny <CallData>()), Times.Once);
            pipeConnectorMock.Verify(m => m.ReceiveCallResponse(), Times.Once);
        }
Esempio n. 4
0
        public void TestCallParametersAreCopied()
        {
            object[] passedParameters   = { 1, 2, 3 };
            object[] returnedParameters = { 3, 2, 1 };
            CallData callData           = new CallData()
            {
                Parameters = passedParameters
            };

            Mock <IPipeConnector> pipeConnectorMock = new Mock <IPipeConnector>();

            pipeConnectorMock.Setup(m => m.ReceiveCallResponse())
            .Returns(new CallResult()
            {
                Parameters = returnedParameters
            });
            WrapperClient wrapperClient = new WrapperClient(pipeConnectorMock.Object);

            wrapperClient.InvokeInternal(callData);

            CollectionAssert.AreEqual(returnedParameters, passedParameters);
            Assert.AreNotSame(passedParameters, returnedParameters);
        }