Skip to content

Instantly share code, notes, and snippets.

@WooCode
Created February 28, 2012 20:32
Show Gist options
  • Save WooCode/1934917 to your computer and use it in GitHub Desktop.
Save WooCode/1934917 to your computer and use it in GitHub Desktop.
TinyParser
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
namespace WooCode.Tools.Test
{
public class SettingsForMyApp
{
public string StoragePath { get; set; }
public int NumberOfChildren { get; set; }
public TimeSpan TimeUntilLunch { get; set; }
public long BigNumber { get; set; }
public DateTime Date { get; set; }
public int[] Ints { get; set; }
public IPAddress Ip { get; set; }
}
class Program
{
static void Main(string[] args)
{
string propertiesMeta = "StoragePath=C:\\mystuff\\;" +
"numberofchildren=45;" +
"timeuntilLunch=00:01:00;" +
"bigNumber=3215345667576;" +
"date=2011/08/01 10:30:20;" +
"ints=32,33,17;" +
"IP=127.0.0.1;";
var settings = new SettingsForMyApp();
// Populate the properties on the settings class with data from the string.
TinyParser.PopulateProperties(settings, propertiesMeta);
Console.WriteLine(settings.StoragePath);
Console.WriteLine(settings.NumberOfChildren);
Console.WriteLine(settings.TimeUntilLunch);
Console.WriteLine(settings.BigNumber);
Console.WriteLine(settings.Date.Year);
Console.WriteLine(settings.Date);
Console.WriteLine(settings.Ints.Length);
Console.WriteLine(settings.Ints[0]);
Console.WriteLine(settings.Ints[1]);
Console.WriteLine(settings.Ints[2]);
Console.WriteLine(settings.Ip);
Console.ReadLine();
/* Ouput
C:\mystuff\
45
00:01:00
3215345667576
2011
2011-08-01 10:30:20
3
32
33
17
127.0.0.1
* */
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
namespace WooCode.Tools
{
public static class TinyParser
{
static TinyParser()
{
KeyValueDelimiter = '=';
KeyValuePairDelimiter = ';';
Converters = new Dictionary<string, Func<string, object>>();
// Add supported types. (Extend with more later)
Converters["String"] = x => x;
Converters["Double"] = x => Double.Parse(x);
Converters["Int32"] = x => Int32.Parse(x);
Converters["Int64"] = x => Int64.Parse(x);
Converters["Double[]"] = SetArrayAndValues<Double>;
Converters["Int32[]"] = SetArrayAndValues<Int32>;
Converters["Int64[]"] = SetArrayAndValues<Int64>;
Converters["String[]"] = SetArrayAndValues<String>;
Converters["IPAddress"] = IPAddress.Parse;
Converters["TimeSpan"] = new TimeSpanConverter().ConvertFrom;
Converters["DateTime"] = new DateTimeConverter().ConvertFrom;
}
public static char KeyValueDelimiter { get; set; }
public static char KeyValuePairDelimiter { get; set; }
public static Dictionary<string, Func<string, object>> Converters { get; private set; }
public static void PopulateProperties(object @object, string inputString)
{
var keyvaluepairs = inputString.Split(KeyValuePairDelimiter).Where(x=> !string.IsNullOrEmpty(x));
var properties = @object.GetType().GetProperties();
foreach (var parameter in keyvaluepairs)
{
var keyvalue = parameter.Split(KeyValueDelimiter);
var objectKeyValuePair = new KeyValuePair<string, string>(keyvalue[0].Trim().ToLower(), keyvalue[1].Trim());
// Get all Properties.
var property = properties.SingleOrDefault(x => x.Name.ToLower() == objectKeyValuePair.Key);
if (property == null) continue;
// Convert Value to match property type.
var propertyValue = CreateTypeWithValue(property.PropertyType.Name, objectKeyValuePair.Value);
// Set the property.
property.SetValue(@object, propertyValue, null);
}
}
private static T[] SetArrayAndValues<T>(string arrayData)
{
var arrayType = typeof(T);
var arrayitems = arrayData.Split(',');
T[] items = arrayitems.Select(x => (T)CreateTypeWithValue(arrayType.Name, x)).ToArray();
return items;
}
private static object CreateTypeWithValue(string propertyType, string data)
{
return !Converters.ContainsKey(propertyType) ? null : Converters[propertyType](data);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment