简体   繁体   English

使用常量创建DLL程序集

[英]Creating a DLL Assembly with constants

I have a solution with about 3 projects in the solution. 我在解决方案中有大约3个项目的解决方案。 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? 任何人都可以确认这是否属实,如果是,请告诉我这样做的首选和替代方法,以便我可以更改我的程序集中的常量?

Instead of declaring const values, declare the actual values in a static class. 而不是声明const值,而是在静态类中声明实际值。

public static readonly int someConst = 6;

That will force your program to use the updated constant when you change it in the linked DLL. 这将强制您的程序在链接的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? 静态只读与const - 不同的装配POV?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM