| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852 |
- //
- // System.Reflection/Assembly.cs
- //
- // Author:
- // Paolo Molaro ([email protected])
- //
- // (C) 2001 Ximian, Inc. http://www.ximian.com
- // Copyright (C) 2004 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
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // 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.Emit;
- using System.IO;
- using System.Globalization;
- using System.Runtime.CompilerServices;
- using System.Runtime.InteropServices;
- using System.Collections;
- using System.Configuration.Assemblies;
- using Mono.Security;
- namespace System.Reflection {
- #if NET_2_0
- [ComVisible (true)]
- #endif
- [Serializable]
- [ClassInterface(ClassInterfaceType.None)]
- public class Assembly : System.Reflection.ICustomAttributeProvider,
- System.Security.IEvidenceFactory, System.Runtime.Serialization.ISerializable {
- internal class ResolveEventHolder {
- public event ModuleResolveEventHandler ModuleResolve;
- }
- // Note: changes to fields must be reflected in _MonoReflectionAssembly struct (object-internals.h)
- private IntPtr _mono_assembly;
- private ResolveEventHolder resolve_event_holder;
- private Evidence _evidence;
- internal PermissionSet _minimum; // for SecurityAction.RequestMinimum
- internal PermissionSet _optional; // for SecurityAction.RequestOptional
- internal PermissionSet _refuse; // for SecurityAction.RequestRefuse
- private PermissionSet _granted; // for the resolved assembly granted permissions
- private PermissionSet _denied; // for the resolved assembly denied permissions
- private bool fromByteArray;
-
- internal Assembly ()
- {
- resolve_event_holder = new ResolveEventHolder ();
- }
- //
- // We can't store the event directly in this class, since the
- // 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 ();
- [MethodImplAttribute (MethodImplOptions.InternalCall)]
- private extern string get_location ();
- [MethodImplAttribute (MethodImplOptions.InternalCall)]
- private extern string InternalImageRuntimeVersion ();
- [MethodImplAttribute (MethodImplOptions.InternalCall)]
- private extern bool get_global_assembly_cache ();
- // SECURITY: this should be the only caller to icall get_code_base
- private string GetCodeBase ()
- {
- string cb = get_code_base ();
- 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 ();
- }
- }
- return cb;
- }
- public virtual string CodeBase {
- get { return GetCodeBase (); }
- }
- public virtual string EscapedCodeBase {
- get { return Uri.EscapeString (GetCodeBase (), false, true, true); }
- }
- public virtual string FullName {
- get {
- //
- // FIXME: This is wrong, but it gets us going
- // in the compiler for now
- //
- return ToString ();
- }
- }
- public virtual extern MethodInfo EntryPoint {
- [MethodImplAttribute (MethodImplOptions.InternalCall)]
- get;
- }
- public virtual Evidence Evidence {
- [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;
- }
- public bool GlobalAssemblyCache {
- get {
- return get_global_assembly_cache ();
- }
- }
- internal bool FromByteArray {
- set { fromByteArray = true; }
- }
- public virtual String Location {
- get {
- if (fromByteArray)
- return String.Empty;
- string loc = get_location ();
- if ((loc != String.Empty) && SecurityManager.SecurityEnabled) {
- // we cannot divulge local file informations
- new FileIOPermission (FileIOPermissionAccess.PathDiscovery, loc).Demand ();
- }
- 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)
- throw new ArgumentNullException ("info");
- UnitySerializationHolder.GetAssemblyData (this, info, context);
- }
- public virtual bool IsDefined (Type attributeType, bool inherit)
- {
- return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
- }
- public virtual object [] GetCustomAttributes (bool inherit)
- {
- return MonoCustomAttrs.GetCustomAttributes (this, inherit);
- }
- public virtual object [] GetCustomAttributes (Type attributeType, bool inherit)
- {
- return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
- }
- [MethodImplAttribute (MethodImplOptions.InternalCall)]
- private extern object GetFilesInternal (String name, bool getResourceModules);
- public virtual FileStream[] GetFiles ()
- {
- return GetFiles (false);
- }
- public virtual FileStream [] GetFiles (bool getResourceModules)
- {
- string[] names = (string[]) GetFilesInternal (null, getResourceModules);
- 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);
- return res;
- }
- public virtual FileStream GetFile (String name)
- {
- if (name == null)
- throw new ArgumentNullException ("name");
- if (name.Length == 0)
- throw new ArgumentException ("name");
- string filename = (string)GetFilesInternal (name, true);
- if (filename != null)
- return new FileStream (filename, FileMode.Open, FileAccess.Read);
- else
- return null;
- }
- [MethodImplAttribute (MethodImplOptions.InternalCall)]
- private 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.");
- ManifestResourceInfo info = GetManifestResourceInfo (name);
- if (info == null)
- return null;
- 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);
- return new FileStream (filename, FileMode.Open, FileAccess.Read);
- }
- int size;
- Module module;
- IntPtr data = GetManifestResourceInternal (name, out size, out module);
- if (data == (IntPtr) 0)
- return null;
- else {
- IntPtrStream stream = new IntPtrStream (data, size);
- /*
- * The returned pointer points inside metadata, so
- * we have to increase the refcount of the module, and decrease
- * it when the stream is finalized.
- */
- stream.Closed += new EventHandler (new ResourceCloseHandler (module).OnClose);
- return stream;
- }
- }
- public virtual Stream GetManifestResourceStream (Type type, String name)
- {
- string ns;
- if (type != null)
- ns = type.Namespace;
- else
- ns = null;
- if ((ns == null) || (ns == ""))
- return GetManifestResourceStream (name);
- else
- return GetManifestResourceStream (ns + "." + name);
- }
- [MethodImplAttribute (MethodImplOptions.InternalCall)]
- private extern Type[] GetTypes (bool exportedOnly);
-
- public virtual Type[] GetTypes ()
- {
- return GetTypes (false);
- }
- public virtual Type[] GetExportedTypes ()
- {
- return GetTypes (true);
- }
- public virtual Type GetType (String name, Boolean throwOnError)
- {
- return GetType (name, throwOnError, false);
- }
- public virtual Type GetType (String name) {
- return GetType (name, false, false);
- }
- [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")]
- public virtual AssemblyName GetName (Boolean copiedName)
- {
- // CodeBase, which is restricted, will be copied into the AssemblyName object so...
- if (SecurityManager.SecurityEnabled) {
- GetCodeBase (); // this will ensure the Demand is made
- }
- return UnprotectedGetName ();
- }
- public virtual AssemblyName GetName ()
- {
- return GetName (false);
- }
- // the security runtime requires access to the assemblyname (e.g. to get the strongname)
- internal AssemblyName UnprotectedGetName ()
- {
- AssemblyName aname = new AssemblyName ();
- FillName (this, aname);
- return aname;
- }
- public override string ToString ()
- {
- // note: ToString work without requiring CodeBase (so no checks are needed)
- AssemblyName aname = new AssemblyName ();
- FillName (this, aname);
- return aname.ToString ();
- }
- public static String CreateQualifiedName (String assemblyName, String typeName)
- {
- return typeName + ", " + assemblyName;
- }
- public static Assembly GetAssembly (Type type)
- {
- if (type != null)
- return type.Assembly;
- throw new ArgumentNullException ("type");
- }
- [MethodImplAttribute (MethodImplOptions.InternalCall)]
- public static extern Assembly GetEntryAssembly();
- public Assembly GetSatelliteAssembly (CultureInfo culture)
- {
- return GetSatelliteAssembly (culture, null);
- }
- public Assembly GetSatelliteAssembly (CultureInfo culture, Version version)
- {
- if (culture == null)
- throw new ArgumentException ("culture");
- AssemblyName aname = GetName (true);
- if (version != null)
- aname.Version = version;
- aname.CultureInfo = culture;
- aname.Name = aname.Name + ".resources";
- return Load (aname);
- }
-
- [MethodImplAttribute (MethodImplOptions.InternalCall)]
- private extern static Assembly LoadFrom (String assemblyFile, bool refonly);
- public static Assembly LoadFrom (String assemblyFile)
- {
- return LoadFrom (assemblyFile, false);
- }
- public static Assembly LoadFrom (String assemblyFile, Evidence securityEvidence)
- {
- Assembly a = LoadFrom (assemblyFile, false);
- if ((a != null) && (securityEvidence != null)) {
- // merge evidence (i.e. replace defaults with provided evidences)
- a.Evidence.Merge (securityEvidence);
- }
- return a;
- }
- #if NET_1_1
- [MonoTODO]
- public static Assembly LoadFrom (String assemblyFile, Evidence securityEvidence, byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm)
- {
- if (assemblyFile == null)
- throw new ArgumentNullException ("assemblyFile");
- if (assemblyFile == String.Empty)
- throw new ArgumentException ("Name can't be the empty string", "assemblyFile");
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public static Assembly LoadFile (String path, Evidence securityEvidence) {
- if (path == null)
- throw new ArgumentNullException ("path");
- if (path == String.Empty)
- throw new ArgumentException ("Path can't be empty", "path");
- // FIXME: Make this do the right thing
- return LoadFrom (path, securityEvidence);
- }
- public static Assembly LoadFile (String path) {
- return LoadFile (path, null);
- }
- #endif
- public static Assembly Load (String assemblyString)
- {
- return AppDomain.CurrentDomain.Load (assemblyString);
- }
-
- public static Assembly Load (String assemblyString, Evidence assemblySecurity)
- {
- return AppDomain.CurrentDomain.Load (assemblyString, assemblySecurity);
- }
- public static Assembly Load (AssemblyName assemblyRef)
- {
- return AppDomain.CurrentDomain.Load (assemblyRef);
- }
- public static Assembly Load (AssemblyName assemblyRef, Evidence assemblySecurity)
- {
- return AppDomain.CurrentDomain.Load (assemblyRef, assemblySecurity);
- }
- public static Assembly Load (Byte[] rawAssembly)
- {
- return AppDomain.CurrentDomain.Load (rawAssembly);
- }
- public static Assembly Load (Byte[] rawAssembly, Byte[] rawSymbolStore)
- {
- return AppDomain.CurrentDomain.Load (rawAssembly, rawSymbolStore);
- }
- 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)
- {
- return AppDomain.CurrentDomain.Load (assemblyName, null, true);
- }
- public static Assembly ReflectionOnlyLoadFrom (string assemblyFile)
- {
- if (assemblyFile == null)
- throw new ArgumentNullException ("assemblyFile");
-
- return LoadFrom (assemblyFile, true);
- }
- #endif
- #if NET_2_0
- [Obsolete ("")]
- #endif
- public static Assembly LoadWithPartialName (string partialName)
- {
- return LoadWithPartialName (partialName, null);
- }
- [MonoTODO]
- public Module LoadModule (string moduleName, byte [] rawModule)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public 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 ("")]
- #endif
- public static Assembly LoadWithPartialName (string partialName, Evidence securityEvidence)
- {
- return LoadWithPartialName (partialName, securityEvidence, true);
- }
- /**
- * LAMESPEC: It is possible for this method to throw exceptions IF the name supplied
- * is a valid gac name and contains filesystem entry charachters at the end of the name
- * ie System/// will throw an exception. However ////System will not as that is canocolized
- * out of the name.
- */
- // FIXME: LoadWithPartialName must look cache (no CAS) or read from disk (CAS)
- #if NET_2_0
- [Obsolete ("")]
- [ComVisible (false)]
- [MonoTODO]
- public
- #else
- internal
- #endif
- static Assembly LoadWithPartialName (string partialName, Evidence securityEvidence, bool oldBehavior)
- {
- if (!oldBehavior)
- throw new NotImplementedException ();
- if (partialName == null)
- throw new NullReferenceException ();
- return load_with_partial_name (partialName, securityEvidence);
- }
- public Object CreateInstance (String typeName)
- {
- return CreateInstance (typeName, false);
- }
- public Object CreateInstance (String typeName, Boolean ignoreCase)
- {
- Type t = GetType (typeName, false, ignoreCase);
- if (t == null)
- return null;
- try {
- return Activator.CreateInstance (t);
- } catch (InvalidOperationException) {
- throw new ArgumentException ("It is illegal to invoke a method on a Type loaded via ReflectionOnly methods.");
- }
- }
- public Object CreateInstance (String typeName, Boolean ignoreCase,
- BindingFlags bindingAttr, Binder binder,
- Object[] args, CultureInfo culture,
- Object[] activationAttributes)
- {
- Type t = GetType (typeName, false, ignoreCase);
- if (t == null)
- return null;
- try {
- return Activator.CreateInstance (t, bindingAttr, binder, args, culture, activationAttributes);
- } catch (InvalidOperationException) {
- throw new ArgumentException ("It is illegal to invoke a method on a Type loaded via ReflectionOnly methods.");
- }
- }
- public Module[] GetLoadedModules ()
- {
- 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 ();
- 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 ();
-
- [MethodImplAttribute (MethodImplOptions.InternalCall)]
- public extern virtual String[] GetManifestResourceNames ();
- [MethodImplAttribute (MethodImplOptions.InternalCall)]
- public extern static Assembly GetExecutingAssembly ();
- [MethodImplAttribute (MethodImplOptions.InternalCall)]
- public extern static Assembly GetCallingAssembly ();
- [MethodImplAttribute (MethodImplOptions.InternalCall)]
- public extern AssemblyName[] GetReferencedAssemblies ();
- [MethodImplAttribute (MethodImplOptions.InternalCall)]
- private extern bool GetManifestResourceInfoInternal (String name, ManifestResourceInfo info);
- public virtual ManifestResourceInfo GetManifestResourceInfo (String resourceName)
- {
- if (resourceName == null)
- throw new ArgumentNullException ("resourceName");
- if (resourceName == "")
- throw new ArgumentException ("String cannot have zero length.");
- ManifestResourceInfo result = new ManifestResourceInfo ();
- bool found = GetManifestResourceInfoInternal (resourceName, result);
- if (found)
- return result;
- else
- return null;
- }
- private class ResourceCloseHandler {
- Module module;
- public ResourceCloseHandler (Module module) {
- this.module = module;
- }
- public void OnClose (object sender, EventArgs e) {
- // The module dtor will take care of things
- module = null;
- }
- }
- //
- // The following functions are only for the Mono Debugger.
- //
- [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);
- [MethodImplAttribute (MethodImplOptions.InternalCall)]
- internal static extern int MonoDebugger_GetTypeToken (Type type);
- #if NET_2_0
- [MonoTODO]
- [ComVisible (false)]
- public 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;
- }
- [ComVisible (false)]
- public extern int MetadataToken {
- [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;
- }
- }
- [ComVisible (false)]
- public virtual extern bool ReflectionOnly {
- [MethodImplAttribute (MethodImplOptions.InternalCall)]
- get;
- }
- #endif
- // Code Access Security
- internal void Resolve ()
- {
- lock (this) {
- // FIXME: As we (currently) delay the resolution until the first CAS
- // 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 (UnprotectedGetEvidence (),
- _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) {
- Resolve ();
- }
- return _granted;
- }
- }
- internal PermissionSet DeniedPermissionSet {
- get {
- // yes we look for granted, as denied may be null
- if (_granted == null) {
- Resolve ();
- }
- return _denied;
- }
- }
- [MethodImplAttribute (MethodImplOptions.InternalCall)]
- extern internal static bool LoadPermissions (Assembly a,
- ref IntPtr minimum, ref int minLength,
- ref IntPtr optional, ref int optLength,
- ref IntPtr refused, ref int refLength);
- // Support for SecurityAction.RequestMinimum, RequestOptional and RequestRefuse
- private void LoadAssemblyPermissions ()
- {
- IntPtr minimum = IntPtr.Zero, optional = IntPtr.Zero, refused = IntPtr.Zero;
- int minLength = 0, optLength = 0, refLength = 0;
- if (LoadPermissions (this, ref minimum, ref minLength, ref optional,
- ref optLength, ref refused, ref refLength)) {
- // Note: no need to cache these permission sets as they will only be created once
- // at assembly resolution time.
- if (minLength > 0) {
- byte[] data = new byte [minLength];
- Marshal.Copy (minimum, data, 0, minLength);
- _minimum = SecurityManager.Decode (data);
- }
- if (optLength > 0) {
- byte[] data = new byte [optLength];
- Marshal.Copy (optional, data, 0, optLength);
- _optional = SecurityManager.Decode (data);
- }
- if (refLength > 0) {
- byte[] data = new byte [refLength];
- Marshal.Copy (refused, data, 0, refLength);
- _refuse = SecurityManager.Decode (data);
- }
- }
- }
- }
- }
|