■ Assembly 클래스 : GetCustomAttributes 메소드를 사용해 어셈블리 정보 구하기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip


AssemblyHelper.cs

 

 

using System;

using System.Reflection;

using System.Resources;

using System.Runtime.InteropServices;

 

namespace TestProject

{

    /// <summary>

    /// 어셈블리 헬퍼

    /// </summary>

    public class AssemblyHelper

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region Field

 

        /// <summary>

        /// 제목

        /// </summary>

        public string Title = string.Empty;

        

        /// <summary>

        /// 설명

        /// </summary>

        public string Description = string.Empty;

        

        /// <summary>

        /// 회사

        /// </summary>

        public string Company = string.Empty;

        

        /// <summary>

        /// 제품

        /// </summary>

        public string Product = string.Empty;

        

        /// <summary>

        /// 저작권

        /// </summary>

        public string Copyright = string.Empty;

        

        /// <summary>

        /// 상표

        /// </summary>

        public string Trademark = string.Empty;

        

        /// <summary>

        /// 어셈블리 버전

        /// </summary>

        public string AssemblyVersion = string.Empty;

        

        /// <summary>

        /// 파일 버전

        /// </summary>

        public string FileVersion = string.Empty;

        

        /// <summary>

        /// GUID

        /// </summary>

        public string GUID = string.Empty;

 

        /// <summary>

        /// 중립 언어

        /// </summary>

        public string NeutralLanguage = string.Empty;

 

        /// <summary>

        /// COM 표시 여부

        /// </summary>

        public bool IsCOMVisible = false;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - AssemblyHelper(assembly)

 

        /// <summary>

        /// 생성자

        /// </summary>

        /// <param name="assembly">어셈블리</param>

        public AssemblyHelper(Assembly assembly)

        {

            AssemblyTitleAttribute assemblyTitleAttribute = GetAssemblyAttribute<AssemblyTitleAttribute>(assembly);

 

            if(assemblyTitleAttribute != null)

            {

                Title = assemblyTitleAttribute.Title;

            }

 

            AssemblyDescriptionAttribute assemblyDescriptionAttribute = GetAssemblyAttribute<AssemblyDescriptionAttribute>(assembly);

 

            if(assemblyDescriptionAttribute != null)

            {

                Description = assemblyDescriptionAttribute.Description;

            }

 

            AssemblyCompanyAttribute assemblyCompanyAttribute = GetAssemblyAttribute<AssemblyCompanyAttribute>(assembly);

 

            if(assemblyCompanyAttribute != null)

            {

                Company = assemblyCompanyAttribute.Company;

            }

 

            AssemblyProductAttribute assemblyProductAttribute = GetAssemblyAttribute<AssemblyProductAttribute>(assembly);

 

            if(assemblyProductAttribute != null)

            {

                Product = assemblyProductAttribute.Product;

            }

 

            AssemblyCopyrightAttribute assemblyCopyrightAttribute = GetAssemblyAttribute<AssemblyCopyrightAttribute>(assembly);

 

            if(assemblyCopyrightAttribute != null)

            {

                Copyright = assemblyCopyrightAttribute.Copyright;

            }

 

            AssemblyTrademarkAttribute assemblyTrademarkAttribute = GetAssemblyAttribute<AssemblyTrademarkAttribute>(assembly);

 

            if(assemblyTrademarkAttribute != null)

            {

                Trademark = assemblyTrademarkAttribute.Trademark;

            }

 

            AssemblyVersion = assembly.GetName().Version.ToString();

 

            AssemblyFileVersionAttribute assemblyFileVersionAttribute = GetAssemblyAttribute<AssemblyFileVersionAttribute>(assembly);

 

            if(assemblyFileVersionAttribute != null)

            {

                FileVersion = assemblyFileVersionAttribute.Version;

            }

 

            GuidAttribute guidAttribute = GetAssemblyAttribute<GuidAttribute>(assembly);

 

            if(guidAttribute != null)

            {

                GUID = guidAttribute.Value;

            }

 

            NeutralResourcesLanguageAttribute neutralResourcesLanguageAttribute =

                GetAssemblyAttribute<NeutralResourcesLanguageAttribute>(assembly);

 

            if(neutralResourcesLanguageAttribute != null)

            {

                NeutralLanguage = neutralResourcesLanguageAttribute.CultureName;

            }

 

            ComVisibleAttribute comVisibleAttribute = GetAssemblyAttribute<ComVisibleAttribute>(assembly);

 

            if(comVisibleAttribute != null)

            {

                IsCOMVisible = comVisibleAttribute.Value;

            }

        }

 

        #endregion

        #region 생성자 - AssemblyHelper()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public AssemblyHelper() : this(Assembly.GetExecutingAssembly())

        {

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Public

 

        #region 어셈블리 어트리뷰트 구하기 - GetAssemblyAttribute<T>(assembly)

 

        /// <summary>

        /// 어셈블리 어트리뷰트 구하기

        /// </summary>

        /// <typeparam name="T">어트리뷰트 타입</typeparam>

        /// <param name="assembly">어셈블리</param>

        /// <returns>어셈블리 어트리뷰트</returns>

        public static T GetAssemblyAttribute<T>(Assembly assembly) where T : Attribute

        {

            object[] attributeArray = assembly.GetCustomAttributes(typeof(T), true);

 

            if((attributeArray == null) || (attributeArray.Length == 0))

            {

                return null;

            }

 

            return (T)attributeArray[0];

        }

 

        #endregion

    }

}

 

 

MainForm.cs

 

 

using System;

using System.Windows.Forms;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : Form

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            #region 이벤트를 설정한다.

 

            Load += Form_Load;

 

            #endregion

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        //////////////////////////////////////////////////////////////////////////////// Event

 

        #region 폼 로드시 처리하기 - Form_Load(sender, e)

 

        /// <summary>

        /// 폼 로드시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void Form_Load(object sender, EventArgs e)

        {

            AssemblyHelper helper = new AssemblyHelper();

 

            this.titleTextBox.Text           = helper.Title;

            this.descriptionTextBox.Text     = helper.Description;

            this.companyTextBox.Text         = helper.Company;

            this.productTextBox.Text         = helper.Product;

            this.copyrightTextBox.Text       = helper.Copyright;

            this.trademarkTextBox.Text       = helper.Trademark;

            this.assemblyVersionTextBox.Text = helper.AssemblyVersion;

            this.fileVersionTextBox.Text     = helper.FileVersion;

            this.guidTextBox.Text            = helper.GUID;

            this.neutralLanguageTextBox.Text = helper.NeutralLanguage;

            this.isCOMVisibleTextBox.Text    = helper.IsCOMVisible.ToString();

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker

댓글을 달아 주세요