Class EnumHelper
A set of functions and extensions for helping to work with an Enum
Inherited Members
Namespace: LemonEdge.Utils
Assembly: LemonEdge.Utils.dll
Syntax
public static class EnumHelper
Methods
GetCustomEnumValue(string, short)
Returns a value for a specified custom list.
Declaration
public static object GetCustomEnumValue(string enumName, short value)
Parameters
| Type | Name | Description |
|---|---|---|
| string | enumName | The name of the enum/custom list only. The system will prefex LemonEdge.Entities to it. |
| short | value | The short value for the enum value to return |
Returns
| Type | Description |
|---|---|
| object |
GetEnumValue(object)
Returns the value of the specified enum value
Can be the value tiself, or if it has a specified enum value uses that instead, from API.Entities.ICustomListItem.EnumValue
Declaration
public static decimal GetEnumValue(object value)
Parameters
| Type | Name | Description |
|---|---|---|
| object | value | The enum value |
Returns
| Type | Description |
|---|---|
| decimal | The value of the specified enum value |
GetEnumValueFromDisplayName(string, string)
Convert a string to an enum value, using the enum's DisplayAttribute values.
Declaration
public static object GetEnumValueFromDisplayName(string displayName, string typeName)
Parameters
| Type | Name | Description |
|---|---|---|
| string | displayName | If you give the attribute to the enum member like [Display(name="someName")] then you can just pass someNameDisplayAttribute |
| string | typeName | A type name string of the enum |
Returns
| Type | Description |
|---|---|
| object |
GetEnumValueFromDisplayName<T>(string)
Convert a string to an enum value, using the enum's DisplayAttribute values.
Declaration
public static T GetEnumValueFromDisplayName<T>(string displayName) where T : Enum
Parameters
| Type | Name | Description |
|---|---|---|
| string | displayName | If you give the attribute to the enum member like [Display(name="someName")] then you can just pass someNameDisplayAttribute |
Returns
| Type | Description |
|---|---|
| T |
Type Parameters
| Name | Description |
|---|---|
| T | Type of enum |
Exceptions
| Type | Condition |
|---|---|
| ArgumentException |
GetOrderedValues(Type)
Returns an array of all items within a specified Enum ordered by value
Declaration
public static Array GetOrderedValues(Type t)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | t | The enum to enumerate all values of |
Returns
| Type | Description |
|---|---|
| Array | An enumeration of all items within a specified Enum |
GetShortFlaggedValues<T>(T)
Returns all enum values that total the specified enum value
Declaration
public static IEnumerable<T> GetShortFlaggedValues<T>(this T value) where T : Enum
Parameters
| Type | Name | Description |
|---|---|---|
| T | value | The value to return as a combination of smaller bitwise values |
Returns
| Type | Description |
|---|---|
| IEnumerable<T> | All enum values that total the specified enum value |
Type Parameters
| Name | Description |
|---|---|
| T | The type of enum |
Remarks
For example:
public enum MyEnum : short
{
Left = 1,
Right = 2,
Up = 4,
Down = 8,
Vertical = 12
}
public class Example
{
public void GetVertical()
{
var vertical = MyEnum.Vertical;
foreach (var dir in vertical.GetShortFlaggedValues())
{
switch (dir)
{
//matches these two
case MyEnum.Up: break;
case MyEnum.Down: break;
default:
//no other value is enumerated
break;
}
}
}
}
GetValues(Type)
Returns an array of all items within a specified Enum ordered alphabetically
Declaration
public static Array GetValues(Type t)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | t | The enum to enumerate all values of |
Returns
| Type | Description |
|---|---|
| Array | An enumeration of all items within a specified Enum |
GetValuesWithTooltips(Type)
Returns an EnumWrapper<T> for each enum value in the specifid enum t type
Declaration
public static IEnumerable<EnumWrapper> GetValuesWithTooltips(Type t)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | t | The type of the enum |
Returns
| Type | Description |
|---|---|
| IEnumerable<EnumWrapper> | An EnumWrapper<T> for each enum value in the specifid enum |
GetValuesWithTooltips(Type, Type)
Returns an EnumWrapper<T> for each enum value in the specifid enum t type
Declaration
public static IEnumerable<EnumWrapper> GetValuesWithTooltips(Type t, Type propType)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | t | The type of the enum |
| Type | propType | Optionally converts each enum value to a different type such as an int for compatability in the ui |
Returns
| Type | Description |
|---|---|
| IEnumerable<EnumWrapper> | An EnumWrapper<T> for each enum value in the specifid enum |
GetValuesWithTooltips<T>()
Returns an EnumWrapper<T> for each enum value in the specifid enum type
Declaration
public static IEnumerable<EnumWrapper<T>> GetValuesWithTooltips<T>() where T : Enum
Returns
| Type | Description |
|---|---|
| IEnumerable<EnumWrapper<T>> | An EnumWrapper<T> for each enum value in the specifid enum type |
Type Parameters
| Name | Description |
|---|---|
| T | The type of enum |
GetValues<T>()
Returns an enumeration of all items within a specified Enum
Declaration
public static IEnumerable<T> GetValues<T>() where T : Enum
Returns
| Type | Description |
|---|---|
| IEnumerable<T> | An enumeration of all items within a specified Enum |
Type Parameters
| Name | Description |
|---|---|
| T | The enum to enumerate all values of |
Parse(Type, string)
Parses the specified string representation of an enum value and returns if it was valid, and the value
Declaration
public static (bool Valid, object? Result) Parse(Type enumType, string value)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | enumType | The type of enum to parse |
| string | value | The value to parse |
Returns
| Type | Description |
|---|---|
| (bool Valid, object Result) | A tuple indicating if the parse was valid, and the value if it was |
ParseAsShortID<T>(Guid, out T)
Parses the given guid that holds the equivelant of a short value representing a value in the specified enum
T type
Declaration
public static bool ParseAsShortID<T>(this Guid id, out T intID) where T : struct, Enum
Parameters
| Type | Name | Description |
|---|---|---|
| Guid | id | The short guid to try to parse and convert into an enum value |
| T | intID | If the guid was correctly parsed into an enum value this is the value returned |
Returns
| Type | Description |
|---|---|
| bool | True if the guid was valid and could be converted to the specified enum type |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the enum to retrieve from the specified guid short value |
See Also
Parse<T>(string)
Parses an enum string representation and returns the enum value
Declaration
public static T Parse<T>(string value) where T : struct, Enum
Parameters
| Type | Name | Description |
|---|---|---|
| string | value | The string representation of an enum value to parse |
Returns
| Type | Description |
|---|---|
| T | The enum value |
Type Parameters
| Name | Description |
|---|---|
| T | The enum value |
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException | If the value can not be parsed an error is thrown |
ToSafeString(string)
Returns a safe string representation of an enum value
Declaration
public static string ToSafeString(string enumName)
Parameters
| Type | Name | Description |
|---|---|---|
| string | enumName |
Returns
| Type | Description |
|---|---|
| string |
ToShortGuid<T>(T)
Returns the value of an enum as a guid. This is used by LemonEdge for enum values that can also be represented by custom items holding a unique ID, such as images ImageType
Declaration
public static Guid ToShortGuid<T>(this T i) where T : Enum
Parameters
| Type | Name | Description |
|---|---|---|
| T | i | The value of an enum to be converted to a guid equivelant |
Returns
| Type | Description |
|---|---|
| Guid | The value of an enum as a guid. This is used by LemonEdge for enum values that can also be represented by custom items holding a unique ID, such as images ImageType |
Type Parameters
| Name | Description |
|---|---|
| T | The enum type |
See Also
ToValidEnumMember(string, int)
returns a C# valid enum member name from the given name
Declaration
public static string ToValidEnumMember(string name, int index)
Parameters
| Type | Name | Description |
|---|---|---|
| string | name | potential enum member name |
| int | index | the index/order of the enum member |
Returns
| Type | Description |
|---|---|
| string |