Microsoft.Scripting.Hosting.ScriptRuntime.UseFile C# (CSharp) Méthode

UseFile() public méthode

path is null file extension does not map to language engine language does not have any search paths file does exist in language's search path
public UseFile ( string path ) : ScriptScope
path string
Résultat ScriptScope
        public ScriptScope UseFile(string path) {
            ContractUtils.RequiresNotEmpty(path, "path");
            string extension = Path.GetExtension(path);

            ScriptEngine engine;
            if (!TryGetEngineByFileExtension(extension, out engine)) {
                throw new ArgumentException(string.Format("File extension '{0}' is not associated with any language.", extension));
            }

            var searchPaths = engine.GetSearchPaths();
            if (searchPaths.Count == 0) {
                throw new InvalidOperationException(string.Format("No search paths defined for language '{0}'", engine.Setup.DisplayName));
            }

            // See if the file is already loaded, if so return the scope
            foreach (string searchPath in searchPaths) {
                string filePath = Path.Combine(searchPath, path);
                ScriptScope scope = engine.GetScope(filePath);
                if (scope != null) {
                    return scope;
                }
            }

            // Find the file on disk, then load it
            foreach (string searchPath in searchPaths) {
                string filePath = Path.Combine(searchPath, path);
                if (_manager.Platform.FileExists(filePath)) {
                    return ExecuteFile(filePath);
                }
            }

            // Didn't find the file, throw
            string allPaths = searchPaths.Aggregate((x, y) => x + ", " + y);
            throw new FileNotFoundException(string.Format("File '{0}' not found in language's search path: {1}", path, allPaths));
        }

Usage Example

        private void UpdateScript(string script)
        {
            LogTo.Debug("A script was changed - {0}", script);

            if (File.Exists(script))
            {
                _runtime.Shutdown();
                _runtime = Python.CreateRuntime();
                try
                {
                    _script = _runtime.UseFile(script);
                    LogTo.Info("Script loaded");
                    ScriptScope scope = _script;
                    dynamic parser = scope.GetVariable("ParserScript");
                    name = parser.Name;
                    version = parser.Version;
                    author = parser.Author;
                    LogTo.Info("Name: {0}, Version: {1}, Author: {2}", name, version, author);
                }
                catch (SyntaxErrorException e)
                {
                    LogTo.ErrorException("Syntax error", e);
                }
                catch (Exception e)
                {
                    LogTo.WarnException("Script error", e);
                }
            }
            else
            {
                LogTo.Error("Script not loaded (file doesn't exist)");
            }
        }
All Usage Examples Of Microsoft.Scripting.Hosting.ScriptRuntime::UseFile