// 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 class IgnoreAttribute : Attribute { } public abstract class SmartSaver { 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().Where(pi => pi.GetCustomAttributes(typeof(IgnoreAttribute), true).Length == 0).Select(pi => pi.Name)); string vlist = string.Join(",", this.GetType().GetProperties().Where(pi => pi.GetCustomAttributes(typeof(IgnoreAttribute), true).Length == 0).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 : SmartSaver { public int A { get; set; } public string B { get; set; } [Ignore] public string C { get; set; } } public class Program { public static void Main(string[] args) { X o = new X { A = 123, B = "ABC", C = "nothing" }; o.Save(); } } }