Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8850463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:54:34+00:00 2026-06-14T12:54:34+00:00

I have a Windows Phone page (MainPage), and in the codebehind, I want to

  • 0

I have a Windows Phone page (MainPage), and in the codebehind, I want to dynamically add an event handler – for example to the MouseEnter event. The trick is, I want this event handler to have the event argument as an object:

    private void MyEventHandler(object sender, object args)
    {

    }

(The reason for this is that this is a simplified example)

This works:

this.MouseEnter += MyEventHandler;

But this throws an ArgumentException:

var handlerType = Type.GetType("System.EventHandler`1").
                  MakeGenericType(typeof(EventArgs));
this.GetType().GetEvent("Hold").AddEventHandler(
                  this, Delegate.CreateDelegate(handlerType, this, "MyEventHandler"));

And the message of the exception is:
“Cannot bind to the target method because its signature or security transparency is not compatible with that of the delegate type.” If I change the signature of the event handler, all works, but I want to keep it as “object” or “dynamic”.

How can I add the event handler with the AddEventHandler call?

Any help is highly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T12:54:35+00:00Added an answer on June 14, 2026 at 12:54 pm

    Keyword is delegate covariance:

    class Program
    {
      static void Main(string[] args)
      {
        var t = new Test();
    
        var e = t.GetType().GetEvent("TestEvent");
        var te = Delegate.CreateDelegate(e.EventHandlerType, new EventHandler(MyMethod).Method);
    
        e.AddEventHandler(t, te);
    
        t.RaiseTest();
      }
    
      static void MyMethod(object sender, object args)
      {
    
      }
    }
    
    public class Test
    {
      public class MyEventArgs : EventArgs { }
    
      public void RaiseTest()
      {
        var e = TestEvent;
        if (e != null)
          e(this, new MyEventArgs());
      }
    
      public event EventHandler<MyEventArgs> TestEvent;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Page in a Windows Phone class library. This page has an
I have a page in my windows phone 7 app. This page sets it's
I have one textbox on a windows phone page and I want to show
Has anyone encountered this problem with Windows Phone 7.1? I have a simple page
I have a button on my registration Page (Windows Phone), in 30% of pushing
I have a windows phone application and I need to add a user control
I'm developing a Windows Phone application . I have defined this on App.xaml: <nav:UriMapping
In my Windows Phone 7.5 application i want to have a merged ResourceDictionary filled
I want to create a tabbed UI in windows phone app's single page. Closest
I have a multi page windows phone application. I have a main page and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.