Compile and run dynamic code, without generating EXE?

后端 未结 8 1893
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-27 14:17

I was wondering if it was possible to compile, and run stored code, without generating an exe or any type of other files, basically run the file from memory.

Basical

8条回答
  •  甜味超标
    2020-11-27 14:41

    It's possible. It's easy or hard, depending on how much and what kind of code you want to write.

    • Compile one-line expressions: System.Linq.Expressions (straightforward but limited)
    • Compile whole programs: System.Reflection.Emit (hard to debug, but not impossible)

    Edit: Note that, prior to .NET 4.0, System.Linq.Expressions is limited to what you can fit on a single line of C#: that is, no if, while, variable assignment etc.

提交回复
热议问题