gpt4 book ai didi

C# : How to design a UI that couples to a dynamically generated object?

转载 作者:太空宇宙 更新时间:2023-11-03 13:52:31 24 4
gpt4 key购买 nike

我在工作中开发的软件有问题。在用户执行以下步骤后,我正在创建一个 dll 文件。

  1. 将某些参数和计算代码指定为win形式。
  2. 指定 dll 的名称。

完成此操作后,我将创建所有必要的代码文件(使用 codeDOM)并编译源文件以生成 dll。

现在,我的问题是用户界面。我想在我的 UI 中的 dll 对象中显示参数,但我不知道用户要添加什么参数。

我想要一个系统,我可以在其中指定配置文件来耦合 UI 元素(我事先知道)和 dll 中的对象(我事先不知道,除了我可以从中收集的信息使用反射)。

因此,实际上,我想将 UI 元素(label.text 等)之间的耦合带到我的代码之外,也许是一个 xml 文件,我的 UI 应该使用这个 xml 文件来填充来自内部对象的数据动态加载的 dll。

请帮忙。

提前致谢。

最佳答案

这是一个简短的代码片段,可以帮助您入门:

Assembly asm = Assembly.LoadFrom("generated_asm.dll");
// or if the assembly is already loaded:
// asm = AppDomain.CurrentDomain.GetAssemblies().First(a => a.GetName().Name == "Generated.Assembly");

var type = asm.GetType("InsertNamespaceHere.InsertTypeNameHere");

// creates a table layout which you can add to a form (preferable you use the designer to create this)
var tbl = new TableLayoutPanel { ColumnCount = 2 };

// enumerate the public properties of the type
foreach(var property in type.GetProperties())
{
tbl.Add(new Label(property.Name));

var input = new TextBox { Tag = property };
input.TextChanged = this.HandleTextChanged;
input.Enabled = property.CanWrite;

tbl.Add(input);
}

在处理程序中你可以使用这个:

void HandleTextChanged(object source, ...) {
var input = source as TextBox;
var property = input.Tag as PropertyInfo;
property.GetSetMethod().Invoke(this.instanceOfThatType, new object[] { Convert.ChangeType(input.Text, property.PropertyType) });
}

希望这有帮助:)

关于C# : How to design a UI that couples to a dynamically generated object?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13302362/

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