/// <summary> /// Namespace to use "System", "System.IO", ... /// </summary> /// <param name="scriptHandler"></param> /// <param name="usings"></param> /// <returns></returns> public static ScriptHandler AddUsings(this ScriptHandler scriptHandler, params string[] usings) { using (scriptHandler._scriptStateLock.Lock()) { if (scriptHandler._scriptState != null) { throw new NotSupportedException($"Cannot add usings after script was already initialized, call ExecuteCode(\"using {usings.FirstOrDefault()};\") instead"); } } scriptHandler._usings.AddRange( usings .Except(scriptHandler._usings) .Distinct() .ToArray() ); return(scriptHandler); }
/// <summary> /// Add assemblies to use with the ScriptHandler. /// Typically : typeof(string).Assembly, myInstance.GetType().Assembly /// </summary> /// <param name="model"></param> /// <param name="references"></param> /// <returns></returns> public static ScriptHandler AddReferences(this ScriptHandler model, params Assembly[] references) { using (model._scriptStateLock.Lock()) { if (model._scriptState != null) { throw new NotSupportedException("Cannot add reference after script was already initialized"); } } model._references.AddRange( references .Except(model._references) .Distinct() .ToArray() ); return(model); }
/// <summary> /// Add assembly and optionally the referenced assembly. Can be use calling AddReferences(Assembly.GetExecutingAssembly(), true), /// this will take the full pack of the currently executing software assemblies /// </summary> /// <param name="scriptHandler"></param> /// <param name="reference"></param> /// <param name="includeReferencedAssemblies"></param> /// <returns></returns> public static ScriptHandler AddReferences(this ScriptHandler scriptHandler, Assembly reference, bool includeReferencedAssemblies = false) { using (scriptHandler._scriptStateLock.Lock()) { if (scriptHandler._scriptState != null) { throw new NotSupportedException("Cannot add reference after script was already initialized"); } } scriptHandler._references.AddRange( (includeReferencedAssemblies ? reference.GetReferencedAssemblies() .Select(a => Assembly.Load(a)) .Concat(reference.Yield()) : reference.Yield()) .Except(scriptHandler._references) .Distinct() .ToArray() ); return(scriptHandler); }
/// <summary> /// Add global instances to the script /// Typically : (nameof(MyInstance), MyInstance), ("SomeString", "Hello World"), ... /// </summary> /// <param name="scriptHandler"></param> /// <param name="globals"></param> /// <returns></returns> public static ScriptHandler AddGlobals(this ScriptHandler scriptHandler, params (string name, object value)[] globals)