问题
I seem to have run into a strange issue whereby a string reference is not loading an updated value.
In short, I've replaced a DLL file (App_Code.dll) that contains a bunch of page titles (think HTML Page Titles), but the values aren't being updated when referenced from other DLL's.
Here's a code snippet from a codebehind of a sample page:
this.Master.PageDescription = Constants.Titles.CardInfoPageDescription;
The Constants class is compiled into App_Code.dll, which I just replaced. I've also cleared the cache (IIS 6 in this case), rebooted the machine, and made sure my local browser cache is empty.
However, when I load the web page, it is not loading the new value for Constants.Titles.CardInfoPageDescription. This is true for ALL web pages.
The only way I can get it to update it to replace the DLL for that page, which hasn't changed at all...
Any idea why this is? Is this string reference not actually looked up at runtime and built into the page DLL's?
Any help is GREATLY appreciated!
Thanks, Adam
回答1:
Let me guess: your constants are exposed as public const
fields.
Whenever you use a const
, its value is embedded into the compiled code at build-time rather than being referenced dynamically at run-time. So when you subsequently replace the DLL where the constants are declared, all code outside of the replaced DLL will continue to use the old value until it is recompiled.
On a more philosophical note - why are your "constants" being updated? Only use const
for values that will never, ever, ever change. If it can change then it's not a constant.
And on a more practical note - it's not generally considered good practice to expose public fields. Use properties instead. (One possible exception to this rule might be genuine constants that will never, ever, ever change.)
回答2:
Constants are converted at Compile time to their respective values and thus not changed at Runtime. References of these constants will be built at compile time in these referencing DLL's.
来源:https://stackoverflow.com/questions/3523241/net-c-sharp-string-reference-in-code-behind-not-loading-updated-value