C# 方法的字符串表示

发布于 2024-07-25 06:38:56 字数 221 浏览 2 评论 0原文

.NET 3.0(或更早版本)中有没有办法获取方法的字符串表示形式? 我知道我可以从 MethodBody 对象中获取 IL 字节数组,但我感兴趣的是获取一个本质上代表方法体的字符串,就像我在 VS 中看到的那样。

我在 CodeDom 命名空间中进行了探索,看看是否有一种方法可以在运行时将方法转换为 CodeMemberMethods,但到目前为止我还是一无所获。

有什么想法吗?

Is there a way in .NET 3.0 (or earlier) to get a string representation of a method? I know that I can get an IL byte array from a MethodBody object, but I'm interested in getting a string that essentially represents the method body as it appears to my eyes in VS.

I've poked around in the CodeDom namespace to see if there was a way to convert methods to CodeMemberMethods at runtime, but I've come up dry so far.

Any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浊酒尽余欢 2024-08-01 06:38:56

这是一项非常复杂的工作。 基本上,您会考虑在代码中重写 reflector ...并且不要忘记,并非所有 IL 都可以逆转。

简短的回答是“不,没有内置任何东西”。

即使是表达式树(来自 lambda)也无法轻松地完全反转回适当的字符串。

That is very complex work. Basically, you'd be looking at re-writing reflector in your code... and don't forget that not all IL can be reversed.

The short answer is "no, nothing built in".

Even expression trees (from lambdas) can't be fully reversed back to appropriate strings easily.

厌倦 2024-08-01 06:38:56

尝试使用 Reflector API 进行探索(您可能想检查 Red Gate 接管后它是否仍然可用)。

即使您成功了,您也会丢失一些原始信息,例如局部变量名称、注释或某些格式。

基本上,编译期间丢弃的所有内容都会永久丢失。

Try poking around with reflector API (you might want to check if that is still available after Red Gate took it over).

Still even if you succeed you will loose some of the original information like local variable names, comments or some formatting.

Basically everything that is thrown away during compilation is lost for good.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文