-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCsEnumExtensions.cs
More file actions
40 lines (35 loc) · 1.6 KB
/
CsEnumExtensions.cs
File metadata and controls
40 lines (35 loc) · 1.6 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
38
39
40
//*****************************************************************************
//* Code Factory SDK
//* Copyright (c) 2022 CodeFactory, LLC
//*****************************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CodeFactory.DotNet;
using CodeFactory.DotNet.CSharp;
using CodeFactory.DotNet.CSharp.FormattedSyntax;
namespace CodeFactory.Formatting.CSharp
{
/// <summary>
/// Extensions class that provides common automation tasks rolled up under standard extension methods that support the <see cref="CsEnum"/> model.
/// </summary>
public static class CsEnumExtensions
{
/// <summary>
/// Extension method that will lookup the value of an enumeration by the enumeration type name.
/// </summary>
/// <param name="source">The target <see cref="IDotNetEnum"/> model to get the enumeration value from.</param>
/// <param name="enumName">The target numerical named item to use to lookup the enumeration value.</param>
/// <returns>The target enumeration value or null if it could not be found.</returns>
public static string CSharpFormatEnumValue(this CsEnum source, string enumName)
{
if (source == null) return null;
if (string.IsNullOrEmpty(enumName)) return null;
if (!source.Values.Any()) return null;
var enumValue = source.Values.Where(v => string.Equals(v.Name, enumName)).Select(v => v.Value).FirstOrDefault();
return enumValue;
}
}
}