첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

TestProject.zip
다운로드

▶ TypeTreeViewItem.cs

using System;
using System.Windows.Controls;

namespace TestProject
{
    /// <summary>
    /// 타입 트리 뷰 항목
    /// </summary>
    public class TypeTreeViewItem : TreeViewItem
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 타입
        /// </summary>
        private Type type;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 타입 - Type

        /// <summary>
        /// 타입
        /// </summary>
        public Type Type
        {
            set
            {
                this.type = value;

                if(this.type.IsAbstract)
                {
                    Header = this.type.Name + " (abstract)";
                }
                else
                {
                    Header = this.type.Name;
                }
            }
            get
            {
                return this.type;
            }
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - TypeTreeViewItem()

        /// <summary>
        /// 생성자
        /// </summary>
        public TypeTreeViewItem()
        {
        }

        #endregion
        #region 생성자 - TypeTreeViewItem(type)

        /// <summary>
        /// 생성자
        /// </summary>
        /// <param name="type">타입</param>
        public TypeTreeViewItem(Type type)
        {
            Type = type;
        }

        #endregion
    }
}

 

▶ ClassHierarchyTreeView.cs

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;

namespace TestProject
{
    /// <summary>
    /// 클래스 계층 트리 뷰
    /// </summary>
    public class ClassHierarchyTreeView : TreeView
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - ClassHierarchyTreeView(rootType)

        /// <summary>
        /// 생성자
        /// </summary>
        /// <param name="rootType">루트 타입</param>
        public ClassHierarchyTreeView(Type rootType)
        {
            UIElement dummyUIElement = new UIElement();

            List<Assembly> assemblyList = new List<Assembly>();

            AssemblyName[] assemblyNameArray = Assembly.GetExecutingAssembly().GetReferencedAssemblies();

            foreach(AssemblyName assemblyName in assemblyNameArray)
            {
                assemblyList.Add(Assembly.Load(assemblyName));
            }

            SortedList<string, Type> classSortedList = new SortedList<string, Type>();

            classSortedList.Add(rootType.Name, rootType);

            foreach(Assembly assembly in assemblyList)
            {
                foreach(Type type in assembly.GetTypes())
                {
                    if(type.IsPublic && type.IsSubclassOf(rootType))
                    {
                        classSortedList.Add(type.Name, type);
                    }
                }
            }

            TypeTreeViewItem rootTypeTreeViewItem = new TypeTreeViewItem(rootType);

            Items.Add(rootTypeTreeViewItem);

            CreateDerivedClasses(rootTypeTreeViewItem, classSortedList);
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 파생 클래스 생성하기 - CreateDerivedClasses(baseTypeTreeViewItem, classSortedList)

        /// <summary>
        /// 파생 클래스 생성하기
        /// </summary>
        /// <param name="baseTypeTreeViewItem">베이스 타입 트리 뷰 항목</param>
        /// <param name="classSortedList">클래스 정렬 리스트</param>
        private void CreateDerivedClasses(TypeTreeViewItem baseTypeTreeViewItem, SortedList<string, Type> classSortedList)
        {
            foreach(KeyValuePair<string, Type> keyValuePair in classSortedList)
            {
                if(keyValuePair.Value.BaseType == baseTypeTreeViewItem.Type)
                {
                    TypeTreeViewItem typeTreeViewItem = new TypeTreeViewItem(keyValuePair.Value);

                    baseTypeTreeViewItem.Items.Add(typeTreeViewItem);

                    CreateDerivedClasses(typeTreeViewItem, classSortedList);
                }
            }
        }

        #endregion
    }
}

 

▶ MainWindow.cs

using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Threading;

namespace TestProject
{
    /// <summary>
    /// 메인 윈도우
    /// </summary>
    public class MainWindow : Window
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainWindow()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainWindow()
        {
            Width      = 800;
            Height     = 600;
            Title      = "클래스 계층도 표시하기";
            FontFamily = new FontFamily("나눔고딕코딩");
            FontSize   = 16;

            ClassHierarchyTreeView classHierarchyTreeView = new ClassHierarchyTreeView(typeof(DispatcherObject));

            Content = classHierarchyTreeView;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region 프로그램 시작하기 - Main()

        /// <summary>
        /// 프로그램 시작하기
        /// </summary>
        [STAThread]
        public static void Main()
        {
            Application application = new Application();

            application.Run(new MainWindow());
        }

        #endregion
    }
}
728x90
반응형
그리드형
Posted by 사용자 icodebroker
TAG , ,

댓글을 달아 주세요