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 6047989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:24:47+00:00 2026-05-23T07:24:47+00:00

As is asked in this post , I came up with an example that

  • 0

As is asked in this post, I came up with an example that uses Delegate to speedup Refection in .NET/C#.

However, I got this error when running (compilation works fine). What might be wrong?

Unhandled Exception: System.ArgumentException: type is not a subclass of Multicastdelegate
  at System.Delegate.CreateDelegate (System.Type type, System.Object firstArgument, System.Reflection.MethodInfo method, Boolean throwOnBindFailure, Boolean allowClosed) [0x00000] in <filename unknown>:0 
  at System.Delegate.CreateDelegate (System.Type type, System.Reflection.MethodInfo method, Boolean throwOnBindFailure) [0x00000] in <filename unknown>:0 
  at System.Delegate.CreateDelegate (System.Type type, System.Reflection.MethodInfo method) [0x00000] in <filename unknown>:0 
  at EX.RefTest.DelegateTest () [0x00000] in <filename unknown>:0 
  at EX.RefTest.Main () [0x00000] in <filename unknown>:0 

ADDED

This is the (working) source code thanks to the help from Jon & ChaosPandion.

using System.Reflection;
using System;

namespace EX
{
    public class Hello
    {
        // properties
        public int Valx {get; set;}
        public int Valy {get; set;}
        public Hello()
        {
            Valx = 10; Valy = 20;
        }
        public int Sum(int x, int y)
        {
            Valx = x; Valy = y;
            return (Valx + Valy);
        }
    }
    public class RefTest
    {
        static void DelegateTest()
        {
            Hello h = new Hello();
            Type type = h.GetType();
            MethodInfo m = type.GetMethod("Sum");

            // Wrong! Delegate call = Delegate.CreateDelegate(type, m);
            Delegate call = Delegate.CreateDelegate(typeof(Func<int, int, int>), h, m);
            int res = (int) call.DynamicInvoke(new object[] {100, 200});
            Console.WriteLine("{0}", res);
            // This is a direct method implementation from Jon's post, and this is much faster
            Func<int, int, int> sum = (Func<int, int, int>) Delegate.CreateDelegate(typeof(Func<int, int, int>), h, m);
            res = sum(100, 200);
            Console.WriteLine("{0}", res);
        }
        static void Main()
        {
            DelegateTest();
        }
    }
}

ADDED2

Based on Jon’s answer, I did some performance test to use sum 1000 time.
Compared to the method of using (int) call.DynamicInvoke(new object[] {100, 200});, Func<int, int, int> sum = (Func<int, int, int>) Delegate.CreateDelegate(typeof(Func<int, int, int>), h, m); is 300 times faster.

  • 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-23T07:24:48+00:00Added an answer on May 23, 2026 at 7:24 am

    Hello isn’t a delegate type – so you can’t pass it into Delegate.CreateDelegate as the first argument. You need a delegate type with the same parameter types and return type – in this case, something like:

    delegate int Foo(int x, int y);
    

    or

    Func<int, int, int>
    

    Also note that the overload of Delegate.CreateDelegate you’ve called is meant to be used for a static method – you ought to be using the overload which also takes the target of the delegate (in this case, h).

    I have a blog post which shows using Delegate.CreateDelegate for speeding up access. Note that I wouldn’t expect DynamicInvoke to be significantly faster than calling the method directly with reflection… it’s still going to have to check the parameter types etc. Really you want a strongly-typed delegate type called statically (as opposed to dynamically) to make things really fast.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.