Beispiel #1
0
 /// <summary>
 /// Recupera o nome correto do assembly.
 /// </summary>
 /// <param name="original"></param>
 /// <param name="dic"></param>
 /// <returns></returns>
 public static System.Reflection.AssemblyName GetCorrectAssemblyName(System.Reflection.AssemblyName original, Dictionary <string, List <Redirection> > dic)
 {
     if (dic.ContainsKey(original.Name))
     {
         foreach (Redirection redirection in dic[original.Name])
         {
             if (redirection.BindingRedirection == null)
             {
                 System.Diagnostics.Trace.WriteLine("Redirection data is invalid: " + redirection.AssemblyIdentity);
             }
             else
             {
                 Version redirectVersionMin = redirection.BindingRedirection.OldVersionMin;
                 Version redirectVersionMax = redirection.BindingRedirection.OldVersionMax;
                 if ((original.Version >= redirectVersionMin) && (original.Version <= redirectVersionMax))
                 {
                     var name = new System.Reflection.AssemblyName(original.FullName);
                     name.Version = redirection.BindingRedirection.NewVersion;
                     name.ProcessorArchitecture = original.ProcessorArchitecture;
                     name.SetPublicKeyToken(original.GetPublicKeyToken());
                     return(name);
                 }
             }
         }
     }
     return(original);
 }
Beispiel #2
0
        private void InitializeTypeForwarder(ForwarderKey key, TypeForwarder record)
        {
            Cts.Ecma.EcmaModule module       = key.Module;
            Ecma.MetadataReader reader       = module.MetadataReader;
            Ecma.ExportedType   exportedType = reader.GetExportedType(key.ExportedType);

            record.Name = HandleString(reader.GetString(exportedType.Name));

            switch (exportedType.Implementation.Kind)
            {
            case Ecma.HandleKind.AssemblyReference:
            {
                string ns = reader.GetString(exportedType.Namespace);
                NamespaceDefinition namespaceDefinition = HandleNamespaceDefinition(module, ns);

                Ecma.AssemblyReference assemblyRef = reader.GetAssemblyReference((Ecma.AssemblyReferenceHandle)exportedType.Implementation);
                AssemblyName           refName     = new AssemblyName
                {
                    ContentType = (AssemblyContentType)((int)(assemblyRef.Flags & AssemblyFlags.ContentTypeMask) >> 9),
                    Flags       = (AssemblyNameFlags)(assemblyRef.Flags & ~AssemblyFlags.ContentTypeMask),
                    CultureName = reader.GetString(assemblyRef.Culture),
                    Name        = reader.GetString(assemblyRef.Name),
                    Version     = assemblyRef.Version,
                };

                if ((assemblyRef.Flags & AssemblyFlags.PublicKey) != 0)
                {
                    refName.SetPublicKey(reader.GetBlobBytes(assemblyRef.PublicKeyOrToken));
                }
                else
                {
                    refName.SetPublicKeyToken(reader.GetBlobBytes(assemblyRef.PublicKeyOrToken));
                }

                record.Scope = HandleScopeReference(refName);

                namespaceDefinition.TypeForwarders.Add(record);
            }
            break;

            case Ecma.HandleKind.ExportedType:
            {
                TypeForwarder scope = HandleTypeForwarder(module, (Ecma.ExportedTypeHandle)exportedType.Implementation);

                record.Scope = scope.Scope;

                scope.NestedTypes.Add(record);
            }
            break;

            default:
                throw new BadImageFormatException();
            }
        }
        public static string Format(this AssemblyIdentity assemblyIdentity)
        {
            var name = new System.Reflection.AssemblyName();
            name.Name = assemblyIdentity.Name.Value;
#if !COREFX
            name.CultureInfo = new CultureInfo(assemblyIdentity.Culture);
#endif
            name.Version = assemblyIdentity.Version;
            name.SetPublicKeyToken(assemblyIdentity.PublicKeyToken.ToArray());
#if !COREFX
            name.CodeBase = assemblyIdentity.Location;
#endif
            return name.ToString();
        }
