对枚举数据类型定义感到困惑吗?

发布于 2024-08-25 09:53:01 字数 579 浏览 2 评论 0原文

据我所知,我可以在我的项目类中定义一个新的枚举数据格式, 假设我不这样做,我可以从 3rd-Party DLL 引用 DataFormat。 然后我也可以使用 3rd-Party DLL 元数据中的 Enum DataFormat。

有什么区别?定义新的 Enum DataFormat 是否有任何指南?谢谢。

    public enum DataFormat
    {
        SECS = 0,
        S2_L = 4,
        S2_B = 8,
        S2_BOOLEAN = 12,
        S2_A = 16,
        S2_J = 20,
        S2_U1 = 24,
        S2_U2 = 25,
        S2_U4 = 26,
        S2_U8 = 27,
        S2_I1 = 28,
        S2_I2 = 29,
        S2_I4 = 30,
        S2_I8 = 31,
        S2_F4 = 34,
        S2_F8 = 35,
        S2_STRING = 36,
    }
}

As I know I can define a new Enum DataFormat at the my project Class,
Suppose, I don't do that, I can reference the DataFormat from the 3rd-Party DLL.
Then I can use the Enum DataFormat from the metadata of 3rd-Party DLL too.

What's the difference? And are the any guideline to define A new Enum DataFormat ? Thanks.

    public enum DataFormat
    {
        SECS = 0,
        S2_L = 4,
        S2_B = 8,
        S2_BOOLEAN = 12,
        S2_A = 16,
        S2_J = 20,
        S2_U1 = 24,
        S2_U2 = 25,
        S2_U4 = 26,
        S2_U8 = 27,
        S2_I1 = 28,
        S2_I2 = 29,
        S2_I4 = 30,
        S2_I8 = 31,
        S2_F4 = 34,
        S2_F8 = 35,
        S2_STRING = 36,
    }
}

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

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

发布评论

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

评论(2

深爱成瘾 2024-09-01 09:53:01

这里没有“元数据”——枚举类型仍然是类型。如果一个程序集已存在于单独的程序集中,那么您想要在另一个项目中重新创建它的唯一原因是,如果存在某些特定原因导致您无法引用原始程序集。

如果您在任何解决方案中具有相同类型(任何类型)的两个(相同或几乎相同)版本,其中“解决方案”包括引用的程序集,那么您将面临非常现实的冲突或至少是混乱的风险,而没有任何实际好处我可以看到。

如果该类型尚不存在,并且您询问应该在哪里创建它......通常类型位于其依赖项开始的地方。如果它只是坐在外部组件中而不执行任何操作,那么它就不应该在那里。另一方面,如果外部程序集中的其他类依赖于它,那么它肯定需要进入那里或外部程序集的依赖项之一中,否则您可能最终会得到一个丑陋的循环命名空间依赖项,您将需要消除(并且在游戏后期可能很难做到)。

因此,基本上:不要定义与已存在的类型相同的类型,除非您有充分的理由,并且不要在您实际上不打算使用的项目/程序集中定义任何类型。

There's no "metadata" here - Enum types are still types. If one already exists in a separate assembly, then the only reason you would want to recreate it in another project is if there's some specific reason why you cannot reference the original assembly.

If you have two (identical or almost identical) versions of the same type - any type - in any solution, where "solution" includes referenced assemblies, then you are running a very real risk of conflicts or at least confusion, for no tangible benefit that I can see.

If the type doesn't exist yet and you are asking where it should be created... normally a type lives where its dependencies begin. If it's just going to be sitting in the external assembly doing nothing, then it shouldn't be there. On the other hand, if other classes in the external assembly depend on it, then it definitely needs to go in there or in one of the external assembly's dependencies, otherwise you'll probably end up with an ugly circular namespace dependency that you'll need to eliminate (and it can be very difficult to do later in the game).

So, basically: Don't define your own types identical to a type that already exists, unless you have a very good reason, and don't define any type in a project/assembly where you don't actually intend to use it.

人疚 2024-09-01 09:53:01

我喜欢遵循的一般规则是,用它们工作所需的最小范围来定义类、枚举、方法和变量。因此,如果仅从单个类访问枚举,则将其定义为该类的成员。如果在类外部访问它,则在类外部定义它。

The general rule that I like to follow is that you define classes, enumerations, methods and variables with the smallest scope required for them to work. So if the enumeration is only accessed from a single class, then define it as a member of that class. If it's accessed outside of the class, the define it outside of the class.

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