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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:58:12+00:00 2026-05-13T05:58:12+00:00

First, a little introduction. I have to functions: static class C { static void

  • 0

First, a little introduction.

I have to functions:

static class C
{
  static void F1(Type type)
  {
    // Do something to invoke F2<T>
  }
  static void F2<T>()
  {
    // bla bla bla
  }
}

I wish to invoke F1(Type), which in turn should transition to the generic context pertinent to the given type parameter and invoke the generic counterpart F2<T>.

A naive implementation would be a simple reflection, like this (I omit binding flags for clarity):

void F1(Type type)
{
  var f2MethodInfo = typeof(C).GetMethod("F2").MakeGenericMethod(type);
  f2MethodInfo.Invoke(null, new object[0]);
}

A more advanced implementation would store the open method info for F2 – typeof(C).GetMethod("F2") aside, but still it is basically the same thing.

If F1 is invoked many times and we wish to improve the performance, then the standard solution “on the market” employs a dictionary and Delegate.CreateDelegate method, like this:

IDictionary<Type, Action> m_cache = new Dictionary<Type, Action>();
MethodInfo F2MethodInfo = typeof(C).GetMethod("F2");
void F1(Type type)
{
  Action action;
  if (!m_cache.TryGetValue(type, out action))
  {
    m_cache[type] = action = (Action)Delegate.CreateDelegate(typeof(Action), F2MethodInfo.MakeGenericMethod(type));
  }
  action();
}

And now to my question. Is it possible to eliminate the dictionary completely?

For instance, by emitting some fancy function with Reflection.Emit which would receive a Type instance and F2MethodInfo and make the transition inside without and dictionaries? This fancy function should be emitted just once and work for any given type. I wonder if any kind of cache mapping Type to delegates can be eliminated this way.

Thanks.

EDIT

For the sake of this discussion, let us assume that the fancy emitted function knows that it should invoke F2, meaning it does not have to receive its method info. Is it possible to ditch the dictionary then?

  • 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-13T05:58:13+00:00Added an answer on May 13, 2026 at 5:58 am

    Is it possible to make the transition from F(Type) to F without reflection and without a dictionary?

    No, MakeGenericType is the right approach.

    You mention emitting a fancy function to do this; that fancy function will still need to end up with a call instruction, and that requires a MethodInfo in the same way that Delegate.CreateDelegate does.

    You could generate code upfront for a set of known types, maybe using Reflection.Emit, or CodeDom, or even compile-time code generation. But I guess if you knew the types up front, you’d already be taking advantage of that in your dictionary approach, right?

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

Sidebar

Ask A Question

Stats

  • Questions 265k
  • Answers 265k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I fixed this by setting 'ReportOnRetryAmount' in sourceControlErrorHandling May 13, 2026 at 12:22 pm
  • Editorial Team
    Editorial Team added an answer Sometimes asking the question clearly is the best way to… May 13, 2026 at 12:22 pm
  • Editorial Team
    Editorial Team added an answer It's not clear what you are asking, but if you… May 13, 2026 at 12:22 pm

Related Questions

Of late, I've been hearing many good things about functional programming languages such as
I'm not asking for anyone to do this homework for me, but I bring
Okay, I will shortly be starting down the path of windows mobile development. I
Updated question, see below I'm starting a new project and I would like to
I need to write a very simple WinForms application, but I often get stuck

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.