Beispiel #4
0
        public static string Format(this AssemblyIdentity assemblyIdentity)
        {
            var name = new System.Reflection.AssemblyName();

            name.Name = assemblyIdentity.Name.Value;
#if !COREFX
            name.CultureInfo = new CultureInfo(assemblyIdentity.Culture);
#endif
            name.Version = assemblyIdentity.Version;
            name.SetPublicKeyToken(assemblyIdentity.PublicKeyToken.ToArray());
#if !COREFX
            name.CodeBase = assemblyIdentity.Location;
#endif
            return(name.ToString());
        }
 private static System.Reflection.Assembly Load(string assemblyNameVal) {
     System.Reflection.AssemblyName assemblyName = new System.Reflection.AssemblyName(assemblyNameVal);
     byte[] publicKeyToken = assemblyName.GetPublicKeyToken();
     System.Reflection.Assembly asm = null;
     try {
         asm = System.Reflection.Assembly.Load(assemblyName.FullName);
     }
     catch (System.Exception ) {
         System.Reflection.AssemblyName shortName = new System.Reflection.AssemblyName(assemblyName.Name);
         if ((publicKeyToken != null)) {
             shortName.SetPublicKeyToken(publicKeyToken);
         }
         asm = System.Reflection.Assembly.Load(shortName);
     }
     return asm;
 }
Beispiel #6
0
 private static System.Reflection.Assembly Load(string assemblyNameVal) {
     System.Reflection.AssemblyName assemblyName = new System.Reflection.AssemblyName(assemblyNameVal);
     byte[] publicKeyToken = assemblyName.GetPublicKeyToken();
     System.Reflection.Assembly asm = null;
     try {
         asm = System.Reflection.Assembly.Load(assemblyName.FullName);
     }
     catch (System.Exception ) {
         System.Reflection.AssemblyName shortName = new System.Reflection.AssemblyName(assemblyName.Name);
         if ((publicKeyToken != null)) {
             shortName.SetPublicKeyToken(publicKeyToken);
         }
         asm = System.Reflection.Assembly.Load(shortName);
     }
     return asm;
 }
Beispiel #7
0
        public static string Format(this AssemblyIdentity assemblyIdentity)
        {
            var name        = new System.Reflection.AssemblyName();
            var cultureInfo = new CultureInfo(assemblyIdentity.Culture);

            name.Name = assemblyIdentity.Name.Value;
#if NETSTANDARD1_3 || NETSTANDARD1_6
            name.CultureName = cultureInfo.Name;
#else
            name.CultureInfo = cultureInfo;
#endif
            name.Version = assemblyIdentity.Version;
            name.SetPublicKeyToken(assemblyIdentity.PublicKeyToken.ToArray());
#if !NETSTANDARD1_3 && !NETSTANDARD1_6
            name.CodeBase = assemblyIdentity.Location;
#endif
            return(name.ToString());
        }
        public static string Format(this AssemblyIdentity assemblyIdentity)
        {
            var name        = new System.Reflection.AssemblyName();
            var cultureInfo = new CultureInfo(assemblyIdentity.Culture);

            name.Name = assemblyIdentity.Name.Value;
#if FEATURE_ASSEMBLYNAME_CULTUREINFO
            name.CultureInfo = cultureInfo;
#else
            name.CultureName = cultureInfo.Name;
#endif
            name.Version = assemblyIdentity.Version;
            name.SetPublicKeyToken(assemblyIdentity.PublicKeyToken.ToArray());
#if FEATURE_ASSEMBLYNAME_CODEBASE
            name.CodeBase = assemblyIdentity.Location;
#endif
            return(name.ToString());
        }
Beispiel #9
0
 public void SetPublicKeyToken(byte[] publicKeyToken)
 {
     name.SetPublicKeyToken(publicKeyToken);
 }
Beispiel #10
0
// ReSharper disable InconsistentNaming
    public static void initclr()
