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

  • Home
  • SEARCH
  • 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 6019977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:30:01+00:00 2026-05-23T03:30:01+00:00

When i call CreateDelegate(delegateType) i get a System.ArgumentException , which according to MSDN is

  • 0

When i call CreateDelegate(delegateType) i get a System.ArgumentException, which according to MSDN is because the delegateType has the wrong number of parameters or the wrong parameter types.

The strange part is the code I’m using is almost all copied from MSDN. My function as whole:

public static void AssertRaisesEvent(Action action, object obj, string eventName, NumberOfTimes numberOfTimesRaised)
{
    eventCounter = 0;
    EventInfo eventInfo = obj.GetType().GetEvent(eventName);
    Type tDelegate = eventInfo.EventHandlerType;

    Type returnType = GetDelegateReturnType(tDelegate);
    if (returnType != typeof(void))
        throw new ApplicationException("Delegate has a return type.");

    var handler =
        new DynamicMethod("CompletedHandler",
            typeof(int),
            GetDelegateParameterTypes(tDelegate),
            obj.GetType());

    // Generate a method body. This method loads a string, calls 
    // the Show method overload that takes a string, pops the 
    // return value off the stack (because the handler has no
    // return type), and returns.
    //
    ILGenerator ilgen = handler.GetILGenerator();
    FieldInfo counterFieldInfo = typeof (AssertionHelpers).GetField("eventCounter",
                                                                    BindingFlags.NonPublic | BindingFlags.Static);
    ilgen.Emit(OpCodes.Ldfld, counterFieldInfo);
    ilgen.Emit(OpCodes.Ldc_I4, 1);
    ilgen.Emit(OpCodes.Add);
    ilgen.Emit(OpCodes.Pop);
    ilgen.Emit(OpCodes.Ret);

    // Complete the dynamic method by calling its CreateDelegate
    // method. Use the "add" accessor to add the delegate to
    // the invocation list for the event.
    //

    var delParams = GetDelegateParameterTypes(tDelegate);
    var handlerParams = handler.GetParameters();

    Delegate dEmitted = handler.CreateDelegate(tDelegate);
    eventInfo.GetAddMethod().Invoke(obj, new Object[] { dEmitted });

    ...

As you can see the comments are even there. As you also can see i have delParams and handlerParams variables which have the same number of parameters of the same type.

What is going on here?

MSDN: http://msdn.microsoft.com/en-us/library/ms228976.aspx

EDIT:
The event im trying to bind to:

private NullTransaction transaction;

public delegate void CompletedEventHandler(object testParam);

internal class NullTransaction : ITransaction
{
public event CompletedEventHandler Completed;
    public void Dispose()
    {
        // no implementation
    }

    public void Complete()
    {
        // no implementation
    if(Completed != null)
            Completed.Invoke(this);
    }
}
  • 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-05-23T03:30:02+00:00Added an answer on May 23, 2026 at 3:30 am

    Most events don’t return anything – in fact you assert that it has no return-type. You then declare your custom method (handler) as returning int, and try to bind it to a delegate that doesn’t return an int. This won’t work.

    Also; your stack isn’t valid for returning an int, since you “pop” the result.

    i.e. I created a test with

    public event EventHandler SomeEvent;
    

    and bound to it; so then here:

    Delegate dEmitted = handler.CreateDelegate(tDelegate);
    

    you’ll find that tDelegate is EventHandler. That doesn’t match handler, which returns int.


    Re the stack (comments); consider:

    ilgen.Emit(OpCodes.Ldfld, counterFieldInfo); <=== should be ldsfld, by the way
    ilgen.Emit(OpCodes.Ldc_I4, 1); // stack is now [counter] [1]
    ilgen.Emit(OpCodes.Add); // stack is now [counter + 1]
    ilgen.Emit(OpCodes.Pop); // stack is now empty
    ilgen.Emit(OpCodes.Ret); // return
    

    You’ve loaded two values, added them up, thrown the result away, and then returned. But you haven’t returned the int that you claim to – this will fail IL inspection.


    If you change:

    var handler =
        new DynamicMethod("CompletedHandler",
            null,
            GetDelegateParameterTypes(tDelegate),
            obj.GetType());
    

    and:

    ilgen.Emit(OpCodes.Ldsfld, counterFieldInfo);
    ilgen.Emit(OpCodes.Ldc_I4_1);
    ilgen.Emit(OpCodes.Add);
    ilgen.Emit(OpCodes.Stsfld, counterFieldInfo);
    ilgen.Emit(OpCodes.Ret);
    

    then it might work as you intend.

    Also; this is simpler:

    Delegate dEmitted = handler.CreateDelegate(tDelegate);
    eventInfo.AddEventHandler(obj, dEmitted);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Cannot call action method 'System.Web.Mvc.PartialViewResult FooT' on controller 'Controller' because the action method is
I call my JavaScript function. Why do I sometimes get the error 'myFunction is
Function call: $trdata .= $this->table_td($tddata, 1, $td); Function: public function table_td($data = '', $parameters
Call me crazy, but I'm the type of guy that likes constructors with parameters
How do I define a DynamicMethod for a delegate that has an out -parameter,
Call this from a QWidget: Parser->xmlParser(_layout, xmlPath, comingFrom, mapCount); , but get two No
How to call function B from inside A? Function B has elements from A.
I'm trying to get this to work. $.fn.createDelegate = function(scope){ var fn = this;
I call a function from a function in C++ that has the line getline(cin,name)
Call me a 'n00b', but I am new to creating Script Controls. I want

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.