From ebee9b9ded4a84cd2f0a0c8e5b0037d1bf13a04c Mon Sep 17 00:00:00 2001 From: adelikat Date: Thu, 3 Jul 2014 17:35:49 +0000 Subject: [PATCH] More extension reorg --- .../config/N64/N64ControllerSettingControl.cs | 5 ++-- .../config/N64/N64VideoPluginconfig.cs | 8 +++-- BizHawk.Common/EnumHelper.cs | 30 ------------------- .../Extensions/ReflectionExtensions.cs | 30 +++++++++++++++++++ 4 files changed, 38 insertions(+), 35 deletions(-) diff --git a/BizHawk.Client.EmuHawk/config/N64/N64ControllerSettingControl.cs b/BizHawk.Client.EmuHawk/config/N64/N64ControllerSettingControl.cs index 10c17d7f3d..cfd61ec6e7 100644 --- a/BizHawk.Client.EmuHawk/config/N64/N64ControllerSettingControl.cs +++ b/BizHawk.Client.EmuHawk/config/N64/N64ControllerSettingControl.cs @@ -68,8 +68,9 @@ namespace BizHawk.Client.EmuHawk { if (PakTypeDropdown.SelectedItem != null) // Null check for designer { - return EnumHelper.GetValueFromDescription( - PakTypeDropdown.SelectedItem.ToString()); + return PakTypeDropdown.SelectedItem + .ToString() + .GetEnumFromDescription(); } return N64ControllerSettings.N64ControllerPakType.NO_PAK; diff --git a/BizHawk.Client.EmuHawk/config/N64/N64VideoPluginconfig.cs b/BizHawk.Client.EmuHawk/config/N64/N64VideoPluginconfig.cs index 6fa5a6f161..90e655060f 100644 --- a/BizHawk.Client.EmuHawk/config/N64/N64VideoPluginconfig.cs +++ b/BizHawk.Client.EmuHawk/config/N64/N64VideoPluginconfig.cs @@ -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(); + .GetEnumFromDescription(); ss.RspType = RspTypeDropdown.SelectedItem .ToString() - .GetValueFromDescription(); + .GetEnumFromDescription(); PutSettings(s); PutSyncSettings(ss); diff --git a/BizHawk.Common/EnumHelper.cs b/BizHawk.Common/EnumHelper.cs index 62726222b0..03a6dd2e3d 100644 --- a/BizHawk.Common/EnumHelper.cs +++ b/BizHawk.Common/EnumHelper.cs @@ -10,36 +10,6 @@ namespace BizHawk.Common { public static class EnumHelper { - /// - /// Gets an enum from a description attribute - /// - /// The type of the enum - /// The description attribute value - /// An enum value with the given description attribute, if no suitable description is found then a default value of the enum is returned - /// http://stackoverflow.com/questions/4367723/get-enum-from-description-attribute - public static T GetValueFromDescription(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 GetDescriptions() { var vals = Enum.GetValues(typeof(T)); diff --git a/BizHawk.Common/Extensions/ReflectionExtensions.cs b/BizHawk.Common/Extensions/ReflectionExtensions.cs index 6ce6e063e8..d455f36c03 100644 --- a/BizHawk.Common/Extensions/ReflectionExtensions.cs +++ b/BizHawk.Common/Extensions/ReflectionExtensions.cs @@ -51,6 +51,36 @@ namespace BizHawk.Common.ReflectionExtensions return descriptions[0].Description; } + /// + /// Gets an enum from a description attribute + /// + /// The type of the enum + /// The description attribute value + /// An enum value with the given description attribute, if no suitable description is found then a default value of the enum is returned + /// http://stackoverflow.com/questions/4367723/get-enum-from-description-attribute + public static T GetEnumFromDescription(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); + } + /// /// Takes an object and determines if it has methodName as a public method ///