Advertisement
fabis_sparks

cmdline

Jan 20th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.30 KB | None | 0 0
  1.  public object CMDLine(string[] args, string nameClass)
  2.         {
  3.             Type t = Type.GetType(NS + "." + nameClass);
  4.             object obj = Activator.CreateInstance(t);
  5.             foreach (FieldInfo fi in t.GetFields())
  6.             {
  7.                 var parameter = fi.GetCustomAttributes(typeof(CommandLineAttribute), false);
  8.                 foreach (var par in parameter.Cast<CommandLineAttribute>())
  9.                 {
  10.                     string s1 = fi.FieldType.Name;
  11.                     string s = "-" + par.CommandSwitch + "=";
  12.                     string st;
  13.                     foreach (string arg in args)
  14.                     {
  15.                         if (arg.Contains(s))
  16.                         {
  17.                             int n = arg.IndexOf(s);
  18.                             st = arg.Remove(n, s.Length);
  19.                             var result = Convert.ChangeType(st, fi.FieldType);
  20.                             fi.SetValue(obj, result);
  21.                         }
  22.                     }
  23.                 }
  24.             }
  25.             return obj;
  26.         }
  27.     }
  28.     class CommandLineAttribute : Attribute
  29.     {
  30.         public string CommandSwitch;
  31.         public CommandLineAttribute(string CommandSwitch)
  32.         {
  33.             this.CommandSwitch = CommandSwitch;
  34.         }
  35.  
  36.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement