public HttpResponseMessage Get(string relationType) { Assembly asm = Assembly.Load("SocialMasterDataCommon"); string toNativeTypeName = "SocialMasterDataCommon.Models." + relationType; Type t = asm.GetType(toNativeTypeName); var writer = new JsonWriter(); writer.WriteStartArray(); foreach (var myCode in Enum.GetValues(t)) { var strName = Enum.GetName(t, myCode);//获取名称 var strVaule = Convert.ToInt64(myCode); //获取属性 var field = t.GetField(strName); var customAttribute = field.GetCustomAttributes(typeof(DescriptionAttribute), false); var description = customAttribute.Length > 0 ? ((DescriptionAttribute)customAttribute[0]).Description : strName; writer.WriteStartObject(); writer.Write("Name", strName); writer.Write("Value", strVaule); writer.Write("Description", description); writer.WriteEndObject(); } writer.WriteEndArray(); /* * { * "Description":value * } */ return this.JsonResponseJson(0, "data", writer.ToString()); } }
接口外面传一个 枚举名称解析对应的 枚举所有成员,以json 格式返回。 用反射实现 字符串 转 枚举