Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I change value of constant in C#?

Tags:

c#

reflection

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

like image 900
Durika Avatar asked May 30 '11 09:05

Durika


People also ask

Can you change the value of a constant?

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.

Can you overwrite a const?

A variable, const or not, is just a location in memory, and you can break the rules of const and simply overwrite it.

Can we change a constant variable in C using a pointer?

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.


4 Answers

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.

like image 81
Sam Holder Avatar answered Oct 11 '22 20:10

Sam Holder


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.

like image 33
Daniel Hilgarth Avatar answered Oct 11 '22 18:10

Daniel Hilgarth


You cannot change const because it is compile-time calculated. Only way is to change that const filed is to modify that legacy assembly

like image 32
Stecya Avatar answered Oct 11 '22 19:10

Stecya


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.

like image 37
Merlyn Morgan-Graham Avatar answered Oct 11 '22 20:10

Merlyn Morgan-Graham