C#将函数源代码转换为字符串 [英] C# Convert function source code to string

查看:65
本文介绍了C#将函数源代码转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,有没有一种方法可以将函数的源代码转换为字符串?有点像如何用JavaScript完成.我需要这样做是因为我正在为Asp.Net控件编写一些文档,并希望在结果旁边显示原始源代码,而无需复制/粘贴它.

In C# is there a way to convert a function's source code to a string? Sort of like how it can be done in JavaScript. I need this because I'm writing some documentation for an Asp.Net control and want to show the original source code next to the result, without copy/pasting it.

推荐答案

简短答案:否.

长答案:您可以在编译时使用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.)

这篇关于C#将函数源代码转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