【发布时间】:2013-06-26 01:15:46
【问题描述】:
IDictionary<string, string> map = str.Split('|')
.ToDictionary(s => s.Split('@')[0], s => s.Split('@')[1]);
上述语句有效。但我想将其更改为通用 IDictionary
public class CSVMap <TKey, TValue>
{
public IDictionary<TKey, TValue> func (string str)
{
IDictionary<TKey, TValue> map = str.Split('|').ToDictionary (ConvertValue<TKey>(s => s.Split('@')[0]), ConvertValue<TValue>(s => s.Split('@')[1]));
}
public static T ConvertValue<T>(string value)
{
return (T)Convert.ChangeType(value, typeof(T));
}
和ConvertValue 将拆分字符串转换为TKey 和TValue 的类型。
但ConvertValue 部分出现这些错误:
错误 CS1660:无法将 lambda 表达式转换为类型“字符串”,因为它不是委托类型
错误 CS1660:无法将 lambda 表达式转换为类型“字符串”,因为它不是委托类型
我不确定错误的含义或如何解决此类问题。
【问题讨论】:
-
正确的术语是“泛型”,而不是“模板”。模板是 C++ 所拥有的。
-
@JohnSaunders: .. 将其更改为 template for IDictionary