예제 #1
0
        public void Assert(AppContext appContext)
        {
            if (ValidateModelAttributeType == null)
            {
                throw new ArgumentNullException(nameof(ValidateModelAttributeType));
            }

            var controllerTypes = ControllerUtil.GetControllers(appContext);

            foreach (var controllerType in controllerTypes)
            {
                var actions = ControllerUtil.GetControllerActions(appContext, controllerType);
                foreach (var action in actions)
                {
                    var hasModel = action.GetParameters().Any(p =>
                    {
                        var tInfo = p.ParameterType.GetTypeInfo();
                        return(p.ParameterType != typeof(string) && tInfo.IsClass);
                    });
                    if (hasModel && action.GetCustomAttribute(ValidateModelAttributeType) == null)
                    {
                        throw new InvalidProgramException($"Action {controllerType.FullName}.{action.Name} 包含Model类型参数,但未指定ValidateModel属性");
                    }
                }
            }
        }