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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:12:32+00:00 2026-05-12T00:12:32+00:00

After googling and landing on SO and having read this other question Is it

  • 0

After googling and landing on SO and having read this other question

Is it possible to build a correct Delegate from a MethodInfo if you didn’t know the number or types of parameters at compile time?

More on this: can this be done elegantly without the use of Reflection.Emit or type builders?

This is sorta a bummer for me because Delegate.CreateDelegate requires me to specify the correct Delegate type as the first parameter or else it would throw exceptions or invoke an incorrect method.

I’m building some ninja gears and this would helps a lot… Thanks!


Here’s a generic solution:

/// <summary>
/// Builds a Delegate instance from the supplied MethodInfo object and a target to invoke against.
/// </summary>
public static Delegate ToDelegate(MethodInfo mi, object target)
{
    if (mi == null) throw new ArgumentNullException("mi");

    Type delegateType;

    var typeArgs = mi.GetParameters()
        .Select(p => p.ParameterType)
        .ToList();

    // builds a delegate type
    if (mi.ReturnType == typeof(void)) {
        delegateType = Expression.GetActionType(typeArgs.ToArray());

    } else {
        typeArgs.Add(mi.ReturnType);
        delegateType = Expression.GetFuncType(typeArgs.ToArray());
    }

    // creates a binded delegate if target is supplied
    var result = (target == null)
        ? Delegate.CreateDelegate(delegateType, mi)
        : Delegate.CreateDelegate(delegateType, target, mi);

    return result;
}

Note: I am building a Silverlight application that would replace a built-years-ago javascript application in which I have multiple Javascript interfaces that calls into the same Silverlight [ScriptableMember] method.

All those legacy JS interfaces need to be supported as well as new interface for accessing new features, so something that automatically setups the JS interface and “delegates” the call to the right Silverlight method would helps speed up work a lot.

I can’t post code here, so that’s the summary.

  • 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-12T00:12:32+00:00Added an answer on May 12, 2026 at 12:12 am

    To be honest, if you don’t know the type at compile time, there isn’t a huge amount of benefit in creating a Delegate. You don’t want to use DynamicInvoke; it will be about as slow as reflection. The main exception to this is when there is a delegate-type lurking in the shadows, for example when subscribing to an event – in which case EventInfo makes this available.

    For info, in .NET 3.5 on Expression, there is:

    Expression.GetActionType(params Type[] typeArgs);
    Expression.GetFuncType(params Type[] typeArgs)
    

    That might help to an extent:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Linq.Expressions;
    using System.Reflection;
    static class Program {
        static void Main() {
            DoStuff("Test1");
            DoStuff("Test2");
        }
        static void DoStuff(string methodName) {
            MethodInfo method = typeof(Program).GetMethod(methodName);
            List<Type> args = new List<Type>(
                method.GetParameters().Select(p => p.ParameterType));
            Type delegateType;
            if (method.ReturnType == typeof(void)) {
                delegateType = Expression.GetActionType(args.ToArray());
            } else {
                args.Add(method.ReturnType);
                delegateType = Expression.GetFuncType(args.ToArray());
            }
            Delegate d = Delegate.CreateDelegate(delegateType, null, method);
            Console.WriteLine(d);
        }
        public static void Test1(int i, DateTime when) { }
        public static float Test2(string x) { return 0; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think this is a pretty basic question, but after Googling around I can't
After googling, I have only found only this sad forum question . Is it
After googling I haven't found the anwser to this question so I am going
After googling I didn't find answer to my question. So, question. I have site
Anyone know what this error means? after googling I found a bunch of sites
I can't figure this out, and couldn't find a solution after googling around. I'm
After Googling everywhere about how I can fix this I decided to post a
After googling and binging for a whole day about this, and even trying some
I found this solution after googling here but I was horrified: too complicated solution.
My question may be the repeated one here but even after googling i havn't

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.