在C#enum中使用不同的类型

在C#enum中使用不同的类型,c#,types,enumeration,C#,Types,Enumeration,我有一个如下所示的枚举: public enum EnumWeapons { Fists = new WeaponFists(), Sword = new WeaponSword(), Bow = new WeaponBow(), Staff = new WeaponStaff() } 都是从基础武器衍生出来的 可以这样使用枚举吗 如果是这样的话,我能做如下事情吗 public Weapon weapon = (Weapon)EnumWeapons.Fists;

我有一个如下所示的枚举:

public enum EnumWeapons
{
    Fists = new WeaponFists(),
    Sword = new WeaponSword(),
    Bow = new WeaponBow(),
    Staff = new WeaponStaff()
}
都是从基础武器衍生出来的

可以这样使用枚举吗

如果是这样的话,我能做如下事情吗

public Weapon weapon = (Weapon)EnumWeapons.Fists;

我的尝试没有达到预期效果,任何帮助或指导都将不胜感激:)。谢谢大家!

否,枚举必须是整数类型(byte、short、int、long)

您试图做的事情可能是这样实现的:

public static class EnumWeapons
{
    public static readonly Weapon Fists = new WeaponFists();
    public static readonly Weapon Sword = new WeaponSword();
    public static readonly Weapon Bow = new WeaponBow();
    public static readonly Weapon Staff = new WeaponStaff();
}
public enum EnumWeapons
{
    Fists,
    Sword,
    Bow,
    Staff
}

public Weapon EnumWeaponsToWeaponObject(EnumWeapons weapon)
{
        switch (weapon)
        {
            case EnumWeapons.Fists:
                return new FistWeapon();
                break;
            case EnumWeapons.Sword:
                return new SwordWeapon();
                break;
            case EnumWeapons.Bow:
                return new BowWeapon();
                break;
            case EnumWeapons.Staff:
                return new StaffWeapon();
                break;
            default:
                throw new ArgumentException("Not a supported weapon type!");
                break;
        }

}
我不认为C#enums是这样工作的。在C#中,所有枚举值基本上都是整数。但是,您可以使用您的方法:

public class Weapon 
{
    public static readonly Weapon Fists = new WeaponFists();
    public static readonly Weapon Sword = new WeaponSword();
   ...
}
然后

var myWeapon = Weapon.Fists;

这是不可能的,枚举的基础类型是各种整数类型。从:

批准的枚举类型有
byte
sbyte
short
ushort
int
uint
long
ulong

而且,我甚至不认为你想做的有意义。当你想要一件武器时,你真的每次都想要相同的实例吗?我对此表示怀疑

我想,你真正想要的是一个工厂。以最天真的形式,你可以说:

public enum WeaponType {
    Fists,
    Sword,
    Bow, 
    Staff
}

public class WeaponFactory {
    public Weapon Create(WeaponType weaponType) {
        switch(weaponType) {
            case WeaponType.Fists:
                return new WeaponFists();

            case WeaponType.Sword:
                return new WeaponSword();

            case WeaponType.Bow:
                return new WeaponBow();

            case WeaponType.Staff:
                return new WeaponStaff();

            default:
                throw new ArgumentOutOfRangeException("weaponType");
        }
    }
}
var weapon = weaponFactory.Create(WeaponType.Fists);
现在你可以说:

public enum WeaponType {
    Fists,
    Sword,
    Bow, 
    Staff
}

public class WeaponFactory {
    public Weapon Create(WeaponType weaponType) {
        switch(weaponType) {
            case WeaponType.Fists:
                return new WeaponFists();

            case WeaponType.Sword:
                return new WeaponSword();

            case WeaponType.Bow:
                return new WeaponBow();

            case WeaponType.Staff:
                return new WeaponStaff();

            default:
                throw new ArgumentOutOfRangeException("weaponType");
        }
    }
}
var weapon = weaponFactory.Create(WeaponType.Fists);
根据MSDN,“经批准的枚举类型有
字节
sbyte
short
ushort
int
uint
long
ulong


请参阅。

这不会直接用于枚举。但是,您可以使用“常规”枚举和
字典来按枚举值检索武器实例。

您不能以这种方式使用枚举。但是,您可以通过创建静态类来实现类似的功能:

public static class AllWeapons
{
    public static readonly Weapon Fists = new WeaponFists();
    // etc
}

Weapon weapon = AllWeapons.Fists;
根据积分常数(在两个级别上使代码无效)

您可以使用语法糖,并使用以下命令使其看起来像一个枚举:

public static class Weapons
{
    Weapon Fists = new WeaponFists(),
    Weapon Sword = new WeaponSword(),
    Weapon Bow = new WeaponBow(),
    Weapon Staff = new WeaponStaff()
}

