-1

我在 ac# 应用程序的设置中设置了一些属性。我正在使用它们来存储字符串。我的问题是我想根据类的名称变量来调用它们。

所以我想做这样的事情

Description = Properties.Settings.Default.(name + "Description");

这可能吗?还是我必须根据 name 属性编写一个讨厌的 switch 语句?

Swith(name)
case:name1 
 Description = properties.settings.default.name1Descrition;
4

1 回答 1

2

假设你在谈论 Winforms,Properties.Settings.Default是一个可索引的集合,所以你只需要按键获取它:

var Description = Properties.Settings.Default[name + "Description"] as string;

您不需要使用反射,无论哪种方式都可以检索。你的第一个猜测很接近。

使用索引器获取值始终返回 an object,因此您需要将其转换为最终类型 (the as string) 或该属性的任何类型。

于 2018-04-15T20:47:39.747 回答