我對我的命令有Dictionary<string, object>
。所有的命令都有一個從Cmd類擴展的類。現在我嘗試撥打OnExecute
方法,但我收到了ArgumentException
。反射調用方法 - 參數異常
這是我的函數調用的方法:
public void Execute(string command, string[] args)
{
try {
Type type = commands[command].GetType();
MethodInfo method = type.GetMethod("OnExecute");
method.Invoke(commands[command], args);
}
catch (ArgumentException ex)
{
Program.Exception(ex);
} catch (TargetException ex)
{
Program.Exception(ex);
}
}
這是我Cmd的類
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Valda.Valda.Command
{
public class Cmd
{
public Cmd()
{
}
public void OnExecute(string[] args)
{
Program.WriteLine(args.ToString());
}
}
}
嘗試像新對象[] {args}作爲調用的第二個參數。它期望一個代表參數的數組。你想要第一個參數是一個數組。 – dman2306
例外說什麼? – MarcinJuraszek
@ dman2306它的工作!和23.06是我和我女朋友的約會:D –