动态创券反射类
卡券的.net 主要分三个流程 { 创券 , 领券, 核券}
其中创券最复杂,并可扩展高级设置券:如会员卡,电影票,门票等。
先阐述卡券基本券,并在后期更新扩展。
卡券在创券贴代码:
1 public JsonResult GetCardType(String ObjType)
2 {
3 Type t = Assembly.Load("XXX").GetType("XXX.Entities." + ObjType);
4 var dataList = new List<object>();
5 foreach (var value in Enum.GetValues(t))
6 {
7
8 object[] objAttrs = value.GetType().GetField(value.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true);
9 DescriptionAttribute descAttr = objAttrs[0] as DescriptionAttribute;
10 var date = new
11 {
12 Description = descAttr.Description,
13 Name = value.ToString(),
14 value = Convert.ToInt32(value)
15 };
16 dataList.Add(date);
17 }
18 return Json(dataList);
19 }
编译前工具准备工作:
/// <summary> /// 动态创建卡券种类 /// </summary> /// <param name="cardType">case 扩展种类</param> /// <returns></returns> public static object CardCreate(String cardType) { object date = null; CardType _cardType = (CardType)Enum.Parse(typeof(CardType), cardType, true); switch (_cardType) { case CardType.GENERAL_COUPON: date = new Card_GeneralCouponData(); break; case CardType.GROUPON: date = new Card_GrouponData(); break; case CardType.DISCOUNT: date = new Card_DisCountData(); break; case CardType.GIFT: date = new Card_GiftData(); break; case CardType.CASH: date = new Card_CashData(); break; //case CardType.MEMBER_CARD: // date = new Card_MemberCardData(); // break; default: date = null; break;
//自行再扩展新的卡券种类 } return date; } /// <summary> /// 动态反射字段 /// </summary> /// <param name="obj"></param> /// <returns></returns> public static IEnumerable Pro(object obj) { var proInfoList = new List<object>(); Type t = obj.GetType(); foreach (PropertyInfo pi in t.GetProperties()) { object[] a1 = pi.GetCustomAttributes(typeof(DisplayAttribute), true); if (pi.PropertyType.Name != "Card_BaseInfoBase") { var proInfo = new { Name = pi.Name, ProType = pi.PropertyType.Name, Display = ((DisplayAttribute)a1[0]).Name, }; proInfoList.Add(proInfo); } } return proInfoList; }
其中用到几个反射,枚举描述反射和卡券类的反射。自行扩展字段可以借鉴
https://github.com/JeffreySu/WeiXinMPSDK下载。 稍作修改即可使用。(用好继承类更方便)
微信卡券的坑太多,慢慢填满。开发文档用字不严谨害人不浅啊!
微信创券开发文档:https://mp.weixin.qq.com/wiki/15/e33671f4ef511b77755142b37502928f.html