开发者

C# how to know the method Name that the event as attached

开发者 https://www.devze.com 2023-03-13 02:23 出处:网络
I\'d like to know how to find the name of the method that my event as attached. I have the eventInfo of the event

I'd like to know how to find the name of the method that my event as attached.

I have the eventInfo of the event

EventInfo eventInfo = elemento.GetType().GetEvent("Click");

and I know the element that the event as attached.

FrameworkElement elemento = target.TargetObject as FrameworkElement;

After I get the name of the method I will can use this delegate.

Delegate methodDelegate = Delegate.CreateDelegate(eventInfo.EventHandlerType, parentClass, "? ? ? ?");

I have a markupextension class:

[MarkupExtensionReturnType(typeof(FrameworkElement))] [ContentProperty("content")] public class InsereSom : MarkupExtension {

    public InsereSom()
    { }

    [ConstructorArgument("Ligado")]
    public bool Ligado
    {
        get;
        set;
    }

    [ConstructorArgument("Evento")]
    public RoutedEvent Evento
    {
        get;
        set;
    }


    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        IProvideValueTarget target = (IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget));
        FrameworkElement elemento = target.TargetObject as FrameworkElement;
        RoutedEventHandler metodo = new RoutedEventHandler(EventoInsereSom);



        elemento.AddHandler(Evento, metodo);

        EventInfo eventInfo = elemento.GetType().GetEvent("Click");

        FrameworkElement parentClass = (MainWindow)((Grid)elemento.Parent).Parent;

        Delegate methodDelegate = Delegate.CreateDelegate(eventInfo.EventHandlerType, parentClass, "Button_Click");

        eventInfo.RemoveEventHandler(elemento, methodDelegate);
        eventInfo.AddEventHandler(elemento, methodDelegate);


        return new System.Windows.Controls.Label();
    }


    public void EventoInsereSom(object sender, RoutedE开发者_如何学GoventArgs e)
    {
        MessageBox.Show("Hello Extension Markup");
    }

}

and this Xaml code

I'd like to put my event on the markup class first. Before the event in the code behind. For that, I need the name of the method that my component at the xaml call. But I can't get it by code.

Are there any way to do it?


An Event is a pair of accessors (much like a Property), so technically you don't even have access to the delegate, let alone the method name. If it's an auto-wrapper (most typical), you can guess at the delegate field name (you can run ildasm to see what it generates by default), but this is not guaranteed to work if the author of the class wrote their own add and remove handlers. For instance, the delegate might be stored in some sort of dictionary instead of in a field.

If you do get access to the underlying delegate field, you can enumerate the delegate (it could have multiple subscribers) and can use the Method property to get the MethodInfo, which has an associated Name, though now you can use the MethodInfo directly to create your own delegate.

0

精彩评论

暂无评论...
验证码 换一张
取 消