diff --git a/Commands/CommandReader.cs b/Commands/CommandReader.cs index 1cd8132ad..d84bad819 100644 --- a/Commands/CommandReader.cs +++ b/Commands/CommandReader.cs @@ -1,5 +1,4 @@ using System; -using System.ComponentModel; namespace ClassicalSharp.Commands { @@ -60,23 +59,10 @@ namespace ClassicalSharp.Commands { } } - public bool NextOf( out T value ) { - string next = Next(); - if( next == null ) { - value = default( T ); - return false; - } - - TypeConverter converter = TypeDescriptor.GetConverter( typeof( T ) ); - try { - value = (T)converter.ConvertFromInvariantString( next ); - return true; - } catch( FormatException ) { - value = default( T ); - } catch( NotSupportedException ) { - value = default( T ); - } - return false; + public bool NextOf( out T value, TryParseFunc parser ) { + bool success = parser( Next(), out value ); + if( !success ) value = default( T); + return success; } bool MoveNext() { diff --git a/Utils/Utils.cs b/Utils/Utils.cs index a6e5c471c..0ab007e89 100644 --- a/Utils/Utils.cs +++ b/Utils/Utils.cs @@ -15,6 +15,7 @@ namespace ClassicalSharp { public delegate TResult Func(); public delegate TResult Func( T1 arg1 ); public delegate TResult Func( T1 arg1, T2 arg2 ); + public delegate bool TryParseFunc( string s, out T value ); // ################################################################ public static class Utils {