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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:51:03+00:00 2026-05-21T02:51:03+00:00

For creating delegates on the fly, techniques vary from Delegate.CreateDelegate, to Expresion Lambda, DynamicMethod,

  • 0

For creating delegates on the fly, techniques vary from Delegate.CreateDelegate, to Expresion Lambda, DynamicMethod, etc. etc. All of these techniques require that you know the type of the delegate.

I’m trying to convert closed delegates to open delegates generically, and to do to achieve this it seems I need to dynamically create the type of the open delegate before I can actually create the resulting delegate. Consider:

pubic class WeakEvent<TDelegate> where TDelegate : class
{
     public WeakEvent(Delegate aDelegate)
     {
         var dgt = aDelegate as TDelegate;

         if(dgt == null)
             throw new ArgumentException("aDelegate");

         MethodInfo method = dgt.Method;
         var parameters = Enumerable
                         .Repeat(dgt.Target.GetType(),1)
                         .Concat(method.GetParameters().Select(p => p.ParameterType));

         Type openDelegateType = // ???  original delegate, with new 1st arg for @this

         var dm = new DynamicMethod("InnerCode", method.ReturnType, parameters);

         ... your favourite IL code emmisions go here

         var openDelegate = dm.CreateDelegate(openDelegateType);
     }
}

The purpsoe of the above code is to create a new delegate which is identical to the original delegate, but has a new 1st argument for this… i.e. an open version of the previously closed delegate.

Is there a simple way to clone & modify an existing delegate type, or is the nearest solution to build out the generic Func<> and Action<> types?

  • 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-21T02:51:03+00:00Added an answer on May 21, 2026 at 2:51 am

    @Mark – After your 2nd code sample, you said:

    This is a shame, because it means (as
    far as I can tell), that you can’t
    dynamically [re]create delegates with
    ref or out arguments, since the
    generics Func and Action won’t allow
    it.

    That was exactly my problem, see Create C# delegate type with ref parameter at runtime for the solution from User Ani: Expression.GetDelegateType allows ref parameters.

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

Sidebar

Related Questions

I am creating a class (Class B) which is used by another class (Class
I've never done any item delegates in Qt before, and I think the documentation
I am using ASIHTTPRequest in an Objective-C Mac application. I am creating a class
I have declare a variable ( int index) in delegate file of application. And
I'm creating a 3 layer navigation popup controller and on the 3rd popup controller
I'm working with a Navigation Controller based iOS app. There are multiple tableView screens
I'm working on a task that includes image processing. I've found out, that I'm
I have a backend system that currently returns a domain object. I want to
I have a class defined as below: ref class myClass { PictureBox^ pic2; public:
I know this isn't the best place to put this question but here it

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.