CSharp · Console
using System;
using System.Globalization;
using System.Linq;
public class Program
{
public void Main(string[] args)
{
string input = @"ID:56;DATE:25.10.2017;NUMBER:00000094";
Entry entry = Parser.Parse<Entry>(input);
entry.Dump();
}
}
public class Entry
{
public int ID { get; set; }
public DateTime DATE { get; set; }
public int NUMBER { get; set; }
}
public static class Parser
{
public static T Parse<T>(string s) where T : new()
{
var args = s.Split(';').Select(x => x.Split(':')).ToDictionary(x => x[0], x => x[1]);
var props = from prop in typeof (T).GetProperties()
join arg in args on prop.Name equals arg.Key
select new {prop, arg.Value};
var obj = new T();
foreach (var t in props)
{
var value = Convert.ChangeType(t.Value, t.prop.PropertyType, new CultureInfo("ru-RU"));
t.prop.SetValue(obj, value);
}
return obj;
}
}