Mega Code Archive

 
Categories / C# / Reflection
 

Use reflection to get the Elapsed event

using System; using System.Reflection; using System.Reflection.Emit; public class Example {     private static object timer;     public static void Main()     {         string fullName = "";         foreach (Assembly assem in AppDomain.CurrentDomain.GetAssemblies())         {             if (assem.GetName().Name == "mscorlib")             {                 fullName = assem.FullName;             }         }         Assembly sys = Assembly.Load("System" + fullName.Substring(fullName.IndexOf(",")));         Type t = sys.GetType("System.Timers.Timer");         timer = Activator.CreateInstance(t);         EventInfo eInfo = t.GetEvent("Elapsed");     } }