Beispiel #1
0
        /// <inheritdoc />
        public virtual void Apply(ConventionContext context)
        {
            var modelAccessor = context.ModelAccessor;

            if (context.ModelType == null || modelAccessor == null)
            {
                return;
            }

            const BindingFlags MethodFlags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;

            var method = context.ModelType
                         .GetTypeInfo()
                         .GetMethod("OnValidationError", MethodFlags);

            if (method == null)
            {
                return;
            }

            context.Application.ValidationErrorHandler = (v) =>
            {
                var arguments = ReflectionHelper.BindParameters(method, context.Application, default);
                var result    = method.Invoke(modelAccessor.GetModel(), arguments);
                if (method.ReturnType == typeof(int))
                {
                    return((int)result);
                }

                return(CommandLineApplication.ValidationErrorExitCode);
            };
        }
Beispiel #2
0
        private async Task <int> OnExecute(ConventionContext context)
        {
            const BindingFlags binding = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;

            var        typeInfo = context.ModelType.GetTypeInfo();
            MethodInfo?method;
            MethodInfo?asyncMethod;

            try
            {
                method      = typeInfo.GetMethod("OnExecute", binding);
                asyncMethod = typeInfo.GetMethod("OnExecuteAsync", binding);
            }
            catch (AmbiguousMatchException ex)
            {
                throw new InvalidOperationException(Strings.AmbiguousOnExecuteMethod, ex);
            }

            if (method != null && asyncMethod != null)
            {
                throw new InvalidOperationException(Strings.AmbiguousOnExecuteMethod);
            }

            method ??= asyncMethod;

            if (method == null)
            {
                throw new InvalidOperationException(Strings.NoOnExecuteMethodFound);
            }

            var arguments     = ReflectionHelper.BindParameters(method, context.Application);
            var modelAccessor = context.ModelAccessor;

            if (modelAccessor == null)
            {
                throw new InvalidOperationException(Strings.ConventionRequiresModel);
            }
            var model = modelAccessor.GetModel();

            if (method.ReturnType == typeof(Task) || method.ReturnType == typeof(Task <int>))
            {
                return(await InvokeAsync(method, model, arguments));
            }
            else if (method.ReturnType == typeof(void) || method.ReturnType == typeof(int))
            {
                return(Invoke(method, model, arguments));
            }

            throw new InvalidOperationException(Strings.InvalidOnExecuteReturnType(method.Name));
        }