Avoid random failure in CountdownEvent unit test
[mono.git] / mcs / class / corlib / System.Reflection.Emit / AssemblyBuilder.cs
index 2d5b6ea4817ec68903ebc9b0fbdf8a0812195386..5f49190abb1e28f7fbb91d1bd02a26c4c71d16e3 100644 (file)
@@ -39,6 +39,7 @@ using System.Runtime.Serialization;
 using System.Globalization;
 using System.Runtime.CompilerServices;
 using System.Collections;
+using System.Collections.Generic;
 using System.Runtime.InteropServices;
 using System.Security;
 using System.Security.Cryptography;
@@ -93,11 +94,16 @@ namespace System.Reflection.Emit
        internal class GenericInstanceKey {
                Type gtd;
                internal Type[] args;
+               int hash_code;
 
                internal GenericInstanceKey (Type gtd, Type[] args)
                {
                        this.gtd = gtd;
                        this.args = args;
+
+                       hash_code = gtd.GetHashCode ();
+                       for (int i = 0; i < args.Length; ++i)
+                               hash_code ^= args [i].GetHashCode ();
                }
 
                static bool IsBoundedVector (Type type) {
@@ -193,10 +199,7 @@ namespace System.Reflection.Emit
 
                public override int GetHashCode ()
                {
-                       int hash = gtd.GetHashCode ();
-                       for (int i = 0; i < args.Length; ++i)
-                               hash ^= args [i].GetHashCode ();
-                       return hash;
+                       return hash_code;
                }
        }
 
@@ -425,12 +428,13 @@ namespace System.Reflection.Emit
 #endif
                }
 
+               // Still in use by al.exe
                internal void EmbedResourceFile (string name, string fileName)
                {
                        EmbedResourceFile (name, fileName, ResourceAttributes.Public);
                }
 
-               internal void EmbedResourceFile (string name, string fileName, ResourceAttributes attribute)
+               void EmbedResourceFile (string name, string fileName, ResourceAttributes attribute)
                {
                        if (resources != null) {
                                MonoResource[] new_r = new MonoResource [resources.Length + 1];
@@ -449,10 +453,9 @@ namespace System.Reflection.Emit
                                s.Read (resources [p].data, 0, (int)len);
                                s.Close ();
                        } catch {
-                               /* do something */
                        }
                }
-
+/*
                internal void EmbedResource (string name, byte[] blob, ResourceAttributes attribute)
                {
                        if (resources != null) {
@@ -467,7 +470,7 @@ namespace System.Reflection.Emit
                        resources [p].attrs = attribute;
                        resources [p].data = blob;
                }
-
+*/
                internal void AddTypeForwarder (Type t) {
                        if (t == null)
                                throw new ArgumentNullException ("t");
@@ -607,6 +610,8 @@ namespace System.Reflection.Emit
                        /*
                         * The format of the argument byte array is not documented
                         * so this method is impossible to implement.
+                        *
+                        * https://connect.microsoft.com/VisualStudio/feedback/details/95784/fatal-assemblybuilder-defineunmanagedresource-byte-and-modulebuilder-defineunmanagedresource-byte-bugs-renders-them-useless
                         */
 
                        throw new NotImplementedException ();
@@ -823,6 +828,19 @@ namespace System.Reflection.Emit
                                }
                        }
 
+                       if (res != null) {
+                               List<Exception> exceptions = null;
+                               foreach (var type in res) {
+                                       if (type is TypeBuilder) {
+                                               if (exceptions == null)
+                                                       exceptions = new List <Exception> ();
+                                               exceptions.Add (new TypeLoadException (string.Format ("Type '{0}' is not finished", type.FullName))); 
+                                       }
+                               }
+                               if (exceptions != null)
+                                       throw new ReflectionTypeLoadException (new Type [exceptions.Count], exceptions.ToArray ());
+                       }
+                       
                        return res == null ? Type.EmptyTypes : res;
                }
 
@@ -1198,7 +1216,7 @@ namespace System.Reflection.Emit
                        throw new NotImplementedException ();
                }
 
-#if NET_4_0
+#if NET_4_0 || MOONLIGHT
                public override Type GetType (string name, bool throwOnError, bool ignoreCase)
                {
                        if (name == null)
@@ -1238,14 +1256,52 @@ namespace System.Reflection.Emit
                        Module[] modules = GetModulesInternal ();
 
                        if (!getResourceModules) {
-                               ArrayList result = new ArrayList (modules.Length);
+                               var result = new List<Module> (modules.Length);
                                foreach (Module m in modules)
                                        if (!m.IsResource ())
                                                result.Add (m);
-                               return (Module[])result.ToArray (typeof (Module));
+                               return result.ToArray ();
                        }
                        return modules;
                }
+
+               [MonoTODO ("This always returns an empty array")]
+               public override AssemblyName[] GetReferencedAssemblies () {
+                       return GetReferencedAssemblies (this);
+               }
+
+               public override Module[] GetLoadedModules (bool getResourceModules)
+               {
+                       return GetModules (getResourceModules);
+               }
+
+               //FIXME MS has issues loading satelite assemblies from SRE
+               public override Assembly GetSatelliteAssembly (CultureInfo culture)
+               {
+                       return GetSatelliteAssembly (culture, null, true);
+               }
+
+               //FIXME MS has issues loading satelite assemblies from SRE
+               public override Assembly GetSatelliteAssembly (CultureInfo culture, Version version)
+               {
+                       return GetSatelliteAssembly (culture, version, true);
+               }
+
+               public override Module ManifestModule {
+                       get {
+                               return GetManifestModule ();
+                       }
+               }
+
+               public override bool GlobalAssemblyCache {
+                       get {
+                               return false;
+                       }
+               }
+
+               public override bool IsDynamic {
+                       get { return true; }
+               }
 #endif
        }
 }