Add a ScriptCsCatalog which can be used to add some ScriptCs scripts to a MEF container.
Simply create a new ScriptCsCatalog and use it in your MEF composition:
// You can add script by script//var catalog = new ScriptCsCatalog(new[] { "ScriptA.csx", "ScriptB.csx" });// Or an entire foldervarcatalog=newScriptCsCatalog("Scripts");varcontainer=newCompositionContainer(catalog);varbatch=newCompositionBatch();batch.AddPart(this);container.Compose(batch);Add References
It can be useful to add a references to all scripts loaded by the ScriptCsCatalog.
For example, you provide the interface IGreeter for imports in MEF.
So a basic script should be:
#r "Greeter.Contracts.dll"usingGreeter.Contracts;publicclassMyGreeter:IGreeter{
...}But you can pass the IGreeter type as reference to the ScriptCsCatalog:
// You can add script by scriptvarcatalog=newScriptCsCatalog(new[]{"ScriptA.csx","ScriptB.csx"},newScriptCsCatalogOptions{References=new[]{typeof(IGreeter)}});// Or an entire foldervarcatalog=newScriptCsCatalog("Scripts",newScriptCsCatalogOptions{References=new[]typeof(IGreeter)}});And the script will become:
publicclassMyGreeter:IGreeter{
...}Script args
It is also possible to passes arguments to the script with the ScriptArgs property of the options:
varcatalog=newScriptCsCatalog("Scripts",newScriptCsCatalogOptions{ScriptArgs=new[]{"-loglevel","INFO"}});It can be helpful with the use of Script Packs.
Script Packs
Script Packs can be used, the location varies depending on the approach used:
- script by script: the packages location is the current directory of your application
- folder: the packages location is the folder specified
Load script one by one
By default all scripts are loaded in one file to be interpreted by ScriptCS, but it can be source of issues like when alias are used.
The KeepScriptsSeparated option allow to load script one by one and remove these issues:
varcatalog=newScriptCsCatalog("Scripts",newScriptCsCatalogOptions{KeepScriptsSeparated=true});Thanks to Glenn Block Script Packs can be used!
Thanks to beolutz for the custom file system support and KeepScriptsSeparated option.