可以将所有枚举值序列化为JSON:
private void ExportEnum<T>()
{
var type = typeof(T);
var values = Enum.GetValues(type).Cast<T>();
var dict = values.ToDictionary(e => e.ToString(), e => Convert.ToInt32(e));
var json = new JavaScriptSerializer().Serialize(dict);
var script = string.Format("{0}={1};", type.Name, json);
System.Web.UI.ScriptManager.RegisterStartupScript(this, GetType(), "CloseLightbox", script, true);
}
ExportEnum<MyEnum>();
这注册了如下脚本:
MyEnum={"Red":1,"Green":2,"Blue":3};