Skip to content

Commit

Permalink
DisplayNames for Enums
Browse files Browse the repository at this point in the history
Small fixes for descriptions, added extension and utils for handling the attributes
  • Loading branch information
ZsoltGajdacs committed Feb 15, 2021
1 parent c6f3530 commit 9c26775
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 4 deletions.
5 changes: 4 additions & 1 deletion Enums/DataPrecision.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace UsageWatcher
{
public enum DataPrecision
{
[Description("Data is kept for every usages, having a detailed record as per chosen resolution")]
[Description("Data is kept for every usage, having a detailed record as per chosen resolution")]
[Display(Name = "High Precision")]
HighPrecision,
[Description("Not Implemented yet")]
[Display(Name = "Low Precision")]
LowPrecision
}
}
46 changes: 46 additions & 0 deletions Enums/EnumExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;

namespace UsageWatcher
{
/// <summary>
/// Taken from: https://stackoverflow.com/a/19621488
/// </summary>
public static class EnumExtensions
{
/// <summary>
/// Extensible base method, can be used to get the attributes of an Enum
/// </summary>
[SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "This is an extension method")]
public static T GetAttribute<T>(this Enum value) where T : Attribute
{
Type type = value.GetType();
System.Reflection.MemberInfo[] memberInfo = type.GetMember(value.ToString());
object[] attributes = memberInfo[0].GetCustomAttributes(typeof(T), false);

return attributes.Length > 0 ? (T)attributes[0] : null;
}

/// <summary>
/// Retrieves the Description attribute of the given Enum
/// </summary>
[SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "This is an extension method")]
public static string GetDescription(this Enum value)
{
DescriptionAttribute attribute = value.GetAttribute<DescriptionAttribute>();
return attribute == null ? value.ToString() : attribute.Description;
}

/// <summary>
/// Retrieves the DisplayAttribute.DisplayAttribute Name property
/// </summary>
[SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "This is an extension method")]
public static string GetDisplayName(this Enum value)
{
DisplayAttribute attribute = value.GetAttribute<DisplayAttribute>();
return attribute == null ? value.ToString() : attribute.Name;
}
}
}
10 changes: 10 additions & 0 deletions Enums/Resolution.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
using System;
using System.ComponentModel.DataAnnotations;

namespace UsageWatcher
{
[Serializable]
public enum Resolution
{
[Display(Name = "1/2 min")]
HalfMinute = 30000,
[Display(Name = "1 min")]
OneMinute = 60000,
[Display(Name = "2 min")]
TwoMinutes = 120000,
[Display(Name = "3 min")]
ThreeMinutes = 180000,
[Display(Name = "4 min")]
FourMinutes = 240000,
[Display(Name = "5 min")]
FiveMinutes = 3000000,
[Display(Name = "10 min")]
TenMinutes = 600000,
[Display(Name = "15 min")]
FifteenMinutes = 900000,
[Display(Name = "30 min")]
ThirtyMinutes = 1800000
}
}
6 changes: 5 additions & 1 deletion Enums/SavePreference.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace UsageWatcher
{
[Serializable]
public enum SavePreference
{
[Description("No data will be saved, if the app containing the lib is shut down, the data is lost.")]
[Display(Name = "No data save")]
NoSave,
[Description("Data is saved for a single day.")]
[Display(Name = "Save Today")]
KeepDataForToday,
[Description("Data is saved permanently, can be recalled any time.")]
[Description("Data is saved for every day, can be recalled any time.")]
[Display(Name = "Save All")]
KeepDataForever
}
}
41 changes: 41 additions & 0 deletions Enums/Utils/EnumUtil.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace UsageWatcher.Enums.Utils
{
public static class EnumUtil
{
public static IEnumerable<T> GetValues<T>() where T : Enum
{
return Enum.GetValues(typeof(T)).Cast<T>();
}

public static EnumMatchResult<T> GetEnumForString<T>(string enumDisplayNameOrDescription) where T : Enum
{
IEnumerable<T> enumElements = GetValues<T>();

EnumMatchResult<T> result = null;
foreach (T enumElem in enumElements)
{
if (enumElem.GetDisplayName() == enumDisplayNameOrDescription ||
enumElem.GetDescription() == enumDisplayNameOrDescription)
{
result = new EnumMatchResult<T>(enumElem);
}
}

return result;
}
}

public class EnumMatchResult<T> where T : Enum
{
public T FoundEnum { get; private set; }

public EnumMatchResult(T foundEnum)
{
FoundEnum = foundEnum;
}
}
}
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.1.0.0")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: AssemblyVersion("1.2.0.0")]
[assembly: AssemblyFileVersion("1.2.0.0")]
6 changes: 6 additions & 0 deletions UsageWatcher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
<HintPath>packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.Annotations, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>packages\System.ComponentModel.Annotations.5.0.0\lib\net461\System.ComponentModel.Annotations.dll</HintPath>
</Reference>
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml.Linq" />
Expand All @@ -54,6 +58,8 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Enums\DataPrecision.cs" />
<Compile Include="Enums\EnumExtensions.cs" />
<Compile Include="Enums\Utils\EnumUtil.cs" />
<Compile Include="Events\ResolutionPassed.cs" />
<Compile Include="Events\TimerElasped.cs" />
<Compile Include="Helpers\Serializer.cs" />
Expand Down
1 change: 1 addition & 0 deletions packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
<package id="Microsoft.NetCore.Analyzers" version="3.3.0" targetFramework="net48" developmentDependency="true" />
<package id="Microsoft.NetFramework.Analyzers" version="3.3.0" targetFramework="net48" developmentDependency="true" />
<package id="Newtonsoft.Json" version="12.0.3" targetFramework="net48" />
<package id="System.ComponentModel.Annotations" version="5.0.0" targetFramework="net48" />
</packages>

0 comments on commit 9c26775

Please sign in to comment.