具有默认类型转换的枚举? 那可能吗?

发布于 2024-07-19 08:49:53 字数 83 浏览 1 评论 0原文

是否可以为枚举进行默认类型转换?

我经常使用枚举,例如状态,并且我想将枚举直接与 LINQ 字段进行比较,但我必须始终进行类型转换。

is it possible to make a default typecast for an enum?

I use enum for a lot, such as states and I want to compare enums directly to LINQ fields, but I have to typecast all the time.

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

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

发布评论

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

评论(4

叫嚣ゝ 2024-07-26 08:49:54

您应该能够在 LINQ 对象中拥有具有枚举类型的属性。 这样你就不必施放。

因此,只需更改您的属性以具有正确的枚举类型,您就不必再担心强制转换。 您可以在 LINQtoSQL 设计器中执行此操作。 只需右键单击某个属性,选择“属性”,然后在 Visual Studio 属性窗口中设置适当的类型。

You should be able to have properties in your LINQ objects that have an enum type. This way you do not have to cast.

So just change your properties to have the correct enum type and you don't have to worry about casts any longer. You can do this in the LINQtoSQL designer. Just right-click on a property, select 'Properties' and set the appropriate Type in the Visual Studio Properties window.

夏雨凉 2024-07-26 08:49:54

答案要简单得多!

我的一个好朋友告诉我这很简单! 看看这个样本!

public enum State:byte
{
    EmailNotValidated = 0x00,
    EmailValidated = 0x10,
    Admin_AcceptPending = 0x40,
    Active = 0x80,
    Admin_BlockedAccount = 0xff
}

请注意枚举名称后面的 :BYTE 部分...这就是我一直在寻找的技巧! 但感谢每个为我努力的人!

The answer was MUCH more simple!!!

A good friend of mine told me this is very simple! have a look at this sample!

public enum State:byte
{
    EmailNotValidated = 0x00,
    EmailValidated = 0x10,
    Admin_AcceptPending = 0x40,
    Active = 0x80,
    Admin_BlockedAccount = 0xff
}

Pay attention to the :BYTE part after the name of the Enum... there is the trick I was looking for! But thanks to everyone trying for me!

何处潇湘 2024-07-26 08:49:54

LINQ-to-SQL 通常会处理直接整数映射和精确字符串(名称)映射(注意:区分大小写)。 含义:将枚举写入某处,然后在设计器中将属性类型设置为完全限定的枚举名称:Some.Namespace.MyEnum。 它通常应该有效。

对于重要的映射(例如,列是具有混合大小写值的 varchar,或者诸如“进行中”[注意空格]),您必须将存储属性保留为 int/varchar (等)并手动映射它。 我通常通过将其标记为私有并将其命名为 FooStorage,并在分部类中添加映射属性来实现此目的:

partial class MyType {
    public MyEnum Foo {
        get {... mapping logic reading from FooStorage...}
        set {... mapping logic, updating FooStorage...}
    }
}

唯一的问题是 LINQ 查询仅适用于存储 财产(不是定制财产)。

LINQ-to-SQL will usually handle direct integer maps and exact string (name) maps (note: case sensitive). Meaning: write your enum somewhere, and in the designer set the property type as the fully-qualified enum name: Some.Namespace.MyEnum. It should usually work.

For non-trivial mappings (for example where the column is a varchar with mixed-case values, or things like "In Progress" [note the space]), you will have to leave the storage property as int/varchar (etc) and map it manually. I usually do this by marking it as private and naming it FooStorage, and adding a mapping property in a partial class:

partial class MyType {
    public MyEnum Foo {
        get {... mapping logic reading from FooStorage...}
        set {... mapping logic, updating FooStorage...}
    }
}

The only problem is that LINQ queries will only work against the storage property (not the bespoke property).

最美不过初阳 2024-07-26 08:49:54

您尝试过扩展方法吗?

public enum MyEnum
{
    First = 1,
    Second = 2,
    Third = 3
}

public static class Utility
{
    public static string Description(this Enum e)
    {
        Type t = e.GetType();
        DescriptionAttribute[] desc =
            (DescriptionAttribute[])(t.GetField(e.ToString())
            .GetCustomAttributes(typeof(DescriptionAttribute), false));
        return desc.Length > 0 ? desc[0].Description : e.ToString();
    }
    public static byte ToByte(this Enum ai)
    {
        object o=Enum.ToObject(ai.GetType(), ai);
        return Convert.ToByte(o);
    }
}


class Program
{
    static void Main(string[] args)
    {
        MyEnum me = MyEnum.Third;

        Console.WriteLine("Value: {0}\r\nType: {1}"
        ,me.ToByte(),me.ToByte().GetType().ToString());

        Console.ReadLine();
    }
}

它输出:

值:3

类型:System.Byte

Have you tried extension methods?

public enum MyEnum
{
    First = 1,
    Second = 2,
    Third = 3
}

public static class Utility
{
    public static string Description(this Enum e)
    {
        Type t = e.GetType();
        DescriptionAttribute[] desc =
            (DescriptionAttribute[])(t.GetField(e.ToString())
            .GetCustomAttributes(typeof(DescriptionAttribute), false));
        return desc.Length > 0 ? desc[0].Description : e.ToString();
    }
    public static byte ToByte(this Enum ai)
    {
        object o=Enum.ToObject(ai.GetType(), ai);
        return Convert.ToByte(o);
    }
}


class Program
{
    static void Main(string[] args)
    {
        MyEnum me = MyEnum.Third;

        Console.WriteLine("Value: {0}\r\nType: {1}"
        ,me.ToByte(),me.ToByte().GetType().ToString());

        Console.ReadLine();
    }
}

It outputs:

Value: 3

Type: System.Byte

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