New tests.
[mono.git] / mcs / class / corlib / System.Reflection / Assembly.cs
index f28f7a2bacf79b98c8a195bd36b78a8edd58761b..d6068ba8cd30ad7165ee1c4e7d4d7256e7d086bd 100644 (file)
@@ -5,7 +5,7 @@
 //   Paolo Molaro (lupus@ximian.com)
 //
 // (C) 2001 Ximian, Inc.  http://www.ximian.com
-// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-using System;
 using System.Security;
 using System.Security.Policy;
 using System.Security.Permissions;
 using System.Runtime.Serialization;
+using System.Reflection;
 using System.Reflection.Emit;
 using System.IO;
 using System.Globalization;
 using System.Runtime.CompilerServices;
 using System.Runtime.InteropServices;
 using System.Collections;
+using System.Collections.Generic;
 using System.Configuration.Assemblies;
 
 using Mono.Security;
 
 namespace System.Reflection {
 
+#pragma warning disable 659 // overrides Equals but not GetHashCode
+
+       [ComVisible (true)]
+       [ComDefaultInterfaceAttribute (typeof (_Assembly))]
        [Serializable]
        [ClassInterface(ClassInterfaceType.None)]
-       public class Assembly : System.Reflection.ICustomAttributeProvider,
-               System.Security.IEvidenceFactory, System.Runtime.Serialization.ISerializable {
+#if NET_2_1
+       public partial class Assembly : ICustomAttributeProvider, _Assembly {
+#elif NET_4_0
+       public abstract class Assembly : ICustomAttributeProvider, _Assembly, IEvidenceFactory, ISerializable {
+#else
+       public partial class Assembly : ICustomAttributeProvider, _Assembly, IEvidenceFactory, ISerializable {
+#endif
                internal class ResolveEventHolder {
                        public event ModuleResolveEventHandler ModuleResolve;
                }
 
                // Note: changes to fields must be reflected in _MonoReflectionAssembly struct (object-internals.h)
+#pragma warning disable 649
                private IntPtr _mono_assembly;
+#pragma warning restore 649
 
                private ResolveEventHolder resolve_event_holder;
                private Evidence _evidence;
@@ -62,8 +74,15 @@ namespace System.Reflection {
                internal PermissionSet _refuse;         // for SecurityAction.RequestRefuse
                private PermissionSet _granted;         // for the resolved assembly granted permissions
                private PermissionSet _denied;          // for the resolved assembly denied permissions
-               
-               internal Assembly () 
+               private bool fromByteArray;
+               private string assemblyName;
+
+#if NET_4_0
+               protected
+#else
+               internal
+#endif
+               Assembly () 
                {
                        resolve_event_holder = new ResolveEventHolder ();
                }
@@ -73,42 +92,50 @@ namespace System.Reflection {
                // compiler would silently insert the fields before _mono_assembly
                //
                public event ModuleResolveEventHandler ModuleResolve {
+                       [SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
                        add {
                                resolve_event_holder.ModuleResolve += value;
                        }
+                       [SecurityPermission (SecurityAction.LinkDemand, ControlAppDomain = true)]
                        remove {
                                resolve_event_holder.ModuleResolve -= value;
                        }
                }
 
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
-               private extern string get_code_base ();
-               
+               private extern string get_code_base (bool escaped);
+
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
-               private extern string get_location ();
+               private extern string get_fullname ();
 
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
-               private extern string InternalImageRuntimeVersion ();
+               private extern string get_location ();
 
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
-               private extern bool get_global_assembly_cache ();
+               private extern string InternalImageRuntimeVersion ();
 
-               public virtual string CodeBase {
-                       get {
-                               return get_code_base ();
+               // SECURITY: this should be the only caller to icall get_code_base
+               private string GetCodeBase (bool escaped)
+               {
+                       string cb = get_code_base (escaped);
+#if !NET_2_1
+                       if (SecurityManager.SecurityEnabled) {
+                               // we cannot divulge local file informations
+                               if (String.Compare ("FILE://", 0, cb, 0, 7, true, CultureInfo.InvariantCulture) == 0) {
+                                       string file = cb.Substring (7);
+                                       new FileIOPermission (FileIOPermissionAccess.PathDiscovery, file).Demand ();
+                               }
                        }
+#endif
+                       return cb;
                }
 
-               internal virtual string CopiedCodeBase {
-                       get {
-                               return get_code_base ();
-                       }
-               } 
+               public virtual string CodeBase {
+                       get { return GetCodeBase (false); }
+               }
 
                public virtual string EscapedCodeBase {
-                       get {
-                               return Uri.EscapeString (get_code_base (), false, true, true);
-                       }
+                       get { return GetCodeBase (true); }
                }
 
                public virtual string FullName {
@@ -117,7 +144,7 @@ namespace System.Reflection {
                                // FIXME: This is wrong, but it gets us going
                                // in the compiler for now
                                //
-                               return GetName (false).ToString ();
+                               return ToString ();
                        }
                }
 
@@ -125,41 +152,57 @@ namespace System.Reflection {
                        [MethodImplAttribute (MethodImplOptions.InternalCall)]
                        get;
                }
-
+#if !MOONLIGHT
                public virtual Evidence Evidence {
-                       get {
-                               // if the host (runtime) hasn't provided it's own evidence...
-                               if (_evidence == null) {
-                                       // ... we will provide our own
-                                       lock (this) {
-                                               _evidence = Evidence.GetDefaultHostEvidence (this);
-                                       }
+                       [SecurityPermission (SecurityAction.Demand, ControlEvidence = true)]
+                       get { return UnprotectedGetEvidence (); }
+               }
+
+               // note: the security runtime requires evidences but may be unable to do so...
+               internal Evidence UnprotectedGetEvidence ()
+               {
+                       // if the host (runtime) hasn't provided it's own evidence...
+                       if (_evidence == null) {
+                               // ... we will provide our own
+                               lock (this) {
+                                       _evidence = Evidence.GetDefaultHostEvidence (this);
                                }
-                               return _evidence;
                        }
+                       return _evidence;
                }
 
-               public bool GlobalAssemblyCache {
-                       get {
-                               return get_global_assembly_cache ();
-                       }
+               [MethodImplAttribute (MethodImplOptions.InternalCall)]
+               internal extern bool get_global_assembly_cache ();
+
+#endif
+               internal bool FromByteArray {
+                       set { fromByteArray = value; }
                }
-               
+
                public virtual String Location {
                        get {
-                               return get_location ();
+                               if (fromByteArray)
+                                       return String.Empty;
+
+                               string loc = get_location ();
+#if !NET_2_1
+                               if ((loc != String.Empty) && SecurityManager.SecurityEnabled) {
+                                       // we cannot divulge local file informations
+                                       new FileIOPermission (FileIOPermissionAccess.PathDiscovery, loc).Demand ();
+                               }
+#endif
+                               return loc;
                        }
                }
 
-#if NET_1_1
                [ComVisible (false)]
                public virtual string ImageRuntimeVersion {
                        get {
                                return InternalImageRuntimeVersion ();
                        }
                }
-#endif
 
+               [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)]
                public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
                {
                        if (info == null)
@@ -197,18 +240,28 @@ namespace System.Reflection {
                        if (names == null)
                                return new FileStream [0];
 
-                       FileStream[] res = new FileStream [names.Length];
-                       for (int i = 0; i < names.Length; ++i)
-                               res [i] = new FileStream (names [i], FileMode.Open, FileAccess.Read);
+                       string location = Location;
+
+                       FileStream[] res;
+                       if (location != String.Empty) {
+                               res = new FileStream [names.Length + 1];
+                               res [0] = new FileStream (location, FileMode.Open, FileAccess.Read);
+                               for (int i = 0; i < names.Length; ++i)
+                                       res [i + 1] = new FileStream (names [i], FileMode.Open, FileAccess.Read);
+                       } else {
+                               res = new FileStream [names.Length];
+                               for (int i = 0; i < names.Length; ++i)
+                                       res [i] = new FileStream (names [i], FileMode.Open, FileAccess.Read);
+                       }
                        return res;
                }
 
                public virtual FileStream GetFile (String name)
                {
                        if (name == null)
-                               throw new ArgumentNullException ("name");
+                               throw new ArgumentNullException (null, "Name cannot be null.");
                        if (name.Length == 0)
-                               throw new ArgumentException ("name");
+                               throw new ArgumentException ("Empty name is not valid");
 
                        string filename = (string)GetFilesInternal (name, true);
                        if (filename != null)
@@ -218,14 +271,15 @@ namespace System.Reflection {
                }
 
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
-               private extern IntPtr GetManifestResourceInternal (String name, out int size, out Module module);
+               internal extern IntPtr GetManifestResourceInternal (String name, out int size, out Module module);
 
                public virtual Stream GetManifestResourceStream (String name)
                {
                        if (name == null)
                                throw new ArgumentNullException ("name");
-                       if (name == "")
-                               throw new ArgumentException ("name cannot have zero length.");
+                       if (name.Length == 0)
+                               throw new ArgumentException ("String cannot have zero length.",
+                                       "name");
 
                        ManifestResourceInfo info = GetManifestResourceInfo (name);
                        if (info == null)
@@ -234,8 +288,17 @@ namespace System.Reflection {
                        if (info.ReferencedAssembly != null)
                                return info.ReferencedAssembly.GetManifestResourceStream (name);
                        if ((info.FileName != null) && (info.ResourceLocation == 0)) {
-                               string filename = Path.Combine (Path.GetDirectoryName (Location),
-                                                                                       info.FileName);
+                               if (fromByteArray)
+                                       throw new FileNotFoundException (info.FileName);
+
+                               string location = Path.GetDirectoryName (Location);
+                               string filename = Path.Combine (location, info.FileName);
+#if MOONLIGHT
+                               // we don't control the content of 'info.FileName' so we want to make sure we keep to ourselves
+                               filename = Path.GetFullPath (filename);
+                               if (!filename.StartsWith (location))
+                                       throw new SecurityException ("non-rooted access to manifest resource");
+#endif
                                return new FileStream (filename, FileMode.Open, FileAccess.Read);
                        }
 
@@ -245,7 +308,10 @@ namespace System.Reflection {
                        if (data == (IntPtr) 0)
                                return null;
                        else {
-                               IntPtrStream stream = new IntPtrStream (data, size);
+                               UnmanagedMemoryStream stream;
+                               unsafe {
+                                       stream = new UnmanagedMemoryStream ((byte*) data, size);
+                               }
                                /* 
                                 * The returned pointer points inside metadata, so
                                 * we have to increase the refcount of the module, and decrease
@@ -259,19 +325,22 @@ namespace System.Reflection {
                public virtual Stream GetManifestResourceStream (Type type, String name)
                {
                        string ns;
-                       if (type != null)
+                       if (type != null) {
                                ns = type.Namespace;
-                       else 
+                       } else {
+                               if (name == null)
+                                       throw new ArgumentNullException ("type");
                                ns = null;
+                       }
 
-                       if ((ns == null) || (ns == ""))
+                       if (ns == null || ns.Length == 0)
                                return GetManifestResourceStream (name);
                        else
                                return GetManifestResourceStream (ns + "." + name);
                }
 
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
-               private extern Type[] GetTypes (bool exportedOnly);
+               internal virtual extern Type[] GetTypes (bool exportedOnly);
                
                public virtual Type[] GetTypes ()
                {
@@ -295,26 +364,20 @@ namespace System.Reflection {
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
                internal extern Type InternalGetType (Module module, String name, Boolean throwOnError, Boolean ignoreCase);
 
-               public Type GetType (string name, bool throwOnError, bool ignoreCase)
-               {
-                       if (name == null)
-                               throw new ArgumentNullException (name);
-
-                       return InternalGetType (null, name, throwOnError, ignoreCase);
-               }
-
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
                internal extern static void InternalGetAssemblyName (string assemblyFile, AssemblyName aname);
-               
+
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
                static extern void FillName (Assembly ass, AssemblyName aname);
 
-               [MonoTODO ("true == not supported")]
+               [MonoTODO ("copiedName == true is not supported")]
                public virtual AssemblyName GetName (Boolean copiedName)
                {
-                       AssemblyName aname = new AssemblyName ();
-                       FillName (this, aname);
-                       return aname;
+                       // CodeBase, which is restricted, will be copied into the AssemblyName object so...
+                       if (SecurityManager.SecurityEnabled) {
+                               GetCodeBase (true); // this will ensure the Demand is made
+                       }
+                       return UnprotectedGetName ();
                }
 
                public virtual AssemblyName GetName ()
@@ -322,9 +385,23 @@ namespace System.Reflection {
                        return GetName (false);
                }
 
-               public override String ToString ()
+               // the security runtime requires access to the assemblyname (e.g. to get the strongname)
+               internal virtual AssemblyName UnprotectedGetName ()
                {
-                       return GetName ().ToString ();
+                       AssemblyName aname = new AssemblyName ();
+                       FillName (this, aname);
+                       return aname;
+               }
+
+               public override string ToString ()
+               {
+                       // note: ToString work without requiring CodeBase (so no checks are needed)
+
+                       if (assemblyName != null)
+                               return assemblyName;
+
+                       assemblyName = get_fullname ();
+                       return assemblyName;
                }
 
                public static String CreateQualifiedName (String assemblyName, String typeName) 
@@ -343,12 +420,12 @@ namespace System.Reflection {
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
                public static extern Assembly GetEntryAssembly();
 
-               public Assembly GetSatelliteAssembly (CultureInfo culture)
+               internal Assembly GetSatelliteAssemblyNoThrow (CultureInfo culture, Version version)
                {
-                       return GetSatelliteAssembly (culture, null);
+                       return GetSatelliteAssembly (culture, version, false);
                }
 
-               public Assembly GetSatelliteAssembly (CultureInfo culture, Version version)
+               internal Assembly GetSatelliteAssembly (CultureInfo culture, Version version, bool throwOnError)
                {
                        if (culture == null)
                                throw new ArgumentException ("culture");
@@ -359,7 +436,33 @@ namespace System.Reflection {
 
                        aname.CultureInfo = culture;
                        aname.Name = aname.Name + ".resources";
-                       return Load (aname);
+                       Assembly assembly;
+
+                       try {
+                               assembly = AppDomain.CurrentDomain.LoadSatellite (aname, false);
+                               if (assembly != null)
+                                       return assembly;
+                       } catch (FileNotFoundException) {
+                               assembly = null;
+                               // ignore
+                       }
+
+                       // Try the assembly directory
+                       string location = Path.GetDirectoryName (Location);
+                       string fullName = Path.Combine (location, Path.Combine (culture.Name, aname.Name + ".dll"));
+#if MOONLIGHT
+                       // it's unlikely that culture.Name or aname.Name could contain stuff like ".." but...
+                       fullName = Path.GetFullPath (fullName);
+                       if (!fullName.StartsWith (location)) {
+                               if (throwOnError)
+                                       throw new SecurityException ("non-rooted access to satellite assembly");
+                               return null;
+                       }
+#endif
+                       if (!throwOnError && !File.Exists (fullName))
+                               return null;
+
+                       return LoadFrom (fullName);
                }
                
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
@@ -370,19 +473,26 @@ namespace System.Reflection {
                        return LoadFrom (assemblyFile, false);
                }
 
+#if NET_4_0
+               [Obsolete]
+#endif
                public static Assembly LoadFrom (String assemblyFile, Evidence securityEvidence)
                {
                        Assembly a = LoadFrom (assemblyFile, false);
+#if !NET_2_1
                        if ((a != null) && (securityEvidence != null)) {
                                // merge evidence (i.e. replace defaults with provided evidences)
                                a.Evidence.Merge (securityEvidence);
                        }
+#endif
                        return a;
                }
 
-#if NET_1_1
-
-               [MonoTODO]
+#if NET_4_0
+               [Obsolete]
+#endif
+               [MonoTODO("This overload is not currently implemented")]
+               // FIXME: What are we missing?
                public static Assembly LoadFrom (String assemblyFile, Evidence securityEvidence, byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm)
                {
                        if (assemblyFile == null)
@@ -392,8 +502,11 @@ namespace System.Reflection {
                        throw new NotImplementedException ();
                }
 
-               [MonoTODO]
-               public static Assembly LoadFile (String path, Evidence securityEvidence) {
+#if NET_4_0
+               [Obsolete]
+#endif
+               public static Assembly LoadFile (String path, Evidence securityEvidence)
+               {
                        if (path == null)
                                throw new ArgumentNullException ("path");
                        if (path == String.Empty)
@@ -402,16 +515,19 @@ namespace System.Reflection {
                        return LoadFrom (path, securityEvidence);
                }
 
-               public static Assembly LoadFile (String path) {
+               public static Assembly LoadFile (String path)
+               {
                        return LoadFile (path, null);
                }
-#endif
 
                public static Assembly Load (String assemblyString)
                {
                        return AppDomain.CurrentDomain.Load (assemblyString);
                }
-               
+
+#if NET_4_0
+               [Obsolete]
+#endif         
                public static Assembly Load (String assemblyString, Evidence assemblySecurity)
                {
                        return AppDomain.CurrentDomain.Load (assemblyString, assemblySecurity);
@@ -422,6 +538,9 @@ namespace System.Reflection {
                        return AppDomain.CurrentDomain.Load (assemblyRef);
                }
 
+#if NET_4_0
+               [Obsolete]
+#endif
                public static Assembly Load (AssemblyName assemblyRef, Evidence assemblySecurity)
                {
                        return AppDomain.CurrentDomain.Load (assemblyRef, assemblySecurity);
@@ -437,21 +556,23 @@ namespace System.Reflection {
                        return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore);
                }
 
+#if NET_4_0
+               [Obsolete]
+#endif
                public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore,
                                             Evidence securityEvidence)
                {
                        return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore, securityEvidence);
                }
 
-#if NET_2_0
                public static Assembly ReflectionOnlyLoad (byte[] rawAssembly)
                {
                        return AppDomain.CurrentDomain.Load (rawAssembly, null, null, true);
                }
 
-               public static Assembly ReflectionOnlyLoad (string assemblyName
+               public static Assembly ReflectionOnlyLoad (string assemblyString
                {
-                       return AppDomain.CurrentDomain.Load (assemblyName, null, true);
+                       return AppDomain.CurrentDomain.Load (assemblyString, null, true);
                }
 
                public static Assembly ReflectionOnlyLoadFrom (string assemblyFile) 
@@ -461,24 +582,27 @@ namespace System.Reflection {
                        
                        return LoadFrom (assemblyFile, true);
                }
-#endif
 
-#if NET_2_0
-               [Obsolete ("")]
+#if NET_4_0
+               [Obsolete]
 #endif
                public static Assembly LoadWithPartialName (string partialName)
                {
                        return LoadWithPartialName (partialName, null);
                }
 
-               [MonoTODO]
+               [MonoTODO ("Not implemented")]
                public Module LoadModule (string moduleName, byte [] rawModule)
                {
                        throw new NotImplementedException ();
                }
 
-               [MonoTODO]
-               public Module LoadModule (string moduleName, byte [] rawModule, byte [] rawSymbolStore)
+               [MonoTODO ("Not implemented")]
+               public
+#if NET_4_0
+               virtual
+#endif
+               Module LoadModule (string moduleName, byte [] rawModule, byte [] rawSymbolStore)
                {
                        throw new NotImplementedException ();
                }
@@ -486,8 +610,8 @@ namespace System.Reflection {
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
                private static extern Assembly load_with_partial_name (string name, Evidence e);
 
-#if NET_2_0
-               [Obsolete ("")]
+#if NET_4_0
+               [Obsolete]
 #endif
                public static Assembly LoadWithPartialName (string partialName, Evidence securityEvidence)
                {
@@ -500,15 +624,9 @@ namespace System.Reflection {
                 * ie System/// will throw an exception. However ////System will not as that is canocolized
                 * out of the name.
                 */
-#if NET_2_0
-               [Obsolete ("")]
-               [ComVisible (false)]
-               [MonoTODO]
-               public
-#else
-               internal
-#endif
-               static Assembly LoadWithPartialName (string partialName, Evidence securityEvidence, bool oldBehavior)
+
+               // FIXME: LoadWithPartialName must look cache (no CAS) or read from disk (CAS)
+               internal static Assembly LoadWithPartialName (string partialName, Evidence securityEvidence, bool oldBehavior)
                {
                        if (!oldBehavior)
                                throw new NotImplementedException ();
@@ -516,10 +634,6 @@ namespace System.Reflection {
                        if (partialName == null)
                                throw new NullReferenceException ();
 
-                       int ci = partialName.IndexOf (',');
-                       if (ci > 0)
-                               partialName = partialName.Substring (0, ci);
-
                        return load_with_partial_name (partialName, securityEvidence);
                }
 
@@ -541,7 +655,11 @@ namespace System.Reflection {
                        }
                }
 
-               public Object CreateInstance (String typeName, Boolean ignoreCase,
+               public
+#if NET_4_0
+               virtual
+#endif
+               Object CreateInstance (String typeName, Boolean ignoreCase,
                                              BindingFlags bindingAttr, Binder binder,
                                              Object[] args, CultureInfo culture,
                                              Object[] activationAttributes)
@@ -562,50 +680,14 @@ namespace System.Reflection {
                        return GetLoadedModules (false);
                }
 
-               [MonoTODO]
-               public Module[] GetLoadedModules (bool getResourceModules)
-               {
-                       // Currently, the two sets of modules are equal
-                       return GetModules (getResourceModules);
-               }
-
                public Module[] GetModules ()
                {
                        return GetModules (false);
                }
 
-               public Module GetModule (String name)
-               {
-                       if (name == null)
-                               throw new ArgumentNullException ("name");
-                       if (name == "")
-                               throw new ArgumentException ("Name can't be empty");
-
-                       Module[] modules = GetModules (true);
-                       foreach (Module module in modules) {
-                               if (module.ScopeName == name)
-                                       return module;
-                       }
-
-                       return null;
-               }
-
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
-               internal extern Module[] GetModulesInternal ();
+               internal virtual extern Module[] GetModulesInternal ();
 
-               public Module[] GetModules (bool getResourceModules) {
-                       Module[] modules = GetModulesInternal ();
-
-                       if (!getResourceModules) {
-                               ArrayList result = new ArrayList (modules.Length);
-                               foreach (Module m in modules)
-                                       if (!m.IsResource ())
-                                               result.Add (m);
-                               return (Module[])result.ToArray (typeof (Module));
-                       }
-                       else
-                               return modules;
-               }
 
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
                internal extern string[] GetNamespaces ();
@@ -620,7 +702,7 @@ namespace System.Reflection {
                public extern static Assembly GetCallingAssembly ();
 
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
-               public extern AssemblyName[] GetReferencedAssemblies ();
+               internal static extern AssemblyName[] GetReferencedAssemblies (Assembly module);
 
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
                private extern bool GetManifestResourceInfoInternal (String name, ManifestResourceInfo info);
@@ -629,7 +711,7 @@ namespace System.Reflection {
                {
                        if (resourceName == null)
                                throw new ArgumentNullException ("resourceName");
-                       if (resourceName == "")
+                       if (resourceName.Length == 0)
                                throw new ArgumentException ("String cannot have zero length.");
                        ManifestResourceInfo result = new ManifestResourceInfo ();
                        bool found = GetManifestResourceInfoInternal (resourceName, result);
@@ -640,8 +722,9 @@ namespace System.Reflection {
                }
 
                private class ResourceCloseHandler {
-
+#pragma warning disable 169, 414
                        Module module;
+#pragma warning restore 169, 414                       
 
                        public ResourceCloseHandler (Module module) {
                                this.module = module;
@@ -658,77 +741,51 @@ namespace System.Reflection {
                //
 
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
-               internal static extern MethodBase MonoDebugger_GetMethod (Assembly assembly, int token);
-
-               [MethodImplAttribute (MethodImplOptions.InternalCall)]
-               internal static extern int MonoDebugger_GetMethodToken (Assembly assembly, MethodBase method);
-
-               [MethodImplAttribute (MethodImplOptions.InternalCall)]
-               internal static extern Type MonoDebugger_GetLocalTypeFromSignature (Assembly assembly, byte[] signature);
-
-               [MethodImplAttribute (MethodImplOptions.InternalCall)]
-               internal static extern Type MonoDebugger_GetType (Assembly assembly, int token);
-
-               [MethodImplAttribute (MethodImplOptions.InternalCall)]
-               internal static extern string MonoDebugger_CheckRuntimeVersion (string filename);
-
-               [MethodImplAttribute (MethodImplOptions.InternalCall)]
-               internal static extern string MonoDebugger_GetMethodIndex (MethodBase method);
-
-               [MethodImplAttribute (MethodImplOptions.InternalCall)]
-               internal static extern Type MonoDebugger_MakeArrayType (Type type, int rank);
+               internal static extern int MonoDebugger_GetMethodToken (MethodBase method);
 
-               [MethodImplAttribute (MethodImplOptions.InternalCall)]
-               internal static extern int MonoDebugger_GetTypeToken (Type type);
-
-#if NET_2_0
-               [MonoTODO]
+               [MonoTODO ("Currently it always returns zero")]
                [ComVisible (false)]
-               public long HostContext {
+               public
+#if NET_4_0
+               virtual
+#endif
+               long HostContext {
                        get { return 0; }
                }
 
-               [ComVisible (false)]
-               public ImageFileMachine ImageFileMachine {
-                       get {
-                               ImageFileMachine machine;
-                               PortableExecutableKind kind;
-                               ModuleHandle handle = ManifestModule.ModuleHandle;
-                               handle.GetPEKind (out kind, out machine);
-                               return machine;
-                       }
-               }
 
-               [ComVisible (false)]
-               public extern Module ManifestModule {
-                       [MethodImplAttribute (MethodImplOptions.InternalCall)]
-                       get;
+               internal virtual Module GetManifestModule () {
+                       return GetManifestModuleInternal ();
                }
 
+               [MethodImplAttribute (MethodImplOptions.InternalCall)]
+               internal extern Module GetManifestModuleInternal ();
+
                [ComVisible (false)]
-               public extern int MetadataToken {
+               public virtual extern bool ReflectionOnly {
                        [MethodImplAttribute (MethodImplOptions.InternalCall)]
                        get;
                }
 
-               [ComVisible (false)]
-               public PortableExecutableKind PortableExecutableKind {
-                       get {
-                               ImageFileMachine machine;
-                               PortableExecutableKind kind;
-                               ModuleHandle handle = ManifestModule.ModuleHandle;
-                               handle.GetPEKind (out kind, out machine);
-                               return kind;
-                       }
-               }
+               public override bool Equals (object o)
+               {
+                       if (((object) this) == o)
+                               return true;
 
-               [ComVisible (false)]
-               public virtual extern bool ReflectionOnly {
-                       [MethodImplAttribute (MethodImplOptions.InternalCall)]
-                       get;
+                       if (o == null)
+                               return false;
+                       
+                       Assembly other = (Assembly) o;
+                       return other._mono_assembly == _mono_assembly;
+               }
+               
+#if NET_4_0
+               public virtual IList<CustomAttributeData> GetCustomAttributesData () {
+                       return CustomAttributeData.GetCustomAttributes (this);
                }
 #endif
 
+#if !MOONLIGHT
                // Code Access Security
 
                internal void Resolve () 
@@ -738,28 +795,22 @@ namespace System.Reflection {
                                // Demand it's too late to evaluate the Minimum permission set as a 
                                // condition to load the assembly into the AppDomain
                                LoadAssemblyPermissions ();
-                               _granted = SecurityManager.ResolvePolicy (Evidence, _minimum, _optional,
-                                       _refuse, out _denied);
+                               Evidence e = new Evidence (UnprotectedGetEvidence ()); // we need a copy to add PRE
+                               e.AddHost (new PermissionRequestEvidence (_minimum, _optional, _refuse));
+                               _granted = SecurityManager.ResolvePolicy (e,
+                                       _minimum, _optional, _refuse, out _denied);
                        }
-#if false
-                       Console.WriteLine ("*** ASSEMBLY RESOLVE INPUT ***");
-                       if (_minimum != null)
-                               Console.WriteLine ("Minimum: {0}", _minimum);
-                       if (_optional != null)
-                               Console.WriteLine ("Optional: {0}", _optional);
-                       if (_refuse != null)
-                               Console.WriteLine ("Refuse: {0}", _refuse);
-                       Console.WriteLine ("*** ASSEMBLY RESOLVE RESULTS ***");
-                       Console.WriteLine ("Granted: {0}", _granted);
-                       if (_denied != null)
-                               Console.WriteLine ("Denied: {0}", _denied);
-                       Console.WriteLine ("*** ASSEMBLY RESOLVE END ***");
-#endif
                }
 
                internal PermissionSet GrantedPermissionSet {
                        get {
                                if (_granted == null) {
+                                       if (SecurityManager.ResolvingPolicyLevel != null) {
+                                               if (SecurityManager.ResolvingPolicyLevel.IsFullTrustAssembly (this))
+                                                       return DefaultPolicies.FullTrust;
+                                               else
+                                                       return null; // we can't resolve during resolution
+                                       }
                                        Resolve ();
                                }
                                return _granted;
@@ -770,6 +821,12 @@ namespace System.Reflection {
                        get {
                                // yes we look for granted, as denied may be null
                                if (_granted == null) {
+                                       if (SecurityManager.ResolvingPolicyLevel != null) {
+                                               if (SecurityManager.ResolvingPolicyLevel.IsFullTrustAssembly (this))
+                                                       return null;
+                                               else
+                                                       return DefaultPolicies.FullTrust; // deny unrestricted
+                                       }
                                        Resolve ();
                                }
                                return _denied;
@@ -809,5 +866,86 @@ namespace System.Reflection {
                                }
                        }
                }
+#endif
+
+#if NET_4_0
+               static Exception CreateNIE ()
+               {
+                       return new NotImplementedException ("Derived classes must implement it");
+               }
+
+               public virtual Type GetType (string name, bool throwOnError, bool ignoreCase)
+               {
+                       throw CreateNIE ();
+               }
+
+               public virtual Module GetModule (String name)
+               {
+                       throw CreateNIE ();
+               }
+
+               public virtual AssemblyName[] GetReferencedAssemblies ()
+               {
+                       throw CreateNIE ();
+               }
+
+               public virtual Module[] GetModules (bool getResourceModules)
+               {
+                       throw CreateNIE ();
+               }
+
+               [MonoTODO ("Always returns the same as GetModules")]
+               public virtual Module[] GetLoadedModules (bool getResourceModules)
+               {
+                       throw CreateNIE ();
+               }
+
+               public virtual Assembly GetSatelliteAssembly (CultureInfo culture)
+               {
+                       throw CreateNIE ();
+               }
+
+               public virtual Assembly GetSatelliteAssembly (CultureInfo culture, Version version)
+               {
+                       throw CreateNIE ();
+               }
+
+               public virtual Module ManifestModule {
+                       get { throw CreateNIE (); }
+               }
+
+               public virtual bool GlobalAssemblyCache {
+                       get { throw CreateNIE (); }
+               }
+
+               public virtual bool IsDynamic {
+                       get { return false; }
+               }
+
+               public override int GetHashCode ()
+               {
+                       return base.GetHashCode ();
+               }
+
+               public static bool operator == (Assembly left, Assembly right)
+               {
+                       if ((object)left == (object)right)
+                               return true;
+                       if ((object)left == null ^ (object)right == null)
+                               return false;
+                       return left.Equals (right);
+               }
+
+               public static bool operator != (Assembly left, Assembly right)
+               {
+                       if ((object)left == (object)right)
+                               return false;
+                       if ((object)left == null ^ (object)right == null)
+                               return true;
+                       return !left.Equals (right);
+               }
+#endif
        }
 }
+
+#pragma warning restore 659