当前位置:  开发笔记 > 编程语言 > 正文

用于将程序集信息枚举到xml的工具

如何解决《用于将程序集信息枚举到xml的工具》经验,为你挑选了1个好方法。

有没有人知道是否有工具来获得给定装配的所有装配信息.优选地以XML格式.

所需信息:

完整命名空间程序集名称

标题

文化

组态

信息版

描述

公司

产品

版权

商标

BFree.. 6

    public static class AssemblyExtensions
    {
        public static string InfoToXML(this Assembly assembly)
        {
            string name = assembly.FullName;
            string title = String.Empty;
            string description = String.Empty;
            string company = String.Empty;
            string culture = String.Empty;
            string configuration = String.Empty;
            string version = String.Empty;
            string informationalVersion = String.Empty;
            string product = String.Empty;
            string trademark = String.Empty;
            string copyright = String.Empty;

            foreach (var attrib in assembly.GetCustomAttributes(false))
            {
                if (attrib is AssemblyTitleAttribute)
                {
                    title = ((AssemblyTitleAttribute)attrib).Title;
                }

                if (attrib is AssemblyDescriptionAttribute)
                {
                    description = ((AssemblyDescriptionAttribute)attrib).Description;
                }

                if (attrib is AssemblyCompanyAttribute)
                {
                    company = ((AssemblyCompanyAttribute)attrib).Company;
                }

                if (attrib is AssemblyCultureAttribute)
                {
                    culture = ((AssemblyCultureAttribute)attrib).Culture;
                }

                if (attrib is AssemblyConfigurationAttribute)
                {
                    configuration = ((AssemblyConfigurationAttribute)attrib).Configuration;
                }

                if (attrib is AssemblyVersionAttribute)
                {
                    version = ((AssemblyVersionAttribute)attrib).Version;
                }

                if (attrib is AssemblyInformationalVersionAttribute)
                {
                    informationalVersion = ((AssemblyInformationalVersionAttribute)attrib).InformationalVersion;
                }

                if (attrib is AssemblyProductAttribute)
                {
                    product = ((AssemblyProductAttribute)attrib).Product;
                }

                if (attrib is AssemblyTrademarkAttribute)
                {
                    trademark = ((AssemblyTrademarkAttribute)attrib).Trademark;
                }

                if (attrib is AssemblyCopyrightAttribute)
                {
                    copyright = ((AssemblyCopyrightAttribute)attrib).Copyright;
                }
            }

            StringBuilder builder = new StringBuilder();
            StringWriter stringWriter = new StringWriter(builder);
            XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);

            xmlWriter.WriteStartDocument();
            xmlWriter.WriteStartElement("AssemblyInformation");
            xmlWriter.WriteElementString("AssemblyName", name);
            xmlWriter.WriteElementString("Title", title);
            xmlWriter.WriteElementString("Description", description);
            xmlWriter.WriteElementString("Company", company);
            xmlWriter.WriteElementString("Culture", culture);
            xmlWriter.WriteElementString("Configuration", configuration);
            xmlWriter.WriteElementString("Version", version);
            xmlWriter.WriteElementString("InformationalVersion", informationalVersion);
            xmlWriter.WriteElementString("Product", product);
            xmlWriter.WriteElementString("Trademark", trademark);
            xmlWriter.WriteElementString("Copyright", copyright);
            xmlWriter.WriteEndElement();

            return builder.ToString();
        }
    }

这应该让你开始.你可以添加错误处理和东西,但这应该工作.



1> BFree..:
    public static class AssemblyExtensions
    {
        public static string InfoToXML(this Assembly assembly)
        {
            string name = assembly.FullName;
            string title = String.Empty;
            string description = String.Empty;
            string company = String.Empty;
            string culture = String.Empty;
            string configuration = String.Empty;
            string version = String.Empty;
            string informationalVersion = String.Empty;
            string product = String.Empty;
            string trademark = String.Empty;
            string copyright = String.Empty;

            foreach (var attrib in assembly.GetCustomAttributes(false))
            {
                if (attrib is AssemblyTitleAttribute)
                {
                    title = ((AssemblyTitleAttribute)attrib).Title;
                }

                if (attrib is AssemblyDescriptionAttribute)
                {
                    description = ((AssemblyDescriptionAttribute)attrib).Description;
                }

                if (attrib is AssemblyCompanyAttribute)
                {
                    company = ((AssemblyCompanyAttribute)attrib).Company;
                }

                if (attrib is AssemblyCultureAttribute)
                {
                    culture = ((AssemblyCultureAttribute)attrib).Culture;
                }

                if (attrib is AssemblyConfigurationAttribute)
                {
                    configuration = ((AssemblyConfigurationAttribute)attrib).Configuration;
                }

                if (attrib is AssemblyVersionAttribute)
                {
                    version = ((AssemblyVersionAttribute)attrib).Version;
                }

                if (attrib is AssemblyInformationalVersionAttribute)
                {
                    informationalVersion = ((AssemblyInformationalVersionAttribute)attrib).InformationalVersion;
                }

                if (attrib is AssemblyProductAttribute)
                {
                    product = ((AssemblyProductAttribute)attrib).Product;
                }

                if (attrib is AssemblyTrademarkAttribute)
                {
                    trademark = ((AssemblyTrademarkAttribute)attrib).Trademark;
                }

                if (attrib is AssemblyCopyrightAttribute)
                {
                    copyright = ((AssemblyCopyrightAttribute)attrib).Copyright;
                }
            }

            StringBuilder builder = new StringBuilder();
            StringWriter stringWriter = new StringWriter(builder);
            XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);

            xmlWriter.WriteStartDocument();
            xmlWriter.WriteStartElement("AssemblyInformation");
            xmlWriter.WriteElementString("AssemblyName", name);
            xmlWriter.WriteElementString("Title", title);
            xmlWriter.WriteElementString("Description", description);
            xmlWriter.WriteElementString("Company", company);
            xmlWriter.WriteElementString("Culture", culture);
            xmlWriter.WriteElementString("Configuration", configuration);
            xmlWriter.WriteElementString("Version", version);
            xmlWriter.WriteElementString("InformationalVersion", informationalVersion);
            xmlWriter.WriteElementString("Product", product);
            xmlWriter.WriteElementString("Trademark", trademark);
            xmlWriter.WriteElementString("Copyright", copyright);
            xmlWriter.WriteEndElement();

            return builder.ToString();
        }
    }

这应该让你开始.你可以添加错误处理和东西,但这应该工作.

推荐阅读
ifx0448363
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有