// 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.Reflection; namespace E { public class InitableAttribute : Attribute { } [Initable] public class A { public static void Init() { Console.WriteLine("Hello from A"); } } public class B { } [Initable] public class C { public static void Init() { Console.WriteLine("Hello from C"); } } public class D { } [Initable] public class E { public static void Init() { Console.WriteLine("Hello from E"); } } public class Program { public static void AllInit() { foreach(Type typ in Assembly.GetExecutingAssembly().GetExportedTypes()) { if(Attribute.GetCustomAttribute(typ, typeof(InitableAttribute)) != null) { typ.InvokeMember("Init", BindingFlags.Public | BindingFlags.Static | BindingFlags.InvokeMethod, null, null, new object[0]); } } } public static void Main(string[] args) { AllInit(); } } }