forked from LEGO/AsyncAPI.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringExtensions.cs
More file actions
37 lines (32 loc) · 1.04 KB
/
Copy pathStringExtensions.cs
File metadata and controls
37 lines (32 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Copyright (c) The LEGO Group. All rights reserved.
namespace LEGO.AsyncAPI.Writers
{
using System;
using System.Reflection;
using Attributes;
public static class StringExtensions
{
/// <summary>
/// Gets the enum value based on the given enum type and display name.
/// </summary>
/// <param name="displayName">The display name.</param>
public static T GetEnumFromDisplayName<T>(this string displayName)
{
var type = typeof(T);
if (!type.IsEnum)
{
return default;
}
foreach (var value in Enum.GetValues(type))
{
var field = type.GetField(value.ToString());
var displayAttribute = (DisplayAttribute)field.GetCustomAttribute(typeof(DisplayAttribute));
if (displayAttribute != null && displayAttribute.Name == displayName)
{
return (T)value;
}
}
return default;
}
}
}