// 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.Linq; namespace E { public abstract class Saver { private static string Q(object v) { return (v is string) ? ("'" + v + "'") : v.ToString(); } public void Save() { string clznam = this.GetType().Name; string flist = string.Join(",", this.GetType().GetProperties().Select(pi => pi.Name)); string vlist = string.Join(",", this.GetType().GetProperties().Select(pi => pi.GetValue(this, null)).Select(v => Q(v))); string sql = string.Format("INSERT INTO {0}({1}) VALUES({2});", clznam, flist, vlist); Console.WriteLine(sql); } } public class X : Saver { public int A { get; set; } public string B { get; set; } } public class Program { public static void Main(string[] args) { X o = new X { A = 123, B = "ABC" }; o.Save(); } } }