Advertisement
Guest User

Untitled

a guest
Jun 8th, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.74 KB | None | 0 0
  1.         private List<Tuple<string, MethodInfo>> Search()
  2.         {
  3.             List<Tuple<string, MethodInfo>> list = new List<Tuple<string, MethodInfo>>();
  4.  
  5.             Assembly
  6.                 .GetExecutingAssembly()
  7.                 .GetTypes()
  8.                 .Where(x => x.IsClass)
  9.                 .ToList()
  10.                 .ForEach(z =>
  11.                 z.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly)
  12.                 .ToList()
  13.                 .ForEach(x =>
  14.                 x.GetCustomAttributes()
  15.                 .ToList()
  16.                 .Where(y =>
  17.                 y is ConsoleCommandAttribute k)
  18.                 .ToList()
  19.                 .ForEach(y =>
  20.                 list.Add(new Tuple<string, MethodInfo>((y as ConsoleCommandAttribute).v.ToLower(), x)))));
  21.  
  22.             return list;
  23.         }
  24.        
  25.         private async Task ConsoleCommandHandler(string input_str)
  26.         {
  27.             string command = input_str.Split(' ')[0].ToLower();
  28.             object[] args = input_str.Split(' ').Skip(1).ToArray();
  29.  
  30.             List<Tuple<string, MethodInfo>> list = Search();
  31.  
  32.             if (!list.Any(x => x.Item1 == command))
  33.                 Console.WriteLine("Command not found.");
  34.             else
  35.             {
  36.                 if (!list.Where(x => x.Item1 == command).Any(x => x.Item2.GetParameters().Length == args.Length))
  37.                     Console.WriteLine("The input text has too few or many parameters.");
  38.                 else
  39.                 {
  40.                     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);
  41.  
  42.                     for(int i = 0; i < methods.Count(); i++)
  43.                     {
  44.                         var param = methods.ElementAt(i).Item2.GetParameters();
  45.                         for(int j = 0; j < param.Length; j++)
  46.                         {
  47.                             try
  48.                             {
  49.                                 args[j] = Convert.ChangeType(args[j], param[j].ParameterType);
  50.                             }catch(Exception e)
  51.                             {
  52.                                 Console.WriteLine(e);
  53.                             }
  54.                         }
  55.                     }
  56.                 }
  57.             }
  58.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement