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 {
-#if NET_2_0
+#pragma warning disable 659 // overrides Equals but not GetHashCode
+
[ComVisible (true)]
[ComDefaultInterfaceAttribute (typeof (_Assembly))]
-#endif
[Serializable]
[ClassInterface(ClassInterfaceType.None)]
- public class Assembly : System.Reflection.ICustomAttributeProvider, _Assembly,
- 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;
private bool fromByteArray;
private string assemblyName;
- internal Assembly ()
+#if NET_4_0
+ protected
+#else
+ internal
+#endif
+ Assembly ()
{
resolve_event_holder = new ResolveEventHolder ();
}
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 ();
// 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) {
new FileIOPermission (FileIOPermissionAccess.PathDiscovery, file).Demand ();
}
}
+#endif
return cb;
}
[MethodImplAttribute (MethodImplOptions.InternalCall)]
get;
}
-
+#if !MOONLIGHT
public virtual Evidence Evidence {
[SecurityPermission (SecurityAction.Demand, ControlEvidence = true)]
get { return UnprotectedGetEvidence (); }
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 = true; }
+ set { fromByteArray = value; }
}
public virtual String Location {
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)
UnitySerializationHolder.GetAssemblyData (this, info, context);
}
-#if ONLY_1_1
- public new Type GetType ()
- {
- return base.GetType ();
- }
-#endif
-
public virtual bool IsDefined (Type attributeType, bool inherit)
{
return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
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)
{
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)
return info.ReferencedAssembly.GetManifestResourceStream (name);
if ((info.FileName != null) && (info.ResourceLocation == 0)) {
if (fromByteArray)
-#if NET_2_0
throw new FileNotFoundException (info.FileName);
-#else
- return null;
-#endif
- string filename = Path.Combine (Path.GetDirectoryName (Location),
- 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);
}
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
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 ()
{
[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)
{
// CodeBase, which is restricted, will be copied into the AssemblyName object so...
if (assemblyName != null)
return assemblyName;
- AssemblyName aname = new AssemblyName ();
- FillName (this, aname);
- assemblyName = aname.ToString ();
-
+ assemblyName = get_fullname ();
return assemblyName;
}
[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");
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)]
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)
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)
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);
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);
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)
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 ();
}
[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)
{
}
}
- 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)
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 ();
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);
{
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);
}
private class ResourceCloseHandler {
-
+#pragma warning disable 169, 414
Module module;
+#pragma warning restore 169, 414
public ResourceCloseHandler (Module module) {
this.module = module;
[MethodImplAttribute (MethodImplOptions.InternalCall)]
internal static extern int MonoDebugger_GetMethodToken (MethodBase method);
-#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 extern Module ManifestModule {
- [MethodImplAttribute (MethodImplOptions.InternalCall)]
- get;
+
+ internal virtual Module GetManifestModule () {
+ return GetManifestModuleInternal ();
}
+ [MethodImplAttribute (MethodImplOptions.InternalCall)]
+ internal extern Module GetManifestModuleInternal ();
+
[ComVisible (false)]
public virtual extern bool ReflectionOnly {
[MethodImplAttribute (MethodImplOptions.InternalCall)]
get;
}
+
+ public override bool Equals (object o)
+ {
+ if (((object) this) == o)
+ return true;
+
+ 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 ()
}
}
}
+#endif
+
+#if NET_4_0
+ static Exception CreateNIE ()
+ {
+ return new NotSupportedException ("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 ();
+ }
+
+ 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 (); }
+ }
+#endif
}
}
+
+#pragma warning restore 659