Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private List<Tuple<string, MethodInfo>> Search()
- {
- List<Tuple<string, MethodInfo>> list = new List<Tuple<string, MethodInfo>>();
- Assembly
- .GetExecutingAssembly()
- .GetTypes()
- .Where(x => x.IsClass)
- .ToList()
- .ForEach(z =>
- z.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly)
- .ToList()
- .ForEach(x =>
- x.GetCustomAttributes()
- .ToList()
- .Where(y =>
- y is ConsoleCommandAttribute k)
- .ToList()
- .ForEach(y =>
- list.Add(new Tuple<string, MethodInfo>((y as ConsoleCommandAttribute).v.ToLower(), x)))));
- return list;
- }
- private async Task ConsoleCommandHandler(string input_str)
- {
- string command = input_str.Split(' ')[0].ToLower();
- object[] args = input_str.Split(' ').Skip(1).ToArray();
- List<Tuple<string, MethodInfo>> list = Search();
- if (!list.Any(x => x.Item1 == command))
- Console.WriteLine("Command not found.");
- else
- {
- if (!list.Where(x => x.Item1 == command).Any(x => x.Item2.GetParameters().Length == args.Length))
- Console.WriteLine("The input text has too few or many parameters.");
- else
- {
- var methods = list.Where(x => x.Item1 == command && x.Item2.GetParameters().Count(y => !(y.IsOptional || y.HasDefaultValue)) <= args.Length && x.Item2.GetParameters().Length >= args.Length);
- for(int i = 0; i < methods.Count(); i++)
- {
- var param = methods.ElementAt(i).Item2.GetParameters();
- for(int j = 0; j < param.Length; j++)
- {
- try
- {
- args[j] = Convert.ChangeType(args[j], param[j].ParameterType);
- }catch(Exception e)
- {
- Console.WriteLine(e);
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement