public void ChangeParameterTest()
        {
            MethodTests         obj        = new MethodTests();
            Type                methodType = typeof(MethodTests);
            MethodInfo          method     = methodType.GetMethod(nameof(MethodTests.ArraySize));
            object              ret;
            MethodInvokeContext context = ObjectFactory.CreateInvokeContext(method);

            ret = context.Invoke(obj);
            Assert.AreEqual(0, ret);

            context.Arguments[0] = new int[] { 1, 2, 3, 4 };
            ret = context.Invoke(obj);
            Assert.AreEqual(4, ret);

            ObjectFactory.ValueMatching += OnValueMatching;
            DirectoryInfo val = (DirectoryInfo)ObjectFactory.InvokeMethod(obj, nameof(obj.TestChange), "D:\\");

            Assert.AreEqual("D:\\", val.FullName);
        }
Example #2
0
        public Task Invoke(IHostContext context, Func <Task> next,
                           IServiceProvider services, ICommandProvider commandProvider, IEnvironment env, IOutputEngine outputEngine, IHostInput hostInput)
        {
            IInput input = context.Command;

            if (input.Name == "help")
            {
                return(ProcessWithHelp(input, context, commandProvider));
            }

            CommandRecord command = commandProvider.MatchCommand(context);

            if (command == null)
            {
                return(next());
            }

            MethodInfo methodInfo = command.CommandEntry;

            ParameterInfo[] parameters  = methodInfo.GetParameters();
            List <int>      pathIndices = new List <int>();

            foreach (var parameter in parameters)
            {
                if (parameter.GetCustomAttribute <PathAttribute>() != null)
                {
                    pathIndices.Add(parameter.Position);
                }
            }

            try
            {
                CommandSet instance = (CommandSet)ObjectFactory.CreateInstance(command.InstanceType, services, input.Options, input.Arguments);
                instance.Command      = input;
                instance.Context      = context;
                instance.InputObject  = context.InputObject;
                instance.OutputEngine = outputEngine;
                instance.HostInput    = hostInput;
                instance.Environment  = env;
                object value;
                if (pathIndices.Count <= 0)
                {
                    value = ObjectFactory.InvokeMethod(instance, methodInfo, services, input.Options, input.Arguments);
                }
                else
                {
                    MethodInvokeContext invokeContext = ObjectFactory.CreateInvokeContext(methodInfo, services, input.Options, input.Arguments);
                    string currentPath = env.WorkingDirectory.FullName;
                    ProcessPathAttribute(pathIndices, parameters, invokeContext.Arguments, currentPath);
                    value = invokeContext.Invoke(instance);
                }
                if (command.CommandEntry.ReturnType != typeof(void))
                {
                    context.SetResult(value);
                }
            }
            catch (Exception ex)
            {
                context.Exception = ex;
            }
            return(Task.CompletedTask);
        }