Ejemplo n.º 1
0
Archivo: Use.cs Proyecto: ohomola/gears
        public override object DoRun()
        {
            object instance  = null;
            var    arguments = new List <object>();

            if (Interpreter.Language.CanParse(What.ToLower()))
            {
                instance = Interpreter.Language.ParseKeyword(What.ToLower());
            }
            else
            {
                var type = TypeRegistry.Types.FirstOrDefault(x => x.Name.ToLower() == What.ToLower());
                if (type == null && What.Contains(" "))
                {
                    var firstPart = What.Split(' ')[0];
                    arguments.Add(What.Substring(firstPart.Length + 1));
                    type = TypeRegistry.Types.FirstOrDefault(x => x.Name.ToLower() == firstPart.ToLower());
                }
                if (type == null)
                {
                    throw new ArgumentException($"{What} is not recognized.");
                }

                instance = Activator.CreateInstance(type, args: arguments.ToArray());
            }

            Data.Add(instance);

            (instance as IAutoRegistered)?.Register(Interpreter);

            if (instance is IKeyword)
            {
                Interpreter.AddToPlan(instance as IKeyword);
            }

            return(new SuccessAnswer($"Added {instance} to Data Context."));
        }