我有我的测试输入一个命令,如基本能力的控制台应用程序: 编辑
AddUser id=1 name=John surname=Doe
编辑假设的方法ADDUSER看起来是这样的:
public AddUser(int id, string name, string surname
{
//add code here to add user
}
这里是我的代码:
protected void Process(string command)
{
//get the type
Type type = this.GetType();
//do a split on the command entered
var commandStructure = command.Split(' ');
try
{
//reassign the command as only the method name
command = commandStructure[0];
//remove the command from the structure
commandStructure = commandStructure.Where(s => s != command).ToArray();
//get the method
MethodInfo method = type.GetMethod(command, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
//if the method was found
if (method != null)
{
Dictionary<string, ParameterInfo> dictionary = new Dictionary<string, ParameterInfo>();
//**Here's where I get the exception that the key was not found in the**
//**dictionary**
var parameters = method.GetParameters()
.Select(p => dictionary[p.Name])
.ToArray();
//add code here to apply values found in the command to parameters found
//in the command
//...
method.Invoke(this, parameters);
}
else
{
throw new Exception("No such command exists man! Try again.");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Prompt();
Wait();
}
}
我努力去理解乔恩斯基特提供的堆栈答案 ,但无法得到它的工作。 我想这是因为我误解他的榜样的讨论或使用。
所以我的问题是:如何获得的填充通过在命令提示符下用户输入的值的参数列表? 该方法的一部分作品,我成功地运行没有参数的方法,但是当我添加的代码来处理参数,它被认为是复杂的。