Domanda

I have a solution with about 3 projects in the solution. These 3 projects share some common constants that, until now, I have been copying and pasting between the 3 projects.

What I want to do instead is create an assembly that contains these constants and just reference the assembly from the other project. The problem is, I think I remember reading somewhere once that when the project is compiled, the constant values are simply "copied and pasted" from the assembly into the assembly referencing it. Which would mean that if I wanted to change the value of one of these constants, I couldn't just change the one assembly. I'd have to recompile the entire solution again.

Can anyone please confirm if this is true and if it is, please tell me the preferred and alternative way to do this so that I can just change the constant in my assembly?

È stato utile?

Soluzione

Instead of declaring const values, declare the actual values in a static class.

public static readonly int someConst = 6;

That will force your program to use the updated constant when you change it in the linked DLL.

For what it's worth, some sort of configuration file is probably a better solution, if you want to do this at runtime, rather than compile time.

Further Reading
Static readonly vs const — different assemblies POV?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top