-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extensions.cs
39 lines (34 loc) · 1.15 KB
/
Extensions.cs
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
namespace EventLogger
{
using System;
using System.ComponentModel;
public static class Extensions
{
public static string GetDescription(this object enumerationValue)
{
var type = enumerationValue.GetType();
if (!type.IsEnum)
{
throw new ArgumentException($"{nameof(enumerationValue)} must be of Enum type", nameof(enumerationValue));
}
var memberInfo = type.GetMember(enumerationValue.ToString());
if (memberInfo.Length > 0)
{
var attrs = memberInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attrs.Length > 0)
{
return ((DescriptionAttribute)attrs[0]).Description;
}
}
return enumerationValue.ToString();
}
public static float ToFloat(this int hex)
{
return BitConverter.ToSingle(BitConverter.GetBytes(hex), 0);
}
public static int ToHex(this float f)
{
return BitConverter.ToInt32(BitConverter.GetBytes(f), 0);
}
}
}