I need this hack for legacy .NET dll which cannot be recompiled. Some hack e.g. using reflection, etc.
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
Adding to Stecya's answer:
The value of the const variable will be inserted anywhere it is used. This means that all assemblies referencing your legacy assembly and using that constant need to be recompiled also to reflect the updated value of the const variable. That's BTW the reason why it is a good idea to always expose constant values through normal properties in public interfaces.
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....
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.
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.
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.
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.
const values are replaced as literals during compile time, so highly unlikely you would be able to change it without recompiling
You cannot change const because it is compile-time calculated. Only way is to change that const filed is to modify that legacy assembly
Reflections won't work because this value is hard-coded into the byte code of the application in a way that reflections wont be able to modify.
If it is not code signed, use a hex editor or ILDasm to modify the value of the constant.
If it is code signed, you have no way to solve this without foregoing code signing.
If you are editing a compiled assembly, be careful. There may be legal reasons you can't do that either.