internal static Assembly GetLoadedAssembly(string assemblyName, string assemblyVersion, string assemblyKey) { Assembly assembly; AssemblyName asmName = new AssemblyName(assemblyName); // We always use the primary assembly (culture neutral) for resource manager. // if the required resource lives in satellite assembly, ResourceManager can find // the right satellite assembly later. asmName.CultureInfo = new CultureInfo(String.Empty); if (!String.IsNullOrEmpty(assemblyVersion)) { asmName.Version = new Version(assemblyVersion); } byte[] keyToken = ParseAssemblyKey(assemblyKey); if (keyToken != null) { asmName.SetPublicKeyToken(keyToken); } assembly = SafeSecurityHelper.GetLoadedAssembly(asmName); if (assembly == null) { // The assembly is not yet loaded to the AppDomain, try to load it with information specified in resource Uri. assembly = Assembly.Load(asmName); } return(assembly); }
private static Assembly LoadAssemblyHelper(string assemblyGivenName, string assemblyPath) { AssemblyName assemblyName = new AssemblyName(assemblyGivenName); string name = assemblyName.Name; name = name.ToUpper(CultureInfo.InvariantCulture); Assembly assembly = (Assembly)_loadedAssembliesHash[name]; if (assembly != null) { if (assemblyName.Version != null) { AssemblyName assemblyName2 = new AssemblyName(assembly.FullName); if (!AssemblyName.ReferenceMatchesDefinition(assemblyName, assemblyName2)) { string text = assemblyName.ToString(); string text2 = assemblyName2.ToString(); throw new InvalidOperationException(SR.Get("ParserAssemblyLoadVersionMismatch", text, text2)); } } } else { if (string.IsNullOrEmpty(assemblyPath)) { assembly = SafeSecurityHelper.GetLoadedAssembly(assemblyName); } if (assembly == null) { if (string.IsNullOrEmpty(assemblyPath)) { try { assembly = Assembly.Load(assemblyGivenName); } catch (FileNotFoundException) { assembly = null; } } else { assembly = Assembly.LoadFile(assemblyPath); } } if (assembly != null) { _loadedAssembliesHash[name] = assembly; } } return(assembly); }
internal static Assembly GetLoadedAssembly(string assemblyName, string assemblyVersion, string assemblyKey) { Assembly assembly; AssemblyName asmName = new AssemblyName(assemblyName); // We always use the primary assembly (culture neutral) for resource manager. // if the required resource lives in satellite assembly, ResourceManager can find // the right satellite assembly later. asmName.CultureInfo = new CultureInfo(String.Empty); if (!String.IsNullOrEmpty(assemblyVersion)) { asmName.Version = new Version(assemblyVersion); } if (!String.IsNullOrEmpty(assemblyKey)) { int byteCount = assemblyKey.Length / 2; byte[] keyToken = new byte[byteCount]; for (int i = 0; i < byteCount; i++) { string byteString = assemblyKey.Substring(i * 2, 2); keyToken[i] = byte.Parse(byteString, NumberStyles.HexNumber, CultureInfo.InvariantCulture); } asmName.SetPublicKeyToken(keyToken); } assembly = SafeSecurityHelper.GetLoadedAssembly(asmName); if (assembly == null) { // The assembly is not yet loaded to the AppDomain, try to load it with information specified in resource Uri. assembly = Assembly.Load(asmName); } return(assembly); }
internal static Assembly GetLoadedAssembly(string assemblyName, string assemblyVersion, string assemblyKey) { AssemblyName assemblyName2 = new AssemblyName(assemblyName) { CultureInfo = new CultureInfo(string.Empty) }; if (!string.IsNullOrEmpty(assemblyVersion)) { assemblyName2.Version = new Version(assemblyVersion); } byte[] array = ParseAssemblyKey(assemblyKey); if (array != null) { assemblyName2.SetPublicKeyToken(array); } Assembly assembly = SafeSecurityHelper.GetLoadedAssembly(assemblyName2); if (assembly == null) { assembly = Assembly.Load(assemblyName2); } return(assembly); }
// Loads the Assembly with the specified name at the specified optional location. // // assemblyName is either short name or full name. // assemblyPath is either full file path or null. // private static Assembly LoadAssemblyHelper(string assemblyGivenName, string assemblyPath) { AssemblyName assemblyName = new AssemblyName(assemblyGivenName); string assemblyShortName = assemblyName.Name; assemblyShortName = assemblyShortName.ToUpper(CultureInfo.InvariantCulture); // Check if the assembly has already been loaded. Assembly retassem = (Assembly)_loadedAssembliesHash[assemblyShortName]; if (retassem != null) { if (assemblyName.Version != null) { AssemblyName cachedName = new AssemblyName(retassem.FullName); if (!AssemblyName.ReferenceMatchesDefinition(assemblyName, cachedName)) { string request = assemblyName.ToString(); string found = cachedName.ToString(); throw new InvalidOperationException(SR.Get(SRID.ParserAssemblyLoadVersionMismatch, request, found)); } } } else { // Check if the current AppDomain has this assembly loaded for some other reason. // If so, then just use that assembly and don't attempt to load another copy of it. // Only do this if no path is provided. if (String.IsNullOrEmpty(assemblyPath)) { retassem = SafeSecurityHelper.GetLoadedAssembly(assemblyName); } if (retassem == null) { if (!String.IsNullOrEmpty(assemblyPath)) { // assemblyPath is set, Load the assembly from this specified place. // the path must be full file path which contains directory, file name and extension. Debug.Assert(!assemblyPath.EndsWith("\\", StringComparison.Ordinal), "the assembly path should be a full file path containing file extension"); // LoadFile will only override your request only if it is in the GAC retassem = Assembly.LoadFile(assemblyPath); } // // At compile time, the build task should always pass the full path of the referenced assembly, even if it // comes from GAC. But below code snippet can run if parser wants to try loading an assembly w/o a path. // This also makes run-time assembly load consistent with compile-time semantics. else { try { retassem = Assembly.Load(assemblyGivenName); } catch (System.IO.FileNotFoundException) { // This may be a locally defined assembly that has not been created yet. // To support these cases, just set a null assembly and return. This // will fail downstream if it really was an assembly miss. retassem = null; } } } // Cache the assembly if (retassem != null) { _loadedAssembliesHash[assemblyShortName] = retassem; } } return(retassem); }