Ejemplo n.º 1
0
        static bool applyCustomMarshalling(ParameterInfo source, ParameterBuilder destination)
        {
            // [Marshaller]
            var cm = source.customMarshaller();

            if (null != cm)
            {
                cm.applyDelegateParams(source, destination);
                return(true);
            }

            // [NativeString]
            NativeStringAttribute nsa = source.GetCustomAttribute <NativeStringAttribute>();

            if (null != nsa)
            {
                var cab = new CustomAttributeBuilder(ciMarshalAs, nativeStringType);
                destination.SetCustomAttribute(cab);

                // Copy In & Out attributes, if any
                if (source.hasAttribute <InAttribute>())
                {
                    destination.applyInAttribute();
                }
                if (source.hasAttribute <OutAttribute>())
                {
                    destination.applyOutAttribute();
                }

                return(true);
            }

            return(false);
        }
Ejemplo n.º 2
0
        static void checkParameter(ParameterInfo source)
        {
            var cm = source.customMarshaller();

            if (null != cm)
            {
                cm.getNativeType(source);
                return;
            }

            NativeStringAttribute nsa = source.GetCustomAttribute <NativeStringAttribute>();

            if (null != nsa)
            {
                if (source.ParameterType != typeof(string) && source.ParameterType != typeof(StringBuilder))
                {
                    throw new ArgumentException($"[NativeString] must be applied to parameters of type string");
                }
                return;
            }

            if (source.isComInterface())
            {
                return;
            }

            if (source.ParameterType.IsByRef)
            {
                Type unwrapped = source.ParameterType.unwrapRef();
                if (unwrapped.isDelegate())
                {
                    throw new ArgumentException($"You can only pass delegates as input parameters");
                }
                if (source.IsIn && !unwrapped.IsValueType)
                {
                    throw new ArgumentException($"in/out ref parameters only supported for value types");
                }
            }

            if (source.ParameterType.isDelegate())
            {
                if (!source.ParameterType.hasCustomAttribute <UnmanagedFunctionPointerAttribute>())
                {
                    throw new ArgumentException($"Parameter \"{ source.Name }\" of the method { source.Member.DeclaringType.FullName }.{ source.Member.Name } is a delegate without [UnmanagedFunctionPointer] attribute.");
                }
            }
        }