More extension reorg

This commit is contained in:
adelikat 2014-07-03 17:35:49 +00:00
parent 90ab7d17b1
commit ebee9b9ded
4 changed files with 38 additions and 35 deletions

View File

@ -68,8 +68,9 @@ namespace BizHawk.Client.EmuHawk
{
if (PakTypeDropdown.SelectedItem != null) // Null check for designer
{
return EnumHelper.GetValueFromDescription<N64ControllerSettings.N64ControllerPakType>(
PakTypeDropdown.SelectedItem.ToString());
return PakTypeDropdown.SelectedItem
.ToString()
.GetEnumFromDescription<N64ControllerSettings.N64ControllerPakType>();
}
return N64ControllerSettings.N64ControllerPakType.NO_PAK;

View File

@ -4,9 +4,11 @@ using System.Linq;
using System.Windows.Forms;
using BizHawk.Common.StringExtensions;
using BizHawk.Common.ReflectionExtensions;
using BizHawk.Emulation.Cores.Nintendo.N64;
using BizHawk.Client.EmuHawk.ControlExtensions;
using BizHawk.Client.Common;
using BizHawk.Client.EmuHawk.ControlExtensions;
using BizHawk.Common;
@ -322,11 +324,11 @@ namespace BizHawk.Client.EmuHawk
ss.CoreType = CoreTypeDropdown.SelectedItem
.ToString()
.GetValueFromDescription<N64SyncSettings.CORETYPE>();
.GetEnumFromDescription<N64SyncSettings.CORETYPE>();
ss.RspType = RspTypeDropdown.SelectedItem
.ToString()
.GetValueFromDescription<N64SyncSettings.RSPTYPE>();
.GetEnumFromDescription<N64SyncSettings.RSPTYPE>();
PutSettings(s);
PutSyncSettings(ss);

View File

@ -10,36 +10,6 @@ namespace BizHawk.Common
{
public static class EnumHelper
{
/// <summary>
/// Gets an enum from a description attribute
/// </summary>
/// <typeparam name="T">The type of the enum</typeparam>
/// <param name="description">The description attribute value</param>
/// <returns>An enum value with the given description attribute, if no suitable description is found then a default value of the enum is returned</returns>
/// <remarks>http://stackoverflow.com/questions/4367723/get-enum-from-description-attribute</remarks>
public static T GetValueFromDescription<T>(this string description)
{
var type = typeof(T);
if (!type.IsEnum) throw new InvalidOperationException();
foreach (var field in type.GetFields())
{
var attribute = Attribute.GetCustomAttribute(field,
typeof(DescriptionAttribute)) as DescriptionAttribute;
if (attribute != null)
{
if (attribute.Description == description)
return (T)field.GetValue(null);
}
else
{
if (field.Name == description)
return (T)field.GetValue(null);
}
}
return default(T);
}
public static IEnumerable<string> GetDescriptions<T>()
{
var vals = Enum.GetValues(typeof(T));

View File

@ -51,6 +51,36 @@ namespace BizHawk.Common.ReflectionExtensions
return descriptions[0].Description;
}
/// <summary>
/// Gets an enum from a description attribute
/// </summary>
/// <typeparam name="T">The type of the enum</typeparam>
/// <param name="description">The description attribute value</param>
/// <returns>An enum value with the given description attribute, if no suitable description is found then a default value of the enum is returned</returns>
/// <remarks>http://stackoverflow.com/questions/4367723/get-enum-from-description-attribute</remarks>
public static T GetEnumFromDescription<T>(this string description)
{
var type = typeof(T);
if (!type.IsEnum) throw new InvalidOperationException();
foreach (var field in type.GetFields())
{
var attribute = Attribute.GetCustomAttribute(field,
typeof(DescriptionAttribute)) as DescriptionAttribute;
if (attribute != null)
{
if (attribute.Description == description)
return (T)field.GetValue(null);
}
else
{
if (field.Name == description)
return (T)field.GetValue(null);
}
}
return default(T);
}
/// <summary>
/// Takes an object and determines if it has methodName as a public method
/// </summary>