键入的代码包含对象,而对象不再是枚举,这是枚举构造的目的。我不想只重复别人说的话,所以这里有一个建议: 您可以保留枚举,但创建一个助手方法来生成武器对象,如下所示:

public static class EnumWeapons
{
    public static readonly Weapon Fists = new WeaponFists();
    public static readonly Weapon Sword = new WeaponSword();
    public static readonly Weapon Bow = new WeaponBow();
    public static readonly Weapon Staff = new WeaponStaff();
}
public enum EnumWeapons
{
    Fists,
    Sword,
    Bow,
    Staff
}

public Weapon EnumWeaponsToWeaponObject(EnumWeapons weapon)
{
        switch (weapon)
        {
            case EnumWeapons.Fists:
                return new FistWeapon();
                break;
            case EnumWeapons.Sword:
                return new SwordWeapon();
                break;
            case EnumWeapons.Bow:
                return new BowWeapon();
                break;
            case EnumWeapons.Staff:
                return new StaffWeapon();
                break;
            default:
                throw new ArgumentException("Not a supported weapon type!");
                break;
        }

}
您可以将此方法作为扩展,以便按如下方式使用它:
枚举武器。剑。枚举武器存储对象()

您可以使用索引器按顺序位置和字符串引用数组或列表。不像枚举那样干净,但数组或列表可以包含对象。这就是它成为索引器的原因

    namespace Indexer
    {
        class Program
        {
            static void Main(string[] args)
            {
                Persons MyPeople = new Persons();
                Console.WriteLine("MyPeople[0] " + MyPeople[0].Name);
                Console.WriteLine("MyPeople[0] " + MyPeople[1].Name);
                Console.WriteLine("MyPeople[Jim] " + MyPeople["Jim"].Name);
                Console.ReadLine();
            }
        }
        public class Persons
        {
            private List<Person> persons = new List<Person>();
            public int Count { get { return persons.Count; } }

            public Person this[int index]
            {
                get
                {
                    return persons[index];
                }
                set
                {
                    persons[index] = value;
                }
            }
            public Person this[string name]
            {
                get
                {
                    foreach (Person p in persons)
                    {
                        if (p.Name == name) return p;
                    }
                    return null;
                }
            }
            public Persons()
            { 
                persons.Add(new Person("Jim"));
                persons.Add(new Person("Harry"));
            }
            public class Person
            {
                private string name;
                public string Name { get { return name; } }
                public Person(string Name) { name = Name; }
            }
        }
    }
名称空间索引器
{
班级计划
{
静态void Main(字符串[]参数)
{
人员MyPeople=新人员();
Console.WriteLine(“MyPeople[0]”+MyPeople[0].Name);
Console.WriteLine(“MyPeople[0]”+MyPeople[1].Name);
Console.WriteLine(“MyPeople[Jim]”+MyPeople[“Jim”].Name);
Console.ReadLine();
}
}
公共阶层人士
{
私人名单人员=新名单();
公共整数计数{get{return persons.Count;}
公众人士本[int索引]
{
得到
{
返回人员[索引];
}
设置
{
人数[指数]=价值;
}
}
公众人物[字符串名称]
{
得到
{
foreach(个人中的p个人)
{
if(p.Name==Name)返回p;
}
返回null;
}
}
公众人士()
{ 
人员。添加(新人员(“Jim”);
人员。添加(新人员(“哈里”);
}
公共阶层人士
{
私有字符串名称;
公共字符串名称{get{return Name;}}
公众人物(字符串名称){Name=Name;}
}
}
}

我想说枚举是整数的类型化文本表示。你不能对它们使用类类型(和继承)为什么不自己试试呢?枚举不是这样工作的。你到底想干什么?看起来你想要的是工厂模式。嗯,我正试图得到一个可以分配给玩家的武器列表(我正在研究游戏开发,对它非常陌生)。我已经阅读了很多次这些文档。我不明白为什么在我尝试此操作时它没有抛出编译器错误?谢谢:)。我要试一试。我不同意这是正确的处理方法。现在,每当他要求使用
EnumWearms.Fists
时,他都会得到同样的
Fists
。我怀疑这是他真正想要的。@Jason-1因为你不同意?那很有趣。我怀疑这也是OP想要的,但他迟早会弄明白的。@Jason:不,我同意这不是最好的解决方案,但也许这就是OP想要的。至于否决票,我没有生气,我只是不同意你的意见:)@Frank Allenby:我很高兴你能成功。但总的来说,期望每个人都能从一个提供误导性解决方案的答案中找出正确的答案是不合理的。谢谢。我曾想过要这么做,但我想最后要保持下去会有点烦人。