HamBook – Diff between revs 1 and 13
?pathlinks?
Rev 1 | Rev 13 | |||
---|---|---|---|---|
Line 1... | Line 1... | |||
1 | using System; |
1 | using System; |
|
- | 2 | using System.ComponentModel; |
||
2 | using System.Diagnostics; |
3 | using System.Diagnostics; |
|
3 | using System.Drawing; |
4 | using System.Drawing; |
|
4 | using System.IO; |
5 | using System.IO; |
|
- | 6 | using System.Linq; |
||
5 | using System.Reflection; |
7 | using System.Reflection; |
|
6 | using System.Threading; |
8 | using System.Threading; |
|
7 | using System.Threading.Tasks; |
9 | using System.Threading.Tasks; |
|
8 | using System.Windows.Forms; |
10 | using System.Windows.Forms; |
|
9 | using Microsoft.Win32; |
11 | using Microsoft.Win32; |
|
Line 146... | Line 148... | |||
146 | } |
148 | } |
|
Line 147... | Line 149... | |||
147 | |
149 | |
|
148 | throw new TimeoutException($"Failed to get a access to {path} within {timeout}ms."); |
150 | throw new TimeoutException($"Failed to get a access to {path} within {timeout}ms."); |
|
Line -... | Line 151... | |||
- | 151 | } |
||
- | 152 | |
||
- | 153 | /////////////////////////////////////////////////////////////////////////// |
||
- | 154 | // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
||
- | 155 | /////////////////////////////////////////////////////////////////////////// |
||
- | 156 | /// <summary> |
||
- | 157 | /// Get the description from an enumeration value. |
||
- | 158 | /// </summary> |
||
- | 159 | /// <param name="value">an enumeration value</param> |
||
- | 160 | /// <returns>the description or the empty string</returns> |
||
- | 161 | public static string GetDescriptionFromEnumValue(Enum value) |
||
- | 162 | { |
||
- | 163 | var attribute = value.GetType() |
||
- | 164 | .GetRuntimeField(value.ToString()) |
||
- | 165 | .GetCustomAttributes(typeof(DescriptionAttribute), false) |
||
- | 166 | .SingleOrDefault() as DescriptionAttribute; |
||
- | 167 | |
||
- | 168 | return attribute?.Description; |
||
- | 169 | } |
||
- | 170 | |
||
- | 171 | /////////////////////////////////////////////////////////////////////////// |
||
- | 172 | // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
||
- | 173 | /////////////////////////////////////////////////////////////////////////// |
||
- | 174 | /// <summary> |
||
- | 175 | /// Get enumeration value from its name attribute. |
||
- | 176 | /// </summary> |
||
- | 177 | /// <typeparam name="T">the enumeration type</typeparam> |
||
- | 178 | /// <param name="description">the description of a member</param> |
||
- | 179 | /// <param name="comparison">the string comparison to use</param> |
||
- | 180 | /// <returns>the value or the default of T if case no name attribute found</returns> |
||
- | 181 | public static T GetEnumValueFromDescription<T>(string description, |
||
- | 182 | StringComparison comparison = StringComparison.OrdinalIgnoreCase) |
||
- | 183 | { |
||
- | 184 | var field = typeof(T).GetRuntimeFields() |
||
- | 185 | .SelectMany(f => f.GetCustomAttributes( |
||
- | 186 | typeof(DescriptionAttribute), |
||
- | 187 | false), |
||
- | 188 | ( |
||
- | 189 | f, |
||
- | 190 | a) => new { Field = f, Att = a }) |
||
- | 191 | .SingleOrDefault(a => string.Equals(((DescriptionAttribute)a.Att) |
||
- | 192 | .Description, |
||
- | 193 | description, |
||
- | 194 | comparison)); |
||
- | 195 | |
||
- | 196 | return (T)field?.Field.GetValue(Activator.CreateInstance<T>()); |
||
149 | } |
197 | } |
|
150 | |
198 | |
|
151 | #endregion |
199 | #endregion |
|
152 | } |
200 | } |