Created
May 15, 2014 20:50
-
-
Save badamczewski/05dd5b839069dc37f140 to your computer and use it in GitHub Desktop.
How to copy events shit hack code :) - don't use if you don't know what ur doing.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Test | |
{ | |
public event EventHandler SomeEvent; | |
public void Do() | |
{ | |
SomeEvent(this, null); | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Test t = new Test(); | |
t.SomeEvent += new EventHandler(t_SomeEvent); | |
var events = typeof(Test).GetEvents(); | |
var ev = events[0]; | |
var result = Activator.CreateInstance(typeof(Test)); | |
FieldInfo fi = typeof(Test) | |
.GetField(ev.Name, | |
BindingFlags.Static | | |
BindingFlags.NonPublic | | |
BindingFlags.Instance | | |
BindingFlags.Public | | |
BindingFlags.FlattenHierarchy); | |
Delegate del = (Delegate)fi.GetValue(t); | |
ev.AddEventHandler(result, del); | |
t.Do(); | |
((Test)(result)).Do(); | |
} | |
static void t_SomeEvent(object sender, EventArgs e) | |
{ | |
Console.WriteLine(sender); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment