■ 클래스 계층도를 표시하는 방법을 보여준다.
▶ TypeTreeViewItem.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
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 } } |