包装枚举的最佳方式是什么

发布于 2024-09-14 16:58:49 字数 111 浏览 4 评论 0原文

我有我的枚举,我不想在层之间共享它。

所以我想把它包起来会很整洁。

但如何呢?项目很多,手动写下所有项目并不是解决方案。 枚举也正在生成。

我想保留智能感知。

I have my enum, which I don't want to share between layers.

So I was thinking wrapping it up would be neat.

But how ? There are many items, manualy writing down all items isn't a solution.
Also the enum is getting generated.

I'd like to keep the intelisense.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如歌彻婉言 2024-09-21 16:58:49

好吧,您可以从 int (或定义的基础类型,默认为 int)进行转换,但这很脆弱;当有人删除条目导致条目重新编号,或者将 7 的含义从 Blah.TrivialDetail 更改为 Blah.CriticalFail 时,它无法保护您

如果您想要智能感知,所有各方都需要了解有关数字的一些,因此在我看来,您不妨通过一些通用程序集共享枚举。

Well, you can cast to/from int (or the defined underlying type, which defaults to int), but this is brittle; it doesn't protect you when somebody removes an entry causing them to be re-numbered, or changes the meaning of 7 from Blah.TrivialDetail to Blah.CriticalFail.

If you want intellisense, all parties are going to need to know something about the numbers, so IMO you might just as well share the enum via some common assembly.

一花一树开 2024-09-21 16:58:49

一种方法(不太优雅)是在程序集中创建另一个可以共享的枚举。您需要创建一些构建操作以将生成的枚举从数据程序集克隆到新的可共享枚举。如果生成的枚举位于单独的文件中,则简单的文件复制(更改命名空间)作为 DAL 项目的构建后事件或共享程序集项目中的预构建事件即可解决问题。如果生成的文件包含其他一些内容,那么您需要用您最喜欢的语言(powershell、c#)进行一些文本处理以提取枚举定义。

Well one way (not so elegant) is to create another enum in the assembly that can be shared. You need to create some build action to clone the generated enum from data assembly to new sharable enum. If generated enum is in a separate file then simple file copy (with change in namespace) as post-build event to DAL project or Pre-Build event in shared assembly project would do the trick. If the generated file contains some other stuff then you need to do some text processing in your favorite language (powershell, c#) to extract enumeration definition.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文