Created
May 16, 2012 08:18
-
-
Save garthk/2708647 to your computer and use it in GitHub Desktop.
Create a RoutedEventArgs subclass (e.g. CanExecuteRoutedEventArgs) for unit testing purposes.
This file contains hidden or 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
protected static T CreateRoutedEventArgs<T>() { | |
var fakeCommand = Substitute.For<ICommand>(); | |
var nonPublicInstance = BindingFlags.NonPublic | BindingFlags.Instance; | |
var ev = (RoutedEvent) Activator.CreateInstance( | |
type: typeof(RoutedEvent), | |
bindingAttr: nonPublicInstance, | |
binder: null, | |
args: new object[4] {null, null, null, null }, | |
culture: null); | |
var args = (T) Activator.CreateInstance( | |
type: typeof(T), | |
bindingAttr: nonPublicInstance, | |
binder: null, | |
args: new object[2] { fakeCommand, null }, | |
culture: null); | |
typeof(RoutedEventArgs) | |
.GetField("_routedEvent", nonPublicInstance) | |
.SetValue(args, ev); | |
return args; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment