Use Script# to compile code (stand-alone)

可紊 提交于 2019-12-06 06:03:30

Some things I see potentially problematic.

  1. TemplateFile is set to a c# code stream. Leave it unset, since that is not a valid template.
  2. References should include the script# mscorlib, and furthermore, only full paths to valid script# assemblies. System.dll is not a script# assembly.
  3. Before you read from the MemoryStream, you need to set the stream position back to the start, otherwise it is at the end after the compiler has written to it, and there is nothing more to read.
  4. Not seeing a call to Compile on the Compiler instance you created, passing in the options instance. My guess is you did do that, just not there in the stack overflow snippet.

You probably should also implement IErrorHandler and pass that to the compiler to get error messages should they occur, once you have the basic thing working.

For reference you can also look at the unit tests at https://github.com/nikhilk/scriptsharp/tree/master/tests/ScriptSharp/Core which does something similar.

Note that you'll need a valid c# source file, rather than a single standalone expression. You can however likely deal with that by stripping off stuff from the start and end of the resulting script to get the script for just the expression you care about.

Hope that helps.

I am certainly interested/curious to understand how you're using this, and where you're compiling c# to script dynamically...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!