0
|
1 using System;
|
|
2 using System.Collections;
|
|
3 using System.Collections.Generic;
|
|
4 using System.ComponentModel.Design;
|
|
5 using System.Diagnostics;
|
|
6 using System.Reflection;
|
|
7 using System.Windows.Forms;
|
|
8
|
|
9 namespace BLToolkit.ComponentModel.Design
|
|
10 {
|
|
11 public partial class GetTypeDialog : Form
|
|
12 {
|
|
13 public GetTypeDialog(IServiceProvider serviceProvider, Type baseType, Predicate<Type> filter)
|
|
14 {
|
|
15 _serviceProvider = serviceProvider;
|
|
16 _baseType = baseType;
|
|
17 _filter = filter;
|
|
18
|
|
19 InitializeComponent();
|
|
20
|
|
21 LoadTypes();
|
|
22 }
|
|
23
|
|
24 private readonly IServiceProvider _serviceProvider;
|
|
25 private readonly Type _baseType;
|
|
26 private readonly Predicate<Type> _filter;
|
|
27
|
|
28 private Type _resultType;
|
|
29 public Type ResultType
|
|
30 {
|
|
31 get { return _resultType; }
|
|
32 }
|
|
33
|
|
34 delegate TypePicker.TypeNode GetTypeNode(Type t);
|
|
35
|
|
36 private void LoadTypes()
|
|
37 {
|
|
38 Cursor = Cursors.WaitCursor;
|
|
39
|
|
40 try
|
|
41 {
|
|
42 _treeView.Nodes.Clear();
|
|
43
|
|
44 var assemblyNodes = new Dictionary<Assembly, TreeNode>();
|
|
45 var namespaceNodes = new Dictionary<string, TreeNode>();
|
|
46 var typeNodes = new Dictionary<Type, TypePicker.TypeNode>();
|
|
47 var service = (ITypeDiscoveryService)_serviceProvider.GetService(typeof(ITypeDiscoveryService));
|
|
48 var cTypes = service.GetTypes(_baseType, _systemCheckBox.Checked);
|
|
49 var types = new List<Type>(cTypes.Count);
|
|
50
|
|
51 foreach (Type type in cTypes)
|
|
52 types.Add(type);
|
|
53
|
|
54 types.Sort((a, b) =>
|
|
55 a.Assembly == b.Assembly ?
|
|
56 string.Compare(a.FullName, b.FullName) :
|
|
57 string.Compare(a.Assembly.FullName, b.Assembly.FullName));
|
|
58
|
|
59 foreach (var type in types)
|
|
60 {
|
|
61 if (_filter != null && _filter(type) == false)
|
|
62 continue;
|
|
63
|
|
64 var assembly = type.Assembly;
|
|
65
|
|
66 TreeNode assemblyNode;
|
|
67
|
|
68 if (!assemblyNodes.TryGetValue(assembly, out assemblyNode))
|
|
69 {
|
|
70 assemblyNodes[assembly] = assemblyNode =
|
|
71 _treeView.Nodes.Add(assembly.FullName, assembly.GetName().Name, 1, 1);
|
|
72 }
|
|
73
|
|
74 var @namespace = type.Namespace ?? string.Empty;
|
|
75 var namespaceKey = assembly.FullName + ", " + @namespace;
|
|
76
|
|
77 TreeNode namespaceNode;
|
|
78
|
|
79 if (!namespaceNodes.TryGetValue(namespaceKey, out namespaceNode))
|
|
80 {
|
|
81 namespaceNodes[namespaceKey] = namespaceNode =
|
|
82 assemblyNode.Nodes.Add(namespaceKey, @namespace, 2, 2);
|
|
83 }
|
|
84
|
|
85 GetTypeNode getTypeNode = null; getTypeNode = t =>
|
|
86 {
|
|
87 TypePicker.TypeNode node;
|
|
88
|
|
89 if (typeNodes.TryGetValue(t, out node))
|
|
90 return node;
|
|
91
|
|
92 if (t.DeclaringType == null)
|
|
93 {
|
|
94 namespaceNode.Nodes.Add(node = new TypePicker.TypeNode(t.Name, t, false));
|
|
95 }
|
|
96 else
|
|
97 {
|
|
98 TreeNode parent = getTypeNode(t.DeclaringType);
|
|
99
|
|
100 parent.Nodes.Add(node = new TypePicker.TypeNode(t.Name, t, false));
|
|
101 }
|
|
102
|
|
103 typeNodes.Add(t, node);
|
|
104
|
|
105 return node;
|
|
106 };
|
|
107
|
|
108 getTypeNode(type).IsSelectable = true;
|
|
109 }
|
|
110 }
|
|
111 catch (Exception ex)
|
|
112 {
|
|
113 Debug.WriteLine(ex.Message);
|
|
114 }
|
|
115 finally
|
|
116 {
|
|
117 Cursor = Cursors.Default;
|
|
118 }
|
|
119 }
|
|
120
|
|
121 private void systemCheckBox_CheckedChanged(object sender, EventArgs e)
|
|
122 {
|
|
123 LoadTypes();
|
|
124 }
|
|
125
|
|
126 private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
|
|
127 {
|
|
128 var node = e.Node as TypePicker.TypeNode;
|
|
129
|
|
130 _resultType = node != null && node.IsSelectable? node.Type: null;
|
|
131
|
|
132 _okButton.Enabled = _resultType != null;
|
|
133 }
|
|
134
|
|
135 private void treeView_DoubleClick(object sender, EventArgs e)
|
|
136 {
|
|
137 _okButton.PerformClick();
|
|
138 }
|
|
139
|
|
140 }
|
|
141 } |