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  格式返回。  用反射实现  字符串  转 枚举 

posted on 2016-03-25 14:44  FlowLight  阅读(198)  评论(0编辑  收藏  举报