.Net/C# 反射——填充组合框

发布于 2024-11-06 18:53:17 字数 764 浏览 1 评论 0原文

大家好,

我有一个通过反思获得的满足特定标准的“类型”列表。每种类型都是用户在运行时可能选择的不同功能。如果我稍后添加更多子类,这种动态实现将让我不必记住更新用户控件,这就是这里的想法。

类型列表很好,但最好能显示比代码中编写的名称更有意义的内容。例如,我想显示“RacingBikeDesigner”,而不是“RacingBikeDesigner”,甚至可能显示与该类型关联的其他属性,例如“描述”,以便用户知道该特定选择的作用。

所以我想问题是,给定一个类型,我如何向用户提供更有意义的表示?我可以向每个子类添加一个静态字段并从类型中调用它,或者我可以以某种方式使用类型转换器吗?

用户控件(ListBox、ComboBox 等)绑定到下面的返回值,但这对用户不友好:

List<string> LeftHandedUserChoices = new List<string>();
Type[] AllTypesInThisAssembly = Assembly.GetAssembly(typeof(UserChoices)).GetTypes();
foreach (Type _currentType in AllTypesInThisAssembly)
    if (_currentType.IsSubclassOf(typeof(UserChoices)))
        LeftHandedUserChoices.Add(_currentType.Name);
return LeftHandedUserChoices;

干杯, 问

Greetings all,

I have a list of "Types" meeting a certain critera that I obtained through reflection. Each of the types is a different feature that the user will potentially choose at runtime. If I add more subclasses later, this dynamic implementation would save my having to remember to update the user control is the idea here.

The list of types is nice, but it'd be nice to display something more meaningful than the Name as it's written in code. For example, instead of "RacingBikeDesigner", I'd like to display "Racing Bike Designer", and maybe even display other properties associated with that type like "Description" so that the user knows what that particular choice does.

So I guess the question is, given a Type, how can I provide a more meaningful representation to the user? Could I maybe add a static field to each subclass and call that from the Type, or could I perhaps use a type converter somehow?

The user control (ListBox, ComboBox, etc) is bound to the return value below, but it's not user-friendly:

List<string> LeftHandedUserChoices = new List<string>();
Type[] AllTypesInThisAssembly = Assembly.GetAssembly(typeof(UserChoices)).GetTypes();
foreach (Type _currentType in AllTypesInThisAssembly)
    if (_currentType.IsSubclassOf(typeof(UserChoices)))
        LeftHandedUserChoices.Add(_currentType.Name);
return LeftHandedUserChoices;

Cheers,
Q

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦冥 2024-11-13 18:53:17

您有多种选择来执行此操作。您可以使用类型上的属性作为描述,或者将其放入类型上的静态字段/属性中并使用反射检索它。

如果本地化是一个问题,您可能需要存储资源字符串名称,并在 runtme 上显示资源值。

You have a couple of options for doing this. You could use an attribute on your type for the description, or put it in a static field/property on the Type and retrieve that using reflection.

If localization is an issue, you will probably want to store the resource string name, and display the resource value at runtme.

绝情姑娘 2024-11-13 18:53:17

向您的类型添加自定义 C# 属性

Add custom C# Attributes to your types.

机场等船 2024-11-13 18:53:17

一种方法是根据您正在使用的命名约定来解析类名(在您的情况下看起来像 Pascal)。例如,RacingBikeDesigner 将成为Racing Bike Designer。这是一个解析示例

One method is for you to parse class names based on the naming convention you are using (looks like Pascal in your case). For instance RacingBikeDesigner will become Racing Bike Designer. Here is a parsing example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文