나는 System.Windows.Interactivity.EventTrigger.EventName이 문자열을 취하는 것을 봅니다. 또한, 나는 그들이 반사를 통해 그 사건에 연결될 필요가 있다고 상상할 것이다.System.Windows.Interactivity.EventTrigger.EventName은 효율적으로 리플렉션을 사용하거나 사용하지 않고 어떻게 작동합니까?
반사에는 특히 성능이 나쁜 이름이 약간 있기 때문에 System.Windows.Interactivity.EventTrigger의 성능 문제가 있습니다. 아래 코드는 이벤트에 연결하는 가장 효율적인 방법이며, Expression Blend의 SDK는 특별한 작업을 수행합니까? 그 이후
public class EventToCommand
{
public static readonly DependencyProperty EventNameProperty = DependencyProperty.RegisterAttached("EventName", typeof(string), typeof(EventToCommand), new UIPropertyMetadata(OnEventHandlerChanged));
public static string GetEventName(DependencyObject obj)
{
return (string)obj.GetValue(EventNameProperty);
}
public static void SetEventName(DependencyObject obj, string value)
{
obj.SetValue(EventNameProperty, value);
}
private static void OnEventHandlerChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
Type objType = obj.GetType();
var info = objType.GetEvent((string)args.NewValue);
MethodInfo handler = typeof(EventToCommand).GetMethod("OnEventHandlerRaised", BindingFlags.Instance | BindingFlags.NonPublic);
var o = Activator.CreateInstance(typeof(EventToCommand));
Delegate d = Delegate.CreateDelegate(info.EventHandlerType, o, handler);
MethodInfo addHandler = info.GetAddMethod();
object[] addHandlerArgs = { d };
addHandler.Invoke(obj, addHandlerArgs);
}
private void OnEventHandlerRaised(object sender, EventArgs args)
{
// Is it a unique instance?
Console.WriteLine(value);
}
Guid value = Guid.NewGuid();
public EventToCommand()
{
}
}
그런 다음 어떻게 이벤트를 명령에 전달합니까? – kevindaub