Beispiel #1
0
        /// <summary>
        /// Registers the native interop signature.
        /// </summary>
        /// <param name="csMethod">The cs method.</param>
        private void RegisterNativeInteropSignature(CsMethod csMethod)
        {
            // Tag if the method is a function
            var cSharpInteropCalliSignature = new InteropMethodSignature {
                IsFunction = (csMethod is CsFunction)
            };

            // Handle Return Type parameter
            // MarshalType.Type == null, then check that it is a structure
            if (csMethod.ReturnType.PublicType is CsStruct || csMethod.ReturnType.PublicType is CsEnum)
            {
                // Return type and 1st parameter are implicitly a pointer to the structure to fill
                if (csMethod.IsReturnStructLarge)
                {
                    cSharpInteropCalliSignature.ReturnType = typeof(void *);
                    cSharpInteropCalliSignature.ParameterTypes.Add(typeof(void *));
                }
                else
                {
                    // Patch for Mono bug with structs marshalling and calli.
                    var returnQualifiedName = csMethod.ReturnType.PublicType.QualifiedName;
                    if (returnQualifiedName == Manager.GlobalNamespace.GetTypeName("Result"))
                    {
                        cSharpInteropCalliSignature.ReturnType = typeof(int);
                    }
                    else if (returnQualifiedName == Manager.GlobalNamespace.GetTypeName("PointerSize"))
                    {
                        cSharpInteropCalliSignature.ReturnType = typeof(void *);
                    }
                    else
                    {
                        cSharpInteropCalliSignature.ReturnType = csMethod.ReturnType.PublicType.QualifiedName;
                    }
                }
            }
            else if (csMethod.ReturnType.MarshalType.Type != null)
            {
                Type type = csMethod.ReturnType.MarshalType.Type;
                cSharpInteropCalliSignature.ReturnType = type;
            }
            else
            {
                throw new ArgumentException(string.Format(System.Globalization.CultureInfo.InvariantCulture, "Invalid return type {0} for method {1}", csMethod.ReturnType.PublicType.QualifiedName, csMethod.CppElement));
            }

            // Handle Parameters
            foreach (var param in csMethod.Parameters)
            {
                InteropType interopType;
                string      publicName = param.PublicType.QualifiedName;
                // Patch for Mono bug with structs marshalling and calli.
                if (publicName == Manager.GlobalNamespace.GetTypeName("PointerSize"))
                {
                    interopType = typeof(void *);
                }
                else if (param.MarshalType.Type == null)
                {
                    if (param.PublicType is CsStruct)
                    {
                        // If parameter is a struct, then a LocalInterop is needed
                        interopType = param.PublicType.QualifiedName;
                        cSharpInteropCalliSignature.IsLocal = true;
                    }
                    else
                    {
                        throw new ArgumentException(string.Format(System.Globalization.CultureInfo.InvariantCulture, "Invalid parameter {0} for method {1}", param.PublicType.QualifiedName, csMethod.CppElement));
                    }
                }
                else
                {
                    Type type = param.MarshalType.Type;
                    // Patch for Mono bug with structs marshalling and calli.
                    if (type == typeof(IntPtr))
                    {
                        type = typeof(void *);
                    }
                    interopType = type;
                }

                cSharpInteropCalliSignature.ParameterTypes.Add(interopType);
            }

            var assembly = csMethod.GetParent <CsAssembly>();

            cSharpInteropCalliSignature = assembly.Interop.Add(cSharpInteropCalliSignature);

            csMethod.Interop = cSharpInteropCalliSignature;
        }