Exemple #1
0
        /// <summary>
        /// 1.类似 _1000_UserController
        /// 2.查找RouteAttribute
        /// 3.使用常量属性ContractIdName
        /// </summary>
        public void InitContractId()
        {
            var nameArr = ControllerType.Name.Split("_", StringSplitOptions.RemoveEmptyEntries);

            if (nameArr.Length == 2)
            {
                this.ContractID = Convert.ToInt32(nameArr[0]);
                return;
            }

            var contract = ControllerType.GetCustomAttribute <RouteAttribute>();

            if (contract != null)
            {
                this.ContractID = Int32.Parse(contract.Template.Split("/", StringSplitOptions.RemoveEmptyEntries).FirstOrDefault());
                return;
            }

            var field = ControllerType.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy)
                        .Where(fi => fi.IsLiteral && !fi.IsInitOnly && fi.Name == options.ContractIdName).FirstOrDefault();

            if (field != null)
            {
                this.ContractID = Convert.ToInt32(field.GetRawConstantValue());
                return;
            }
        }