简体   繁体   English

C#中的参数枚举

[英]Parameter Enums in C#

I'm used to programming in Java however for this project I'm supposed to be using C#, I'm trying to convert my Packet system over from my Java project, however I'm running into some issues using the C# Compiler. 我曾经使用Java编程,但是对于该项目,我应该使用C#,但是我试图将我的Packet系统从Java项目转换过来,但是使用C#编译器却遇到了一些问题。 Here's the code. 这是代码。

abstract class Packet
{
    public static enum PacketTypes
    {
        INVALID(-1), LOGIN(00);

        private int packetId;

        private PacketTypes(int packetId)
        {
            this.packetId = packetId;
        }

        public int getId()  { return packetId; }

    }
}

This is actually exactly how it's done in my Java Code, and I have the individual packets extend the Packet class. 实际上,这正是在Java代码中完成的方式,而且我让各个数据包扩展了Packet类。 I'm trying to figure out how to make this all come together in C#. 我试图弄清楚如何使所有这些在C#中融合在一起。 Perhaps having a separate class for each packet isn't the way it should be done here? 也许每个数据包都有一个单独的类不是这里应该做的方式吗?

I'm not sure what you're trying to achieve, but you can set values for particular enum elements in C#: 我不确定您要实现的目标,但是可以为C#中的特定enum元素设置值:

public enum PacketTypes
{
    INVALID = -1;
    LOGIN = 0;
}

Because enum is by default backed by int , you can cast it from/to int without additional code. 由于默认情况下enumint支持,因此您可以将它从intint而无需其他代码。

enum s in C# cannot content any members, so you can't add methods/properties/fields to enum declaration. C#中的enum不能满足任何成员的要求,因此不能将方法/属性/字段添加到enum声明中。

Unlike Java where enum s are classes, in C# enum s are plain values. 与Java enum是类的Java不同,在C#中enum是纯值。 They cannot have member functions or fields. 它们不能具有成员函数或字段。

One approach that could help is to define an extension method for your enum , like this: 一种可能有用的方法是为enum定义扩展方法,如下所示:

public static class PacketTypesExtensions {
    static readonly IDictionary<PacketTypes,int> IdForType = new Dictionary<PacketTypes,int> {
        { PacketTypes.INVALID, -1 }
    ,   { PacketTypes.LOGIN,    0 }
    };
    static readonly IDictionary<PacketTypes,string> DescrForType = new Dictionary<PacketTypes,string> {
        { PacketTypes.INVALID, "<invalid packet type>" }
    ,   { PacketTypes.LOGIN,   "<user login>" }
    };
    public static string Description(this PacketTypes t) {
        return DescrForType[t];
    }
    public static int Id(this PacketTypes t) {
        return IdForType[t];
    }
}

This lets you keep Java syntax: 这使您可以保留Java语法:

PacketTypes pt = ... // <<== Assign a packet type here
int id = pt.Id();    // This calls the static extension method
string d = pt.Description();

You could try this in addition to Marcins answer. 除了Marcins答案,您还可以尝试一下。

public enum PacketTypes
{
    INVALID = -1;
    LOGIN = 0;
}

public class Packet
{
    public PacketTypes PacketType { get; set;}
}

In your code somewhere, you would do this 在您的代码中,您可以执行此操作

public void DoSomething()
{
    var packet = new Packet();
    packet.PacketType = PacketTypes.INVALID; // Assign packtype
    Console.WriteLine(packet.PacketType.ToString()); // Retrieve and print 
}

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

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