Beispiel #1
0
 private static string Format(AssemblyAttributes attributes, OutputFormat format)
 {
     return(string.Format(
                CultureInfo.InvariantCulture,
                GetOutputFormat(format),
                attributes.FileName,
                attributes.FileVersion,
                attributes.Version,
                attributes.BuildType,
                attributes.DebugOutput,
                attributes.HasDebuggableAttribute,
                attributes.IsJitOptimized,
                attributes.Company,
                attributes.Copyright));
 }
Beispiel #2
0
        /// <summary>
        /// Writes the file.
        /// </summary>
        /// <param name="writer">The stream to write the output to.</param>
        /// <param name="outputFormat">The format the output should be.</param>
        /// <exception cref="ArgumentNullException"> Thrown if <paramref name="writer" /> is null.</exception>
        public static void Write(TextWriter writer, OutputFormat outputFormat)
        {
            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            writer.WriteLine(GetHeader(outputFormat));

            DirectoryInfo di = new DirectoryInfo(@".");

            foreach (FileInfo fi in di.GetFiles())
            {
                AssemblyAttributes attributes = AssemblyAttributes.ReadAssembly(fi.FullName);
                if (attributes.IsManagedAssembly)
                {
                    writer.WriteLine(Format(attributes, outputFormat));
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Analyzes an assembly to read its attributes.
        /// </summary>
        /// <param name="assemblyFile">Path to the assembly.</param>
        /// <returns>Attributes from the assembly.</returns>
        public static AssemblyAttributes ReadAssembly(string assemblyFile)
        {
            AssemblyAttributes attribs = new AssemblyAttributes();

            attribs.FileName = new FileInfo(assemblyFile).Name;

            Assembly reflectedAssembly;

            try
            {
                reflectedAssembly = Assembly.ReflectionOnlyLoadFrom(assemblyFile);
            }
            catch (BadImageFormatException)
            {
                // Not a valid asssembly
                attribs.IsManagedAssembly = false;
                return(attribs);
            }
            catch (FileLoadException)
            {
                // Not a .Net managed assembly
                attribs.IsManagedAssembly = false;
                return(attribs);
            }

            attribs.IsManagedAssembly = true;

            // If the 'DebuggableAttribute' is not found then it is definitely an OPTIMIZED build
            attribs.BuildType   = "Release";
            attribs.DebugOutput = "Full";

            attribs.Version = reflectedAssembly.GetName().Version;

            IList <CustomAttributeData> list = CustomAttributeData.GetCustomAttributes(reflectedAssembly);

            foreach (CustomAttributeData data in list)
            {
                if (data.AttributeType == typeof(DebuggableAttribute))
                {
                    attribs.HasDebuggableAttribute = true;

                    // Just because the 'DebuggableAttribute' is found doesn't necessarily mean
                    // it's a DEBUG build; we have to check the JIT Optimization flag
                    // i.e. it could have the "generate PDB" checked but have JIT Optimization enabled
                    DebuggingModes modes = (DebuggingModes)data.ConstructorArguments[0].Value;
                    attribs.IsJitOptimized = (modes & DebuggingModes.DisableOptimizations) != DebuggingModes.DisableOptimizations;
                    attribs.BuildType      = attribs.IsJitOptimized ? "Release" : "Debug";

                    // check for Debug Output "full" or "pdb-only"
                    attribs.DebugOutput = (modes & DebuggingModes.Default) != DebuggingModes.None ? "Full" : "pdb-only";

                    continue;
                }
                else if (data.AttributeType == typeof(AssemblyFileVersionAttribute))
                {
                    Version ver;
                    if (Version.TryParse((string)data.ConstructorArguments[0].Value, out ver))
                    {
                        attribs.FileVersion = ver;
                    }
                }
                else if (data.AttributeType == typeof(AssemblyCompanyAttribute))
                {
                    attribs.Company = (string)data.ConstructorArguments[0].Value;
                }
                else if (data.AttributeType == typeof(AssemblyCopyrightAttribute))
                {
                    attribs.Copyright = (string)data.ConstructorArguments[0].Value;
                }
            }

            ////object[] customAttribs = reflectedAssembly.GetCustomAttributes(typeof(DebuggableAttribute), false);

            ////// If the 'DebuggableAttribute' is not found then it is definitely an OPTIMIZED build
            ////if (customAttribs.Length > 0)
            ////{
            ////    // Just because the 'DebuggableAttribute' is found doesn't necessarily mean
            ////    // it's a DEBUG build; we have to check the JIT Optimization flag
            ////    // i.e. it could have the "generate PDB" checked but have JIT Optimization enabled
            ////    DebuggableAttribute debuggableAttribute = customAttribs[0] as DebuggableAttribute;
            ////    if (debuggableAttribute != null)
            ////    {
            ////        attribs.HasDebuggableAttribute = true;
            ////        attribs.IsJitOptimized = !debuggableAttribute.IsJITOptimizerDisabled;
            ////        attribs.BuildType = attribs.IsJitOptimized ? "Release" : "Debug";

            ////        // check for Debug Output "full" or "pdb-only"
            ////        attribs.DebugOutput = (debuggableAttribute.DebuggingFlags & DebuggableAttribute.DebuggingModes.Default) != DebuggableAttribute.DebuggingModes.None ? "Full" : "pdb-only";
            ////    }
            ////}
            ////else
            ////{
            ////    attribs.BuildType = "Release";
            ////    attribs.DebugOutput = "Full";
            ////}

            ////customAttribs = reflectedAssembly.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false);
            ////if (customAttribs.Length > 0)
            ////{
            ////    attribs.FileVersion = Version.Parse((customAttribs[0] as AssemblyFileVersionAttribute).Version);
            ////}

            ////attribs.Version = reflectedAssembly.GetName().Version;

            ////customAttribs = reflectedAssembly.GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
            ////if (customAttribs.Length > 0)
            ////{
            ////    attribs.Company = (customAttribs[0] as AssemblyCompanyAttribute).Company;
            ////}

            ////customAttribs = reflectedAssembly.GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
            ////if (customAttribs.Length > 0)
            ////{
            ////    attribs.Copyright = (customAttribs[0] as AssemblyCopyrightAttribute).Copyright;
            ////}

            return(attribs);
        }