// ReSharper restore InconsistentNaming    
    {
#if DEBUG_PRINT
        System.Console.WriteLine("Attempting to load Python.Runtime using standard binding rules... ");
#endif
#if USE_PYTHON_RUNTIME_PUBLIC_KEY_TOKEN
        var pythonRuntimePublicKeyTokenData = new byte[] { 0x50, 0x00, 0xfe, 0xa6, 0xcb, 0xa7, 0x02, 0xdd };
#endif

        // Attempt to find and load Python.Runtime using standard assembly binding rules.
        // This roughly translates into looking in order:
        // - GAC
        // - ApplicationBase
        // - A PrivateBinPath under ApplicationBase
        // With an unsigned assembly, the GAC is skipped.
        var pythonRuntimeName = new System.Reflection.AssemblyName("Python.Runtime")
            {
#if USE_PYTHON_RUNTIME_VERSION
                Version = new System.Version("4.0.0.1"), 
#endif
                CultureInfo = System.Globalization.CultureInfo.InvariantCulture,
            };
#if USE_PYTHON_RUNTIME_PUBLIC_KEY_TOKEN
        pythonRuntimeName.SetPublicKeyToken(pythonRuntimePublicKeyTokenData);
#endif
        // We've got the AssemblyName with optional features; try to load it.
        System.Reflection.Assembly pythonRuntime;
        try
        {
            pythonRuntime = System.Reflection.Assembly.Load(pythonRuntimeName);
#if DEBUG_PRINT
            System.Console.WriteLine("Success!");
#endif
        }
        catch (System.IO.FileNotFoundException)
        {
            try
            {
                // If the above fails for any reason, we fallback to attempting to load "Python.Runtime.dll"
			    // from the directory this assembly is running in. "This assembly" is probably "clr.pyd",
			    // sitting somewhere in PYTHONPATH.  This is using Assembly.LoadFrom, and inherits all the
			    // caveats of that call.  See MSDN docs for details.
			    // Suzanne Cook's blog is also an excellent source of info on this:
			    // http://blogs.msdn.com/suzcook/
			    // http://blogs.msdn.com/suzcook/archive/2003/05/29/57143.aspx
			    // http://blogs.msdn.com/suzcook/archive/2003/06/13/57180.aspx

                var executingAssembly = System.Reflection.Assembly.GetExecutingAssembly();
                var assemblyDirectory = System.IO.Path.GetDirectoryName(executingAssembly.Location);
                if (assemblyDirectory == null)
                    throw new System.InvalidOperationException(executingAssembly.Location);
                var pythonRuntimeDllPath = System.IO.Path.Combine(assemblyDirectory, "Python.Runtime.dll");
#if DEBUG_PRINT
                System.Console.WriteLine("Attempting to load Python.Runtime from: '{0}'...", pythonRuntimeDllPath);
#endif
                pythonRuntime = System.Reflection.Assembly.LoadFrom(pythonRuntimeDllPath);
            }
            catch (System.InvalidOperationException) {
#if DEBUG_PRINT
                System.Console.WriteLine("Could not load Python.Runtime, so sad.");
#endif
                return;
            }
        }

        // Once here, we've successfully loaded SOME version of Python.Runtime
		// So now we get the PythonEngine and execute the InitExt method on it.
        var pythonEngineType = pythonRuntime.GetType("Python.Runtime.PythonEngine");
        pythonEngineType.InvokeMember("InitExt", System.Reflection.BindingFlags.InvokeMethod, null, null, null);
    }
Beispiel #11
0
// ReSharper disable InconsistentNaming
    public static void initclr()
