gpt4 book ai didi

codesmith - 任何人都有选择 System.Type 实例的 UITypeEditor 吗?

转载 作者:行者123 更新时间:2023-12-04 07:04:42 25 4
gpt4 key购买 nike

我正忙于编写一个 CodeSmith 模板,它的属性之一是类型 System.Type .我希望能够使用 UI 选择程序集,加载程序集,然后显示该程序集中可用的类型。然后我可以去选择其中一种。

有没有人遇到或编写过执行此操作或类似操作的代码?

最佳答案

我手头没有任何东西,但敲一个并不是很难……最大的问题是不卸载dll的问题……但是一个粗略的例子:

(这使用字符串 AssemblyQualifiedName ,但 Type 的工作原理几乎相同 - 只需更改大约 3 行)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Design;
using System.Reflection;
using System.Windows.Forms;
using System.Windows.Forms.Design;

class MyData {
[Editor(typeof(TypeTypeEditor), typeof(UITypeEditor))]
[DisplayName("Some Type"), Description("Which type to use...")]
public string SomeType { get; set; }

[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.Run(new Form {
Controls = {
new PropertyGrid {
Dock = DockStyle.Fill,
SelectedObject = new MyData()
}
}
});
}
}

class TypeTypeEditor : UITypeEditor {
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) {
return UITypeEditorEditStyle.Modal;
}
public override object EditValue(ITypeDescriptorContext context, System.IServiceProvider provider, object value) {
IWindowsFormsEditorService svc = provider == null ? null
: provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
if (svc != null) {
using(TypeForm form = new TypeForm()) {
form.TypeName = Convert.ToString(value);
if (svc.ShowDialog(form) == DialogResult.OK) {
value = form.TypeName;
}
}
}
return value;
}
}
class TypeForm : Form {
public string TypeName { get; set; }
Button ok, load;
TreeView tree;
public TypeForm() {
Text = "Select type";
ok = new Button { Text = "OK" };
ok.Enabled = false;
ok.DialogResult = DialogResult.OK;
load = new Button { Text = "Load..." };
load.Dock = ok.Dock = DockStyle.Bottom;
this.AcceptButton = ok;
tree = new TreeView();
tree.Dock = DockStyle.Fill;
load.Click += load_Click;
Controls.Add(load);
Controls.Add(ok);
Controls.Add(tree);
tree.AfterSelect += tree_AfterSelect;
}

void tree_AfterSelect(object sender, TreeViewEventArgs e) {
ok.Enabled = false;
if (e.Node != null && e.Node.Tag != null) {
string s = Convert.ToString(e.Node.Tag);
if (!string.IsNullOrEmpty(s)) {
TypeName = s;
ok.Enabled = true;
}
}
}

void load_Click(object sender, EventArgs e) {
try {
string path = null;
using (OpenFileDialog dlg = new OpenFileDialog()) {
dlg.Filter = "dll|*.dll|exe|*.exe";
if (dlg.ShowDialog(this) == DialogResult.OK) {
path = dlg.FileName;
}
}
if (!string.IsNullOrEmpty(path)) {
Assembly asm = Assembly.LoadFrom(path);
SortedList<string, TreeNode> namespaces = new SortedList<string, TreeNode>();
foreach (Type type in asm.GetTypes()) {
if (!type.IsPublic) continue;
TreeNode nsNode;
if (!namespaces.TryGetValue(type.Namespace, out nsNode)) {
nsNode = new TreeNode(type.Namespace);
namespaces.Add(type.Namespace, nsNode);
}
nsNode.Nodes.Add(type.Name).Tag = type.AssemblyQualifiedName;
}
tree.BeginUpdate();
tree.Nodes.Clear();
try {
foreach (TreeNode node in namespaces.Values) {
tree.Nodes.Add(node);
}
}
finally {
tree.EndUpdate();
}
}
}
catch (Exception ex) {
MessageBox.Show(this, ex.Message, ex.GetType().Name, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}

关于codesmith - 任何人都有选择 System.Type 实例的 UITypeEditor 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1298643/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com