Tools like dotnet-script and CSI allow users to write, compile, and run C# like "scripts" rather than including their code in a complete pre-compiled project. These tools work great for command-line usage, but don't offer much in terms of integrating dynamic C# "scripts" into a larger C# application.
If I have an existing C# application which wishes to load additional classes into its existing namespaces via .csx "scripts", how do I do that? Is it possible?
I guess you need to compile and execute your C# script.
In my experience, I used C# scripts by referencing
Microsoft.CodeAnalysis.CSharp.Scripting(version 3.*) directly.Compilation
I suggest to use default compilation options:
Maybe in the future, you'll need to add referenced assemblies to your script compilation.
So you need to compile your
script(contained in a string variable in the code below):Execution
Obviously you need an entry point to execute your script. I found out this trickly solution. It works.
I hope it can help.