方法内容到字符串 [英] Method contents to string

查看:51
本文介绍了方法内容到字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将方法的内容作为字符串?

例如,如果我有

  void  myfunction()
{
MessageBox.Show( 你好);
}

string FnToString( void fn)
{
// 这里有什么????
}

void fncall()
{
MessageBox.Show(FnToString(myFunction));
}





我希望输出成为一个消息框,上面写着:MessageBox.Show(Hello);



有没有办法做到这一点?

解决方案

不可靠。



问题是方法内容不是作为源存储的 - 它被编译为IL代码,然后传递给CLR和JIT编译器以生成执行的本机代码。只有Debug版本维护任何链接回原始源代码 - 故意从发布版本中删除,以便您在发运产品时不泄露代码!



话虽如此,你可以重新生成源代码(在某种程度上 - 优化可能会严重扭曲你编写的版本的代码),但这不是一个简单的过程。补充一点,代码可以给予额外的混淆,使其更难以重新生成任何有用的东西,并且很可能你完全没有这样做会好多了! :笑:



您最好使用 .NET反射器 [ ^ ]或 JetBrains DotPeek [ ^ ]而不是自己尝试...


从这里开始:

http ://social.msdn.microsoft.com/Forums/vstudio/en-US/00e59445-9f85-4ec5-a04f-9796a72a00a2/library-to-decompile-assembly-to-c [ ^ ]



http://stackoverflow.com/问题/ 9811448 / icsharpcode-decompiler-mono-cecil-how-to-generate-code-for-a-single-method [ ^ ]



http://evain.net/blog/articles/2008/ 12/15 / cecil-decompiler / [ ^ ]

Is there any way to get the contents of a method as a string?
For example, if I had

void myfunction()
{
     MessageBox.Show("Hello");
}

string FnToString(void fn)
{
     //what goes here????
}

void fncall()
{
     MessageBox.Show(FnToString(myFunction));
}



I would want output to be a messagebox that says: "MessageBox.Show("Hello");"

Is there any way to do this?

解决方案

Not reliably.

The problem is that the method content isn't stored as source - it's compiled to IL code and then passed to the CLR and the JIT compiler to generate the native code which gets executed. Only Debug versions maintain any link back to the original source code - it is deliberately removed from release versions so that you don't give away your code when you ship your product!

Having said that, you can "regenerate" the source code (to an extent - optimizations may severely distort the code from the version you wrote) but it's not a simple process. Add in that code can be given an additional obfuscation to make it even harder to regenerate anything useful, and the chances are that you would be a lot better off not doing this at all! :laugh:

You are probably better using .NET reflector[^] or JetBrains DotPeek[^] than trying to do it yourself...


Start here :
http://social.msdn.microsoft.com/Forums/vstudio/en-US/00e59445-9f85-4ec5-a04f-9796a72a00a2/library-to-decompile-assembly-to-c[^]

http://stackoverflow.com/questions/9811448/icsharpcode-decompiler-mono-cecil-how-to-generate-code-for-a-single-method[^]

http://evain.net/blog/articles/2008/12/15/cecil-decompiler/[^]


这篇关于方法内容到字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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