/// <summary>
        /// To be used as a delegate. Gets the AssemblyName of the given file.
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        internal static AssemblyNameExtension GetAssemblyNameEx(string path)
        {
            AssemblyName assemblyName = null;

#if FEATURE_ASSEMBLY_LOADFROM && !MONO
            try
            {
                assemblyName = AssemblyName.GetAssemblyName(path);
            }
            catch (System.IO.FileLoadException)
            {
                // Its pretty hard to get here, you need an assembly that contains a valid reference
                // to a dependent assembly that, in turn, throws a FileLoadException during GetAssemblyName.
                // Still it happened once, with an older version of the CLR.

                // ...falling through and relying on the targetAssemblyName==null behavior below...
            }
            catch (System.IO.FileNotFoundException)
            {
                // Its pretty hard to get here, also since we do a file existence check right before calling this method so it can only happen if the file got deleted between that check and this call.
            }
#else
            using (var stream = File.OpenRead(path))
                using (var peFile = new PEReader(stream))
                {
                    bool hasMetadata = false;
                    try
                    {
                        // This can throw if the stream is too small, which means
                        // the assembly doesn't have metadata.
                        hasMetadata = peFile.HasMetadata;
                    }
                    finally
                    {
                        // If the file does not contain PE metadata, throw BadImageFormatException to preserve
                        // behavior from AssemblyName.GetAssemblyName(). RAR will deal with this correctly.
                        if (!hasMetadata)
                        {
                            throw new BadImageFormatException(string.Format(CultureInfo.CurrentCulture,
                                                                            AssemblyResources.GetString("ResolveAssemblyReference.AssemblyDoesNotContainPEMetadata"),
                                                                            path));
                        }
                    }

                    var metadataReader = peFile.GetMetadataReader();
                    var entry          = metadataReader.GetAssemblyDefinition();

                    assemblyName         = new AssemblyName();
                    assemblyName.Name    = metadataReader.GetString(entry.Name);
                    assemblyName.Version = entry.Version;
                    var cultureString = metadataReader.GetString(entry.Culture);
                    if (!NativeMethodsShared.IsMono)
                    {
                        // set_CultureName throws NotImplementedException on Mono
                        assemblyName.CultureName = cultureString;
                    }
                    else if (cultureString != null)
                    {
                        assemblyName.CultureInfo = new CultureInfo(cultureString);
                    }
                    assemblyName.SetPublicKey(metadataReader.GetBlobBytes(entry.PublicKey));
                    assemblyName.Flags = (AssemblyNameFlags)(int)entry.Flags;
                }
#endif
            return(assemblyName == null ? null : new AssemblyNameExtension(assemblyName));
        }
Esempio n. 2
0
 internal static string GetResourceString(string resourceName)
 {
     return(AssemblyResources.GetString(resourceName));
 }
        internal static void VerifyThrowInvalidProjectFile
        (
            bool condition,
            string errorSubCategoryResourceName,
            BuildEventFileInfo projectFile,
            Exception innerException,
            string resourceName,
            params object[] args
        )
        {
            ErrorUtilities.VerifyThrow(projectFile != null, "Must specify the invalid project file. If project file is not available, use VerifyThrowInvalidProject() and pass in the XML node instead.");

#if DEBUG
            if (errorSubCategoryResourceName != null)
            {
                ResourceUtilities.VerifyResourceStringExists(errorSubCategoryResourceName);
            }

            ResourceUtilities.VerifyResourceStringExists(resourceName);
#endif
            if (!condition)
            {
                string errorSubCategory = errorSubCategoryResourceName is null ? null : AssemblyResources.GetString(errorSubCategoryResourceName);
                string message          = ResourceUtilities.FormatResourceStringStripCodeAndKeyword(out string errorCode, out string helpKeyword, resourceName, args);

                throw new InvalidProjectFileException(projectFile.File, projectFile.Line, projectFile.Column, projectFile.EndLine, projectFile.EndColumn, message, errorSubCategory, errorCode, helpKeyword, innerException);
            }
        }
Esempio n. 4
0
 internal static string FormatResourceString(string resourceName, params object[] args)
 {
     ErrorUtilities.VerifyThrowArgumentNull((object)resourceName, "resourceName");
     return(AssemblyResources.FormatString(AssemblyResources.GetString(resourceName), args));
 }