I need this hack for legacy .NET dll which cannot be recompiled. Some hack e.g. using reflection, etc.
A constant is a value that cannot be altered by the program during normal execution, i.e., the value is constant. When associated with an identifier, a constant is said to be “named,” although the terms “constant” and “named constant” are often used interchangeably.
A variable, const or not, is just a location in memory, and you can break the rules of const and simply overwrite it.
Changing Value of a const variable through pointer The variables declared using const keyword, get stored in . rodata segment, but we can still access the variable through the pointer and change the value of that variable.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With