Can I change value of constant in C#?

13,232

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

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

Solution 5

const values are replaced as literals during compile time, so highly unlikely you would be able to change it without recompiling

Share:
13,232

Related videos on Youtube

Durika
Author by

Durika

Updated on June 01, 2022

Comments

  • Durika
    Durika almost 2 years

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

    • Fredrik Mörk
      Fredrik Mörk almost 13 years
      Oh dear, imagine the chaos that will follow of making a constant non-constant. It might even bring Elvis back.
    • Sam Holder
      Sam Holder almost 13 years
      do you need the value to be changed for use internally within the dll or only where it is used by references outside the dll?
    • Durika
      Durika almost 13 years
      Yes, I can imagine. That's why I am talking about hack.
    • Fredrik Mörk
      Fredrik Mörk almost 13 years
      I hope you did not take offence by my joke there; I really should have included a smiley. It's a good question, with lots of interesting implications to think about (so I upvoted it).
  • Fredrik Mörk
    Fredrik Mörk almost 13 years
    This is one of those answers I wish I could upvote twice; one vote for pointing out the need to recompile consuming code, and one for "don't expose constants directly".
  • Sam Holder
    Sam Holder almost 13 years
    yeah +1 for the always expose the constants through a property.
  • imlokesh
    imlokesh over 8 years
    JustDecompile is free and supports reflexil.