我可以在 C# 中更改常量的值吗? [英] Can I change value of constant in C#?

查看:49
本文介绍了我可以在 C# 中更改常量的值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于无法重新编译的旧版 .NET dll,我需要此 hack.一些黑客,例如使用反射等

I need this hack for legacy .NET dll which cannot be recompiled. Some hack e.g. using reflection, etc.

推荐答案

如果程序集未签名,那么您可能能够反汇编和修改 IL 并重新编译,或者反汇编源代码并修改并重新编译.我会找到合适的链接....

if the assembly is not signed then you might be able to either dissassemble and modify the IL and recompile, or disassemble to source and modify and recompile. I'll find the appropriate links....

要反汇编源代码,您可以使用 reflector(警告-不再免费)与 Dennis Bauers 插件 或者您可以使用 reflexil 这是另一个反射器插件,带有完整的 VB/C# 编辑器和智能感知,允许直接注入代码从反射器.

To disassemble to source you can use reflector (warning- no longer free) with Dennis Bauers plugin or you can use reflexil which is another plugin for reflector which comes with a complete VB/C# editor and intellisense allowing code injection directly from Reflector.

要反汇编为 IL,您可以使用 ILSpy 反汇编程序或 MSILDissasembler

to disassemble to IL you can use ILSpy disassembler or the MSILDissasembler

正如其他人所指出的,尽管您需要仔细考虑这样做的影响.您可能会意识到它有更多的连锁反应.

As others have pointed out though you want to carefully consider the implications of doing this. It may have more knock-ons that you realise.

另一件非常重要的事情是,如果该常量被其他引用您正在重新编译的 dll 的 dll 使用,这些 dll 将不会看到常量的新值而不会被重新编译.

The other thing that is very important is that if the constant is used by other dlls which reference the dll you are recompiling compiling, those dlls WILL NOT SEE THE NEW VALUE FOR THE CONSTANT WITHOUT ALSO BEING RECOMPILED.

这是因为当某些东西被定义为常量时,'constant' 值作为优化被烘焙到引用的 dll 中(所以它不需要每次使用时都在引用的 dll 中查找)AT BUILD TIME 因此在任何引用常量"的库中实际上都不会看到对常量"值的更改.请参阅这个问题和答案更多细节.

This is because when something is defined as a constant, the 'constant' value is baked into the referencing dll as an optimisation (so it doesn't need to be looked up in the referenced dll every time it is used) AT BUILD TIME so changes to the 'constant' value are never actually seen in any libraries that reference the 'constant'. See this question and answers for some more details.

这篇关于我可以在 C# 中更改常量的值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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