glib: Use dictionaries to store type information in GType class

Yay, type safety when managing types...
This commit is contained in:
Bertrand Lorentz 2012-11-04 16:41:17 +01:00
parent 6ecd2d01f9
commit 6e3e73e2ac

View File

@ -23,7 +23,7 @@
namespace GLib { namespace GLib {
using System; using System;
using System.Collections; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Reflection; using System.Reflection;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
@ -88,8 +88,8 @@ namespace GLib {
public static readonly GType Param = new GType ((IntPtr) TypeFundamentals.TypeParam); public static readonly GType Param = new GType ((IntPtr) TypeFundamentals.TypeParam);
public static readonly GType Object = new GType ((IntPtr) TypeFundamentals.TypeObject); public static readonly GType Object = new GType ((IntPtr) TypeFundamentals.TypeObject);
static Hashtable types = new Hashtable (); static IDictionary<IntPtr, Type> types = new Dictionary<IntPtr, Type> ();
static Hashtable gtypes = new Hashtable (); static IDictionary<Type, GType> gtypes = new Dictionary<Type, GType> ();
public static void Register (GType native_type, System.Type type) public static void Register (GType native_type, System.Type type)
{ {
@ -131,8 +131,8 @@ namespace GLib {
GType gtype; GType gtype;
lock (types) { lock (types) {
if (gtypes.Contains (type)) if (gtypes.ContainsKey (type))
return (GType)gtypes[type]; return gtypes[type];
} }
if (type.IsSubclassOf (typeof (GLib.Object))) { if (type.IsSubclassOf (typeof (GLib.Object))) {
@ -189,8 +189,8 @@ namespace GLib {
public static Type LookupType (IntPtr typeid) public static Type LookupType (IntPtr typeid)
{ {
lock (types) { lock (types) {
if (types.Contains (typeid)) if (types.ContainsKey (typeid))
return (Type)types[typeid]; return types[typeid];
} }
string native_name = Marshaller.Utf8PtrToString (g_type_name (typeid)); string native_name = Marshaller.Utf8PtrToString (g_type_name (typeid));
@ -375,8 +375,8 @@ namespace GLib {
internal static GType LookupGObjectType (System.Type t) internal static GType LookupGObjectType (System.Type t)
{ {
lock (types) { lock (types) {
if (gtypes.Contains (t)) if (gtypes.ContainsKey (t))
return (GType) gtypes [t]; return gtypes [t];
} }
PropertyInfo pi = t.GetProperty ("GType", BindingFlags.DeclaredOnly | BindingFlags.Static | BindingFlags.Public); PropertyInfo pi = t.GetProperty ("GType", BindingFlags.DeclaredOnly | BindingFlags.Static | BindingFlags.Public);