0
我有以下代码:我怎么能在现场命名在C#添加到KeyValuePair
var values1 = (EReferenceKey[])Enum.GetValues(typeof(EReferenceKey));
var valuesWithNames = values1.Select(
value => new {
Value = ((int)value).ToString("00"),
Text = Regex.Replace(value.ToString(), "([A-Z])", " $1").Trim()
});
下面是一些代码,建议上计算器,可以使这种方法一般:
public static IEnumerable<KeyValuePair<string, string>> GetValues2<T>() where T : struct {
var t = typeof(T);
if (!t.IsEnum)
throw new ArgumentException("Not an enum type");
return Enum.GetValues(t)
.Cast<T>()
.Select(x => new KeyValuePair<string, string>(
((int)Enum.ToObject(t, x)).ToString("00"),
Regex.Replace(x.ToString(), "([A-Z])", " $1").Trim()
));
}
它给了我几乎相同的结果,但它缺少名称“值”和“文本”。有人可以向我建议我如何修改后面的代码来添加这些代码,并仍然按照顺序返回结果?
我曾尝试做自己,但是当我试图在添加“VALUE =”它给了我错误的“文本=”以选择通用的:
错误6名“值”呢在目前情况下不存在