2

Is there a way that I can add a default using directive to ScriptCS? So when I start ScriptCS I do not need to include the namespace for classes in that namespace.

For example, I may need to interact with the Process class within the System.Diagnostics namespace a lot. If I want to create a new instance of this class, I need to do

var proc = new System.Diagnostics.Process();

I would rather do

var proc = new Process();

I know that I can add the using statement each time I start ScriptCS, but I would like it to always be there.

Shawn Kendrot
  • 12,425
  • 1
  • 25
  • 41
  • 1
    [`using` *directive*](http://msdn.microsoft.com/en-us/library/sf0df423.aspx), not [`using` *statement*](http://msdn.microsoft.com/en-us/library/yh598w02.aspx). `using` statements's in C# are syntactic sugar for a `try`/`catch` block that calls `Dispose` on a disposable object. – Damien_The_Unbeliever Sep 23 '13 at 06:33
  • thanks, I made the correction – Shawn Kendrot Sep 24 '13 at 05:05

3 Answers3

3

You can add additional assembly references and using statements into your en and by writing a Script Pack. You can see an example of injecting using statements in the Web API example.

Shawn Kendrot
  • 12,425
  • 1
  • 25
  • 41
Justin Rusbatch
  • 3,992
  • 2
  • 25
  • 43
2

A script pack can do this as Justin mentioned. An alternative option would be to use global modules, example here (https://github.com/scriptcs-contrib/scriptcs-sample-module) however currently they don't support this nicely. If the module had a hook for getting all the services after they were created, you could do this.

I filed this bug (https://github.com/scriptcs/scriptcs/issues/472) to allow addressing this in a much cleaner fashion.

Glenn Block
  • 8,463
  • 1
  • 32
  • 34
0

Looks like this can be accomplished by modifying the DefaultNamespaces property of the ScriptExecutor class to include the namespace you want to always have.

You would need to download the source (or fork a new branch in github), modify and compile yourself. You would then put the new ScriptCs.Core assembly in the install folder (C:\Users\USER_NAME\AppData\Roaming\scriptcs). This would replace the existing assembly. If you do this you will lose any changes if you are updating through chocolatey

Shawn Kendrot
  • 12,425
  • 1
  • 25
  • 41