在code .NET / C#字符串引用不加载更新后的值的背后字符串、加载、code、NET

2023-09-05 00:27:45 作者:惰性

我似乎遇到了一个奇怪的问题,即一个字符串引用没有加载更新的价值。

I seem to have run into a strange issue whereby a string reference is not loading an updated value.

总之,我把它换成一个DLL文件(APP_ code.dll),其中包含了一堆的页面标题(HTML想想页面标题),但是从其他DLL的引用,当值不被更新。

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.

下面是一个示例页面的codebehind一个code片断:

Here's a code snippet from a codebehind of a sample page:

   this.Master.PageDescription = Constants.Titles.CardInfoPageDescription;

常量类被编译成APP_ code.dll,我刚刚更换。我也被清除高速缓存(IIS 6在这种情况下),重新启动机器,并确保我的本地浏览器的缓存是空的。

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.

然而,当我加载网页,它是不加载的Constants.Titles.CardInfoPageDescription新值。这适用于所有网页。

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.

我可以得到它的更新替换DLL文件的页面,其中有没有改变?唯一的办法

The only way I can get it to update it to replace the DLL for that page, which hasn't changed at all...

任何想法,这是为什么?是此字符串引用未竟抬起头在运行时和内置的网页DLL的?

Any idea why this is? Is this string reference not actually looked up at runtime and built into the page DLL's?

任何帮助是极大AP preciated!

Any help is GREATLY appreciated!

谢谢, 亚当

推荐答案

让我猜:你常数公开为公共常量字段

Let me guess: your constants are exposed as public const fields.

每当你使用常量,其价值被嵌入到编译code在编译时,而不是在运行时动态地被引用。所以,当你后来更换其中常数声明的DLL,被替换DLL之外的所有code将继续使用旧值,直到它被重新编译。

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.)