Created
May 3, 2012 19:43
-
-
Save allrameest/2588735 to your computer and use it in GitHub Desktop.
NameValueCollection parsing extensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Specialized; | |
using System.Threading; | |
namespace ConsoleApplication1 | |
{ | |
internal class Program | |
{ | |
private static void Main() | |
{ | |
var settings = new NameValueCollection { | |
{"int", "123"}, | |
{"bool", "true"}, | |
{"threadpriority", "Lowest"}, | |
{"timespan", "00:00:03"}, | |
}; | |
Console.WriteLine( settings.GetInt32( "int" ).Or( 99 ) ); | |
Console.WriteLine( settings.GetInt32( "bool" ).Or( 99 ) ); | |
Console.WriteLine( settings.GetInt32( "erre" ).Or( 99 ) ); | |
//Console.WriteLine(settings.GetInt32("erre").OrThrowException()); | |
Console.WriteLine( settings.GetBoolean( "bool" ).Or( false ) ); | |
Console.WriteLine( settings.GetEnum<ThreadPriority>( "threadpriority" ).Or( ThreadPriority.Normal ) ); | |
Console.WriteLine( settings.GetEnum<ThreadPriority>( "bool" ).Or( ThreadPriority.Normal ) ); | |
Console.WriteLine( settings.GetTimeSpan( "bool" ).Or( TimeSpan.FromMinutes( 5 ) ) ); | |
Console.WriteLine( settings.GetTimeSpan( "timespan" ).Or( TimeSpan.FromMinutes( 5 ) ) ); | |
Console.WriteLine( settings.GetInt32( "int" ).Or( () => 99 ) ); | |
Console.WriteLine( settings.GetInt32( "asdf" ).Or( () => 99 ) ); | |
Console.WriteLine( settings.GetInt32( "asdf" ).OrNull() ); | |
Console.WriteLine( settings.GetInt32( "int" ).OrNull() ); | |
Console.ReadKey(); | |
} | |
} | |
public static class NameValueCollectionExtensions | |
{ | |
public static StructParserResult<int> GetInt32( this NameValueCollection collection, string name ) | |
{ | |
return StructParse<int>( collection, name, int.TryParse ); | |
} | |
public static StructParserResult<bool> GetBoolean( this NameValueCollection collection, string name ) | |
{ | |
return StructParse<bool>( collection, name, bool.TryParse ); | |
} | |
public static StructParserResult<T> GetEnum<T>( this NameValueCollection collection, string name ) where T : struct | |
{ | |
return StructParse<T>( collection, name, Enum.TryParse ); | |
} | |
public static StructParserResult<TimeSpan> GetTimeSpan( this NameValueCollection collection, string name ) | |
{ | |
return StructParse<TimeSpan>( collection, name, TimeSpan.TryParse ); | |
} | |
public static StructParserResult<TimeSpan> GetTimeSpanFromSeconds( this NameValueCollection collection, string name ) | |
{ | |
return StructParse( collection, name, ( string value, out TimeSpan result ) => { | |
int i; | |
if( int.TryParse( name, out i ) ) | |
{ | |
result = TimeSpan.FromSeconds( i ); | |
return true; | |
} | |
result = default(TimeSpan); | |
return false; | |
} ); | |
} | |
private static StructParserResult<T> StructParse<T>( NameValueCollection collection, string name, TryParse<T> tryParse ) where T : struct | |
{ | |
return new StructParserResult<T>( collection[name], tryParse ); | |
} | |
} | |
public delegate bool TryParse<T>( string value, out T result ); | |
public class ParserResult<T> | |
{ | |
private readonly string _value; | |
private readonly TryParse<T> _tryParse; | |
public ParserResult( string value, TryParse<T> tryParse ) | |
{ | |
_value = value; | |
_tryParse = tryParse; | |
} | |
protected bool TryParse(out T result) | |
{ | |
return _tryParse( _value, out result ); | |
} | |
public T Or( T defaultValue ) | |
{ | |
T value; | |
if( TryParse( out value ) ) | |
{ | |
return value; | |
} | |
return defaultValue; | |
} | |
public T Or( Func<T> getDefaultValue ) | |
{ | |
T value; | |
if( TryParse(out value ) ) | |
{ | |
return value; | |
} | |
return getDefaultValue(); | |
} | |
public T OrDefault() | |
{ | |
T value; | |
if( TryParse( out value ) ) | |
{ | |
return value; | |
} | |
return default(T); | |
} | |
public T OrThrowException() | |
{ | |
T value; | |
if( TryParse( out value ) ) | |
{ | |
return value; | |
} | |
throw new Exception( string.Format( "Value '{0}' could not be parsed to '{1}'.", _value, typeof(T) ) ); | |
} | |
} | |
public class ClassParserResult<T> : ParserResult<T> where T : class | |
{ | |
public ClassParserResult( string value, TryParse<T> tryParse ) | |
: base( value, tryParse ) | |
{ | |
} | |
public T OrNull() | |
{ | |
T value; | |
if( TryParse( out value ) ) | |
{ | |
return value; | |
} | |
return null; | |
} | |
} | |
public class StructParserResult<T> : ParserResult<T> where T : struct | |
{ | |
public StructParserResult( string value, TryParse<T> tryParse ) | |
: base( value, tryParse ) | |
{ | |
} | |
public T? OrNull() | |
{ | |
T value; | |
if( TryParse( out value ) ) | |
{ | |
return value; | |
} | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment