コード例 #1
0
ファイル: GirArrayType.cs プロジェクト: BartonCline/gisharp
 public GirArrayType(GirType type)
 {
     if (type == null)
     {
         throw new ArgumentNullException(nameof(type));
     }
     this.type = type;
 }
コード例 #2
0
 public GirGenericType(GirType type, params Type[] parameters)
 {
     if (type == null)
     {
         throw new ArgumentNullException(nameof(type));
     }
     if (parameters == null)
     {
         throw new ArgumentNullException(nameof(parameters));
     }
     this.type       = type;
     this.parameters = parameters;
 }
コード例 #3
0
        public static Type ResolveType(string typeName, XDocument document)
        {
            var type = GetType(typeName);

            if (type != null)
            {
                return(type);
            }

            var isArray = false;

            if (typeName.EndsWith("[]", StringComparison.Ordinal))
            {
                typeName = typeName.Remove(typeName.Length - "[]".Length);
                isArray  = true;
            }
            var isGeneric   = false;
            var genericArgs = default(Type[]);

            if (typeName.Contains('`'))
            {
                isGeneric   = true;
                genericArgs = string.Concat(typeName
                                            .SkipWhile(x => x != '[')
                                            .Skip(1)
                                            .TakeWhile(x => x != ']'))
                              .Split(',')
                              .Select(x => ResolveType(x, document))
                              .ToArray();
                typeName = typeName.Remove(typeName.IndexOf('['));
                type     = ResolveType(typeName, document);
            }

            switch (typeName)
            {
            case "GLib.List":
                type = typeof(GISharp.GLib.List <>);
                if (genericArgs[0] == typeof(IntPtr))
                {
                    genericArgs[0] = typeof(Opaque);
                }
                break;

            case "GLib.SList":
                type = typeof(GISharp.GLib.SList <>);
                if (genericArgs[0] == typeof(IntPtr))
                {
                    genericArgs[0] = typeof(Opaque);
                }
                break;

            case "GLib.HashTable":
                type = typeof(GISharp.GLib.HashTable <,>);
                if (genericArgs[0] == typeof(IntPtr))
                {
                    genericArgs[0] = typeof(Opaque);
                }
                if (genericArgs[1] == typeof(IntPtr))
                {
                    genericArgs[1] = typeof(Opaque);
                }
                break;

            case "GLib.Array":
                type = typeof(GISharp.GLib.Array <>);
                if (genericArgs[0] == typeof(IntPtr))
                {
                    genericArgs[0] = typeof(Opaque);
                }
                break;

            case "GLib.PtrArray":
                type = typeof(GISharp.GLib.PtrArray <>);
                if (genericArgs[0] == typeof(IntPtr))
                {
                    genericArgs[0] = typeof(Opaque);
                }
                break;

            case "GLib.ByteArray":
                type      = typeof(GISharp.GLib.ByteArray);
                isGeneric = false;
                break;

            case "GLib.Quark":
                type = typeof(GISharp.GLib.Quark);
                break;
            }

            if (type == null)
            {
                XElement typeDefinitionElement;
                if (!girTypeCache.TryGetValue(typeName, out typeDefinitionElement))
                {
                    var unqualifiedTypeName = typeName.Split('.').Last();
                    typeDefinitionElement = document.Descendants()
                                            .Where(d => Fixup.ElementsThatDefineAType.Contains(d.Name))
                                            .SingleOrDefault(d => d.Attribute(gs + "managed-name").Value == unqualifiedTypeName);
                    if (typeDefinitionElement == null)
                    {
                        // special case for callbacks since there is a "Unmanaged" version of each of those as well.
                        typeDefinitionElement = document.Descendants(gi + "callback")
                                                .SingleOrDefault(d => "Unmanaged" + d.Attribute(gs + "managed-name").Value == unqualifiedTypeName);
                    }
                    if (typeDefinitionElement == null)
                    {
                        // special case for interfaces since we add the "I" prefix.
                        typeDefinitionElement = document.Descendants(gi + "interface")
                                                .SingleOrDefault(d => "I" + d.Attribute(gs + "managed-name").Value == unqualifiedTypeName);
                    }
                    if (typeDefinitionElement != null)
                    {
                        girTypeCache.Add(typeName, typeDefinitionElement);
                    }
                }
                if (typeDefinitionElement != null)
                {
                    type = new GirType(typeDefinitionElement);
                }
            }

            if (type != null)
            {
                if (isGeneric)
                {
                    type = type.MakeGenericType(genericArgs);
                }
                if (isArray)
                {
                    type = type.MakeArrayType();
                }
                return(type);
            }

            // If we couldn't find a matching type, try adding the GISharp namespace prefix
            if (!typeName.StartsWith($"{MainClass.parentNamespace}.", StringComparison.Ordinal))
            {
                return(ResolveType($"{MainClass.parentNamespace}.{typeName}", document));
            }

            throw new TypeNotFoundException(typeName);
        }