This project has moved. For the latest updates, please go here.

Loading a script from a custom source

If the script you want to execute is not in a file and you do not want to load it into a string, you can create a custom ScriptSource. The following example shows how it is done:

/// <summary>
/// Represents a string containing script code.
/// </summary>
public class StringScriptSource : ScriptSource
{
    private string code;

    /// <summary>
    /// Creates a new StringScriptSource instance.
    /// </summary>
    /// <param name="code"> The script code. </param>
    public StringScriptSource(string code)
    {
        if (code == null)
            throw new ArgumentNullException("code");
        this.code = code;
    }

    /// <summary>
    /// Gets the path of the source file (either a path on the file system or a URL).  This
    /// can be <c>null</c> if no path is available.
    /// </summary>
    public override string Path
    {
        get { return null; }
    }

    /// <summary>
    /// Returns a reader that can be used to read the source code for the script.
    /// </summary>
    /// <returns> A reader that can be used to read the source code for the script, positioned
    /// at the start of the source code. </returns>
    /// <remarks> If this method is called multiple times then each reader must return the
    /// same source code. </remarks>
    public override TextReader GetReader()
    {
        return new StringReader(this.code);
    }
}


Next tutorial: Threading and concurrency

Last edited Aug 25, 2010 at 1:47 PM by paulbartrum, version 4

Comments

No comments yet.