[英]Passing a parameter into linq expression?
I have the following code which converts ac# enum to javascript object but the types are invisible to the code and cause a compile time error. 我有以下代码将ac#enum转换为javascript对象,但这些类型对代码是不可见的,并导致编译时错误。 the types inside the stars are the problem, I guess that I need to pass the type into the linq expression but Im stock, does any one know how to do this?
星星内部的类型是问题,我想我需要将类型传递给linq表达式,但我是股票,有没有人知道如何做到这一点?
public static string EnumToJsObj(Enum enumType)
{
Type type = enumType.GetType();
string strJS = string.Format("{{{0}}} ",
string.Join(", ", Enum.GetNames(type).ToList().ConvertAll(key =>
{
return string.Format("{0} : {1}", key, (int)((***type***)Enum.Parse(typeof(***type***), key)));
}).ToArray()));
}
Something like this? 像这样的东西?
public static string EnumToJsObj(Enum enumType)
{
Type t = enumType.GetType();
return new JavaScriptSerializer().Serialize(
Enum.GetNames(t).ToDictionary(e => e, e => (int)Enum.Parse(t,e))
);
}
You can either make your method to be a generic method: 您可以使您的方法成为通用方法:
public static string EnumToJsObj<T>(Enum enumType){ ... }
or you might be able to get away with using System.Convert.ChangeType
instead of type casting. 或者您可以使用
System.Convert.ChangeType
而不是类型转换来逃避。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.