// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.IO; using System.Reflection; using System.Text.RegularExpressions; namespace E { public class Something { public bool engineStopped { get; set; } public bool carCrashed { get; set; } public bool @default { get; set; } private readonly Regex re = new Regex(@"(\w+):(\w+)=(""(.*)""|)"); public void Process(string line) { Match m = re.Match(line); string check = m.Groups[1].Value; string clazz = m.Groups[2].Value; bool usearg = (m.Groups[3].Value == @"""" + m.Groups[4].Value + @""""); string arg = m.Groups[4].Value; if((bool)this.GetType().GetProperty(check).GetValue(this, null)) { Type t = Assembly.GetExecutingAssembly().GetType("E." + clazz); if(usearg) { ConstructorInfo ci = t.GetConstructor(new Type[] { typeof(string) } ); object o = ci.Invoke(BindingFlags.Default, null, new object[] { arg }, null); } else { ConstructorInfo ci = t.GetConstructor(new Type[0]); object o = ci.Invoke(BindingFlags.Default, null, new object[0], null); } } } public void Process(string fnm, string section) { using(StreamReader sr = new StreamReader(fnm)) { bool active = false; string line; while((line = sr.ReadLine()) != null) { if(active) { if(line.Contains(":") && line.Contains("=")) { Process(line); } else { active = false; } } else if(line == section) { active = true; } } } } public static void Main(string[] args) { Something o = new Something(); o.engineStopped = true; o.carCrashed = true; o.@default = true; o.Process(@"C:\cfg.txt", "LightningMcQueen"); } } public class engine { public engine(string s) { Console.WriteLine("engine " + s); } } public class mechanicalTeam { public mechanicalTeam(string s) { Console.WriteLine("mechanicalTeam " + s); } } public class Drive { public Drive() { Console.WriteLine("Drive"); } } }