Skip to content

Instantly share code, notes, and snippets.

@garthk
Created May 16, 2012 08:18
Show Gist options
  • Save garthk/2708647 to your computer and use it in GitHub Desktop.
Save garthk/2708647 to your computer and use it in GitHub Desktop.
Create a RoutedEventArgs subclass (e.g. CanExecuteRoutedEventArgs) for unit testing purposes.
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