using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
readonly List<PropertyInfo> properties = typeof(T).GetProperties().OrderBy(p => p.Name).ToList();
public T Map(string line, char delimitter)
if (String.IsNullOrEmpty(line))
throw new ArgumentNullException("line");
if (Char.IsWhiteSpace(delimitter))
throw new ArgumentException("delimiter");
var splitString = line.Split(delimitter);
if (properties.Count() != splitString.Count())
throw new InvalidOperationException(string.Format("Row has {0} columns but object has {1}", splitString.Count(), properties.Count()));
object obj = Activator.CreateInstance(typeof(T));
for (var i = 0; i < splitString.Count(); i++)
var prop = properties[i];
var propType = prop.PropertyType;
var valType = Convert.ChangeType(splitString[i], propType);
prop.SetValue(obj, valType);
public string A { get; set; }
public string B { get; set; }
internal static void Test()
Test<TestStruct>("123,hello");
internal static void Test<T>(string testString)
Console.WriteLine("Testing mapping of \"{0}\" to {1}", testString, typeof(T));
var mapper = new Mapper<T>();
var test = mapper.Map(testString, ',');
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(test));
public static void Main()