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);
        }
Пример #2
0
        /// <summary>
        /// Executes a call to a library.
        /// </summary>
        /// <param name="libraryName">Name of the library to load.</param>
        /// <param name="procedureName">Name of the function to call.</param>
        /// <param name="parameters">Array of args to pass to the function.</param>
        /// <param name="parameterTypes">Array of args to pass to the function.</param>
        /// <param name="returnType">Return type of the function.</param>
        /// <param name="legacyDllImportAttribute">[LegacyDllImport] attribute taken from the method definition.</param>
        /// <returns>Result object returned by the library.</returns>
        /// <exception cref="Exception">This Method will rethrow all exceptions thrown by the wrapper.</exception>
        internal object InvokeInternal(string libraryName, string procedureName, object[] parameters, Type[] parameterTypes, Type returnType, LegacyDllMethodAttribute legacyDllImportAttribute)
        {
            AssertNotDisposed();

            var info = new CallData
            {
                LibraryName       = libraryName,
                ProcedureName     = procedureName,
                Parameters        = parameters,
                ParameterTypes    = parameterTypes,
                ReturnType        = returnType,
                CallingConvention = legacyDllImportAttribute.CallingConvention,
                CharSet           = legacyDllImportAttribute.CharSet,
            };

            // Write request to server
            _formatter.Serialize(_pipe, info);

            // Receive result from server
            CallResult callResult = (CallResult)_formatter.Deserialize(_pipe);

            if (callResult.Exception != null)
            {
                throw callResult.Exception;
            }

            AssertLengthOfArgsEquals(parameters, callResult.Parameters);

            for (int i = 0; i < parameters.Length; i++)
            {
                parameters[i] = callResult.Parameters[i];
            }

            return(callResult.Result);
        }