Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The quick solution to the CompilerException appears to be not use Evaluator to compile the assembly, but instead just <code>CSScript.LoadCode</code> like so</p> <pre><code>var compiledAssemblyName = CSScript.CompileCode(LibraryScriptCode); var evaluatorWithReference = CSScript.Evaluator.ReferenceAssembly(compiledAssemblyName); dynamic scriptInstance = evaluatorWithReference.LoadCode(ScriptCode); </code></pre> <p>However, as stated in previous answer, this limits the possibilities for dependency control that the CodeDOM model offers (like css_include). Also, any change to the LibraryScriptCode are not seen which again limits the usefulness of the Evaluator method. </p> <p>The solution I chose is the <code>AsmHelper.CreateObject</code> and <code>AsmHelper.AlignToInterface&lt;T&gt;</code> methods. This lets you use the regular css_include in your scripts, while at the same time allowing you at any time to reload the scripts by disposing the AsmHelper and starting over. My solution looks something like this:</p> <pre><code>AsmHelper asmHelper = new AsmHelper(CSScript.Compile(filePath), null, false); object obj = asmHelper.CreateObject("*"); IMyInterface instance = asmHelper.TryAlignToInterface&lt;IMyInterface&gt;(obj); // Any other interfaces you want to instantiate... ... if (instance != null) instance.MyScriptMethod(); </code></pre> <p>Once a change is detected (I use <code>FileSystemWatcher</code>), you just call <code>asmHelper.Dispose</code> and run the above code again.</p> <p>This method requires the script class to be marked with the <code>Serializable</code> attribute, or simply inherit from <code>MarshalByRefObject</code>.<br> Note that your script class does not need to inherit any interface. The AlignToInterface works both with and without it. You could use <code>dynamic</code> here, but I prefer having a strongly typed interface to avoid errors down the line.</p> <p>I couldn't get the built in <code>try</code>-methods to work, so I made this extension method for less clutter when it is not known whether or not the interface is implemented:</p> <pre><code>public static class InterfaceExtensions { public static T TryAlignToInterface&lt;T&gt;(this AsmHelper helper, object obj) where T : class { try { return helper.AlignToInterface&lt;T&gt;(obj); } catch { return null; } } } </code></pre> <p>Most of this is explained in the hosting guidelines <a href="http://www.csscript.net/help/script_hosting_guideline_.html" rel="nofollow">http://www.csscript.net/help/script_hosting_guideline_.html</a>, and there are helpful samples mentioned in previous post.</p> <p>I feel I might have missed something regarding script change detection, but this method works solidly.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload