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
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 "."offline_access"withPermissionScope.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.