-1

This is my enum and i would like to identify which ID (see ID 1, ID 2) to use by selecting its property name

public enum PermissionScope
{
    
    OfflineAccess = "7427e0e9-2fba-42fe-b0c0-848c9e6a8182"; //ID 1
    Profile = "14dad69e-099b-42c9-810b-d002981feec1"; //ID 2
}

Here is an example of in code.

        string[] scopes = new[] { "offline_access", "Profile" };

                foreach (var scope in scopes)
                {
                    ResourceAccess resourceAccess = new ResourceAccess
                    {
                        Id = PermissionScope(scope), //id should have the values now: 7427e0e9-2fba-42fe-b0c0-848c9e6a8182 and in the 2nd run 14dad69e-099b-42c9-810b-d002981feec1
                        Type = "Scope"
                    };
                }

Description what i would like to achieve:

  • I want to get the string "7427e0e9-2fba-42fe-b0c0-848c9e6a8182" written into id by calling PermissionScope.OfflineAccess´

simplified example:

            string scope = "offline_access";
            string id = PermissionScope.FindByString(scope); // id = 7427e0e9-2fba-42fe-b0c0-848c9e6a8182
3
  • You can't, at least not directly with just an enum. In C# enums are more or less "just" named numbers. You probably want to look at a dictionary Commented Jan 21 at 8:19
  • well, your enum-names can be retrieved by using Enum.GetName(typeof(PermissionScope), theEnumValue). However in your case you have an additional underscore. You need some string-magic to transform "offline_access" to "OfflineAccess ". Commented Jan 21 at 8:25
  • Hint: To accociate name "offline_access" with PermissionScope.OfflineAccess (those are different) you can use attributes on enum values or create extension method/dictionary. See duplicate for ideas of how accociate enum value with the string. Commented Jan 21 at 10:12

1 Answer 1

3

I'm not sure there is a way using enums to get strings to return, although you could use an extension method like so:

namespace Test
{
    public enum PermissionScope
    {
        OfflineAccess,
        Profile
    }
    
    public static class PermissionScopeExtension
    {
        public const string OFFLINEACCESS = "7427e0e9-2fba-42fe-b0c0-848c9e6a8182";
        public const string PROFILE = "14dad69e-099b-42c9-810b-d002981feec1";
        
        public static string GetId(this PermissionScope scope)
        {
            return scope switch {
                PermissionScope.OfflineAccess => OFFLINEACCESS,
                PermissionScope.Profile => PROFILE,
                _ => String.Empty
            };
        }
    }
    
    public class Test
    {
        static public void Main(String[] args)
        {
            var e = PermissionScope.OfflineAccess;
            var id = e.GetId();
            Console.WriteLine(id);
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.