创建不包含 0 值的 C# 枚举实例

发布于 2024-10-14 08:40:42 字数 392 浏览 4 评论 0原文

我需要创建一个没有 0 值的 Enum 类的实例。使用 0 值时,下一个代码可以正常工作:

ObjectFactory.CreateInstance("Edu3.DTOModel.Schedule.ScheduleStateEnum");

Enum:

namespace Edu3.DTOModel.Schedule
{
    public enum ScheduleStateEnum
    {
        DUMMY = 0,
        Draft = 1,
        Published = 2,
        Archived = 3
    }
}

如果我注释掉 DUMMY,则创建实例将不再起作用。

I need to create an instance of an Enum class that hasn't got a 0 value. With a 0 value, next code works fine:

ObjectFactory.CreateInstance("Edu3.DTOModel.Schedule.ScheduleStateEnum");

Enum:

namespace Edu3.DTOModel.Schedule
{
    public enum ScheduleStateEnum
    {
        DUMMY = 0,
        Draft = 1,
        Published = 2,
        Archived = 3
    }
}

If I comment out DUMMY, Creating the instance doesn't work anymore.

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

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

发布评论

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

评论(4

暮光沉寂 2024-10-21 08:40:42

我需要创建一个没有 0 值的 Enum 类的实例。

假设像这样的枚举:

public enum ScheduleStateEnum
{
    Draft = 1,
    Published = 2,
    Archived = 3
}

您可以创建这样的实例:

ScheduleStateEnum myState = 0;

如果您无法声明该类型的变量并且需要以字符串形式访问该类型(如您的示例中所示),请使用 Activator.CreateInstance:

var myState = Activator.CreateInstance(Type.GetType(
                  "Edu3.DTOModel.Schedule.ScheduleStateEnum"));

当然,这两个选项都会为您提供一个实际上具有整数值0的实例,即使枚举没有声明一个。如果您希望它默认为您实际声明的值之一,则需要使用反射来查找它,例如:

var myState = Type.GetType("Edu3.DTOModel.Schedule.ScheduleStateEnum")
                  .GetFields(BindingFlags.Static | BindingFlags.Public)
                  .First()
                  .GetValue(null);

对于根本没有定义值的枚举,这将会崩溃。如果您想防止这种情况,请使用 FirstOrDefault 并检查 null

I need to create an instance of an Enum class that hasn't got a 0 value.

Assuming an enum like this:

public enum ScheduleStateEnum
{
    Draft = 1,
    Published = 2,
    Archived = 3
}

you can create an instance like this:

ScheduleStateEnum myState = 0;

If you cannot declare a variable of the type and you need to access the type as a string (as in your example), use Activator.CreateInstance:

var myState = Activator.CreateInstance(Type.GetType(
                  "Edu3.DTOModel.Schedule.ScheduleStateEnum"));

Of course, both of these options will give you an instance that actually has the integer value 0, even if the enum doesn’t declare one. If you want it to default to one of the values you have actually declared, you need to use Reflection to find it, for example:

var myState = Type.GetType("Edu3.DTOModel.Schedule.ScheduleStateEnum")
                  .GetFields(BindingFlags.Static | BindingFlags.Public)
                  .First()
                  .GetValue(null);

This will crash for enums that have no values at all defined. Use FirstOrDefault and check for null if you want to prevent this.

二货你真萌 2024-10-21 08:40:42

这是您的 ObjectFactory 类的问题,因为

Activator.CreateInstance(typeof(Edu3.DTOModel.Schedule.ScheduleStateEnum))

工作正常并创建 int 0。

It's a problem with your ObjectFactory class, because

Activator.CreateInstance(typeof(Edu3.DTOModel.Schedule.ScheduleStateEnum))

works fine and creates int 0.

梦忆晨望 2024-10-21 08:40:42

事实上这是不可能的。根据定义,枚举是一个值字段,因此它必须有一种方法用 0 数值对其进行初始化。

Actually it is not possible. Enum is per definition a value field, so it has to ahve a way to initialize it with a 0 numerical value.

‘画卷フ 2024-10-21 08:40:42

最佳做法是提供零值枚举成员 - 请参阅 http://msdn.microsoft.com/en-us/library/ms182149%28VS.80%29.aspx 了解详细信息。您是否有任何理由不想要零值成员(例如 None)?

It is best practice to provide a zero valued enum member - see http://msdn.microsoft.com/en-us/library/ms182149%28VS.80%29.aspx for details. Is there any reason you don't want a zero valued member such as None?

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