【发布时间】:2020-01-20 02:21:44
【问题描述】:
我正在阅读 app.config 条目
<add key="ClassNameSpace.ClassName" value="http://xxxx/xxx.asmx"/>
我正在尝试获取密钥的类型
var section = configuration.GetSection(sectionKey.ToString());
var appSettings = section as AppSettingsSection;
if (appSettings == null) continue;
foreach (var key in appSettings.Settings.AllKeys)
{
System.Type type = System.Type.GetType(typeof(key).AssemblyQualifiedName);
var webService = new SecureWebService<type>().Service;
}
但我遇到了错误
'key' 是一个变量,但用作类型
解决这个问题的任何想法
【问题讨论】:
-
你不能用变量调用
typeof,你需要用一个类型来调用它(这就是警告试图告诉你的) typeof(int) 是有效的。您可以使用 key 调用 GetType 来获取类型实例。 -
没有人读取异常消息 :( "'key' 是一个变量,但用作类型" 表示
key是一个变量,但你可以像这样使用它typeof(key)中的一个类型 -
这个“appSettings.Settings.AllKeys”的类型是什么
-
该键的类型可能只是“字符串”。我认为您希望该键的值获得新类型
-
所以你的问题是如何通过类名获取类型? stackoverflow.com/questions/11107536/…
标签: c#