// ReSharper restore InconsistentNaming
    {
#if DEBUG_PRINT
        System.Console.WriteLine("Attempting to load Python.Runtime using standard binding rules... ");
#endif
#if USE_PYTHON_RUNTIME_PUBLIC_KEY_TOKEN
        var pythonRuntimePublicKeyTokenData = new byte[] { 0x50, 0x00, 0xfe, 0xa6, 0xcb, 0xa7, 0x02, 0xdd };
#endif

        // Attempt to find and load Python.Runtime using standard assembly binding rules.
        // This roughly translates into looking in order:
        // - GAC
        // - ApplicationBase
        // - A PrivateBinPath under ApplicationBase
        // With an unsigned assembly, the GAC is skipped.
        var pythonRuntimeName = new System.Reflection.AssemblyName("Python.Runtime")
        {
#if USE_PYTHON_RUNTIME_VERSION
            Version = new System.Version("4.0.0.1"),
#endif
            CultureInfo = System.Globalization.CultureInfo.InvariantCulture,
        };
#if USE_PYTHON_RUNTIME_PUBLIC_KEY_TOKEN
        pythonRuntimeName.SetPublicKeyToken(pythonRuntimePublicKeyTokenData);
#endif
        // We've got the AssemblyName with optional features; try to load it.
        System.Reflection.Assembly pythonRuntime;
        try
        {
            pythonRuntime = System.Reflection.Assembly.Load(pythonRuntimeName);
#if DEBUG_PRINT
            System.Console.WriteLine("Success!");
#endif
        }
        catch (System.IO.FileNotFoundException)
        {
            try
            {
                // If the above fails for any reason, we fallback to attempting to load "Python.Runtime.dll"
                // from the directory this assembly is running in. "This assembly" is probably "clr.pyd",
                // sitting somewhere in PYTHONPATH.  This is using Assembly.LoadFrom, and inherits all the
                // caveats of that call.  See MSDN docs for details.
                // Suzanne Cook's blog is also an excellent source of info on this:
                // http://blogs.msdn.com/suzcook/
                // http://blogs.msdn.com/suzcook/archive/2003/05/29/57143.aspx
                // http://blogs.msdn.com/suzcook/archive/2003/06/13/57180.aspx

                var executingAssembly = System.Reflection.Assembly.GetExecutingAssembly();
                var assemblyDirectory = System.IO.Path.GetDirectoryName(executingAssembly.Location);
                if (assemblyDirectory == null)
                {
                    throw new System.InvalidOperationException(executingAssembly.Location);
                }
                var pythonRuntimeDllPath = System.IO.Path.Combine(assemblyDirectory, "Python.Runtime.dll");
#if DEBUG_PRINT
                System.Console.WriteLine("Attempting to load Python.Runtime from: '{0}'...", pythonRuntimeDllPath);
#endif
                pythonRuntime = System.Reflection.Assembly.LoadFrom(pythonRuntimeDllPath);
            }
            catch (System.InvalidOperationException) {
#if DEBUG_PRINT
                System.Console.WriteLine("Could not load Python.Runtime, so sad.");
#endif
                return;
            }
        }

        // Once here, we've successfully loaded SOME version of Python.Runtime
        // So now we get the PythonEngine and execute the InitExt method on it.
        var pythonEngineType = pythonRuntime.GetType("Python.Runtime.PythonEngine");
        pythonEngineType.InvokeMember("InitExt", System.Reflection.BindingFlags.InvokeMethod, null, null, null);
    }
Beispiel #12
0
 public System.Reflection.AssemblyName GetAssemblyName()
 {
     if(this.assemblyName == null)
     {
         System.Reflection.AssemblyName aName = new System.Reflection.AssemblyName();
         aName.CultureInfo = new System.Globalization.CultureInfo(this.Culture == null ? "" : this.Culture);
         if(this.PublicKeyOrToken != null && this.PublicKeyOrToken.Length > 8)
             aName.Flags = System.Reflection.AssemblyNameFlags.PublicKey;
         if((this.Flags & AssemblyFlags.Retargetable) != 0)
             aName.Flags |= (System.Reflection.AssemblyNameFlags)AssemblyFlags.Retargetable;
         aName.HashAlgorithm = System.Configuration.Assemblies.AssemblyHashAlgorithm.SHA1;
         if(this.PublicKeyOrToken != null)
         {
             if(this.PublicKeyOrToken.Length > 8)
                 aName.SetPublicKey(this.PublicKeyOrToken);
             else if(this.PublicKeyOrToken.Length > 0)
                 aName.SetPublicKeyToken(this.PublicKeyOrToken);
         }
         else
             aName.SetPublicKey(new byte[0]);
         aName.Name = this.Name;
         aName.Version = this.Version;
         switch(this.Flags & AssemblyFlags.CompatibilityMask)
         {
             case AssemblyFlags.NonSideBySideCompatible:
                 aName.VersionCompatibility = System.Configuration.Assemblies.AssemblyVersionCompatibility.SameDomain;
                 break;
             case AssemblyFlags.NonSideBySideProcess:
                 aName.VersionCompatibility = System.Configuration.Assemblies.AssemblyVersionCompatibility.SameProcess;
                 break;
             case AssemblyFlags.NonSideBySideMachine:
                 aName.VersionCompatibility = System.Configuration.Assemblies.AssemblyVersionCompatibility.SameMachine;
                 break;
         }
         this.assemblyName = aName;
     }
     return this.assemblyName;
 }