コード例 #1
0
        internal LoaderScriptExtension(string path)
            : base(path)
        {
            _script    = File.ReadAllText(Path);
            _usings    = new Usings();
            _commands  = new List <ScriptedCommand>();
            _imports   = new List <AssemblyName>();
            _container = new ScriptedCommandContainer(this);

            ExtensionStore.InitExtension(this);

            Description = System.IO.Path.GetFileName(Path);
            Load();

            foreach (var command in _commands)
            {
                var lcInfo = new LoaderCommand(this, command);
                command.LoaderCommand = lcInfo;
                AddItem(lcInfo);
            }

            var lccInfo = new LoaderCommandContainer(this, _container, typeof(ScriptedCommandContainer));

            ExtensionStore.InitCommandContainer(lccInfo, Items.Cast <LoaderCommand>());
            AddItem(lccInfo);
        }
コード例 #2
0
        void LoadCommandContainer(TypeDescriptor containerType, AssemblyDescriber describer)
        {
            if (containerType.IsAbstract || !containerType.HasPublicParameterlessConstructor)
            {
                return;
            }

            var methods = describer.GetPublicMethods(containerType);

            var cmdMethods = (from method in methods
                              let commandAttr = method.APIAttributes.OfType <CommandAttribute>().FirstOrDefault()
                                                where commandAttr != null
                                                select new { method, commandAttr }).ToArray();

            if (cmdMethods.Length == 0)
            {
                return;
            }

            var dupeCommandNames =
                from i in cmdMethods
                group i by i.commandAttr.Name
                into g
                where g.Count() > 1
                select g;

            if (dupeCommandNames.Any())
            {
                throw new LoaderItemInitException(string.Format("Duplicate command names in container {0}", containerType));
            }

            var container    = CreateLoaderInstance <CommandContainer>(containerType);
            var commandInfos = new List <LoaderCommand>();

            foreach (var method in cmdMethods)
            {
                try
                {
                    var reflectedCmd = new ReflectedCommand(container, method.method, method.commandAttr);
                    var lcInfo       = new LoaderCommand(this, reflectedCmd);
                    reflectedCmd.LoaderCommand = lcInfo;
                    commandInfos.Add(lcInfo);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Could not initialize command {0} in container {1}: {2}", method.method.Name, containerType, ex.Message);
                }
            }

            var lccInfo = new LoaderCommandContainer(this, container, containerType);

            ExtensionStore.InitCommandContainer(lccInfo, commandInfos);
            container.Initialize(Hooks);
            AddItems(commandInfos);
            AddItem(lccInfo);
        }