且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#将函数源代码转换为字符串

更新时间:2023-02-12 17:09:51

简短答案:否.

长答案:您可以在编译时使用MSBuild任务,T4模板或手动解析内容,或使用Roslyn为您解析内容来生成某些内容.

Long answer: you can generate something at compile time with MSBuild tasks, T4 templates, or manually parsing stuff, or using Roslyn to parse it for you.

但是,如果您在运行时需要源代码,则肯定需要在已部署的内容中包含cs文件,因为没有稳定的方法来取回代码.(尽管您可以尝试运行时反编译,但这看起来丑陋"而且不太精确.)

If you need the source code at runtime though, you'll surely need to include the cs files in the deployed stuff, because there is no stable way of getting the code back. (You could try runtime decompilation though, but that will look "ugly" and not very precise.)