Load cache on Application.Init, some more tweaks

This commit is contained in:
Maarten Bosmans 2009-12-28 22:10:00 +01:00 committed by Sebastian Dröge
parent 7a1154182b
commit 21ad48628e

View file

@ -62,24 +62,24 @@ namespace Gst {
private static Dictionary<string,Type> TypeCache = new Dictionary<string,Type> (); private static Dictionary<string,Type> TypeCache = new Dictionary<string,Type> ();
// Recursively check for types with GTypeNameAttribute and put them in TypeCache, // Recursively check for types with GTypeNameAttribute and put them in TypeCache,
// but only gstreamer-sharp is in the chain of referenced assemblies. // but only if gstreamer-sharp is in the chain of referenced assemblies.
private static bool PutAssemblyTypesInCache (Assembly asm) private static void PutAssemblyTypesInCache (Assembly asm)
{ {
bool result;
// If already visited, return immediately // If already visited, return immediately
if (AssemblyTypesInCache.TryGetValue(asm.GetHashCode (), out result)) if (AssemblyTypesInCache.ContainsKey(asm.GetHashCode ()))
return result; return;
result = false; // Add with false to avoid chasing circular dependencies
AssemblyTypesInCache.Add (asm.GetHashCode (), false); AssemblyTypesInCache.Add (asm.GetHashCode (), false);
// Result is true for gstreamer-sharp or if a referenced assembly results in true // Result is true for gstreamer-sharp or if a referenced assembly results in true
if (asm.GetName().Name == "gstreamer-sharp") bool result = asm.GetName().Name.Equals("gstreamer-sharp");
result = true;
foreach (AssemblyName ref_name in asm.GetReferencedAssemblies ()) { foreach (AssemblyName ref_name in asm.GetReferencedAssemblies ()) {
try { try {
result = result | PutAssemblyTypesInCache (Assembly.Load (ref_name)); Assembly ref_asm = Assembly.Load (ref_name);
PutAssemblyTypesInCache (ref_asm);
result = result | AssemblyTypesInCache[ref_asm.GetHashCode ()];
} catch { } catch {
/* Failure to load a referenced assembly is not an error */ /* Failure to load a referenced assembly is not an error */
} }
@ -101,16 +101,11 @@ namespace Gst {
} }
} }
} }
return result;
} }
private static System.Type GstResolveType (Gst.GLib.GType gtype, string gtype_name) { private static System.Type GstResolveType (Gst.GLib.GType gtype, string gtype_name) {
// Make sure all loaded assemblies are in the TypeCache // Make sure all currently loaded assemblies are in the TypeCache
Assembly[] assemblies = (Assembly[]) AppDomain.CurrentDomain.GetAssemblies ().Clone (); System.Array.ForEach((Assembly[]) AppDomain.CurrentDomain.GetAssemblies ().Clone (), Application.PutAssemblyTypesInCache);
foreach (Assembly asm in assemblies) {
PutAssemblyTypesInCache (asm);
}
// Return the managed type // Return the managed type
if (TypeCache.ContainsKey (gtype_name)) if (TypeCache.ContainsKey (gtype_name))
@ -120,6 +115,8 @@ namespace Gst {
} }
private static void RegisterManagedTypes() { private static void RegisterManagedTypes() {
// Load types in TypeCache to speed up later invocations of GstResolveType
System.Array.ForEach((Assembly[]) AppDomain.CurrentDomain.GetAssemblies ().Clone (), Application.PutAssemblyTypesInCache);
Gst.GLib.GType.ResolveType += GstResolveType; Gst.GLib.GType.ResolveType += GstResolveType;
Gst.GLib.GType.Register (Fraction.GType, typeof (Fraction)); Gst.GLib.GType.Register (Fraction.GType, typeof (Fraction));