C# 理解枚举

C# 理解枚举,c#,enums,C#,Enums,我试图弄清楚枚举是如何工作的,我试图创建一个函数来写入注册表,使用枚举作为注册表的根,但有点困惑 public enum RegistryLocation { ClassesRoot = Registry.ClassesRoot, CurrentUser = Registry.CurrentUser, LocalMachine = Registry.LocalMachine, User

我试图弄清楚枚举是如何工作的,我试图创建一个函数来写入注册表,使用枚举作为注册表的根,但有点困惑

public enum RegistryLocation
        {
            ClassesRoot = Registry.ClassesRoot,
            CurrentUser = Registry.CurrentUser,
            LocalMachine = Registry.LocalMachine,
            Users = Registry.Users,
            CurrentConfig = Registry.CurrentConfig
        }

public void RegistryWrite(RegistryLocation location, string path, string keyname, string value)
{
     // Here I want to do something like this, so it uses the value from the enum
     RegistryKey key;
     key = location.CreateSubKey(path);
     // so that it basically sets Registry.CurrentConfig for example, or am i doing it wrong
     ..
}

问题是,您正在尝试使用类初始化枚举值,并将枚举值用作类,但这是无法做到的。发件人:

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

您可以做的是将该枚举作为标准枚举,然后让一个方法基于该枚举返回正确的RegistryKey

例如:

    public enum RegistryLocation
    {
        ClassesRoot,
        CurrentUser,
        LocalMachine,
        Users,
        CurrentConfig
    }

    public RegistryKey GetRegistryLocation(RegistryLocation location)
    {
        switch (location)
        {
            case RegistryLocation.ClassesRoot:
                return Registry.ClassesRoot;

            case RegistryLocation.CurrentUser:
                return Registry.CurrentUser;

            case RegistryLocation.LocalMachine:
                return Registry.LocalMachine;

            case RegistryLocation.Users:
                return Registry.Users;

            case RegistryLocation.CurrentConfig:
                return Registry.CurrentConfig;

            default:
                return null;

        }
    }

    public void RegistryWrite(RegistryLocation location, string path, string keyname, string value) {
         RegistryKey key;
         key = GetRegistryLocation(location).CreateSubKey(path);
    }

问题是,您正在尝试使用类初始化枚举值,并将枚举值用作类,但这是无法做到的。发件人:

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

您可以做的是将该枚举作为标准枚举,然后让一个方法基于该枚举返回正确的RegistryKey

例如:

    public enum RegistryLocation
    {
        ClassesRoot,
        CurrentUser,
        LocalMachine,
        Users,
        CurrentConfig
    }

    public RegistryKey GetRegistryLocation(RegistryLocation location)
    {
        switch (location)
        {
            case RegistryLocation.ClassesRoot:
                return Registry.ClassesRoot;

            case RegistryLocation.CurrentUser:
                return Registry.CurrentUser;

            case RegistryLocation.LocalMachine:
                return Registry.LocalMachine;

            case RegistryLocation.Users:
                return Registry.Users;

            case RegistryLocation.CurrentConfig:
                return Registry.CurrentConfig;

            default:
                return null;

        }
    }

    public void RegistryWrite(RegistryLocation location, string path, string keyname, string value) {
         RegistryKey key;
         key = GetRegistryLocation(location).CreateSubKey(path);
    }

嗯,我想我明白了,但是=在枚举中究竟可以用来做什么呢?然后你可以用它为枚举分配非连续的数值。例如,如果希望枚举从-1开始,然后从-1开始,那么可以将第一个条目设置为-1。例如:InvalidSelection=-1,NoSelection(值为0),ValidSelection(值为1)。嗯,我想我明白了,但是=在枚举中究竟可以用于什么,然后您可以使用它为枚举分配非顺序的数值。例如,如果希望枚举从-1开始,然后从-1开始,那么可以将第一个条目设置为-1。例如:InvalidSelection=-1、NoSelection(值为0)、ValidSelection(值为1)。