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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:25:15+00:00 2026-05-27T00:25:15+00:00

Is there a way to invoke a generic function with a type known only

  • 0

Is there a way to invoke a generic function with a type known only at run time?

I’m trying to do something like:

    static  void bar()
    {
        object b = 6;
        string c = foo<typeof(b)>();
    }

    static  string foo<T>()
    {
       return typeof (T).Name;
    }

Basically I want to decide on the type parameter only at run time, but the function I’m calling depends on the type parameter.

Also I know this can be done with reflections… but it’s not the nicest solution to the problem…


I’m sort of looking for dynamic features in C#…


I’m writhing a bridge between two classes the first one is basically a big tree with different types of of objects (composite by interface) the other is a sort of a “super visitor”.
the supper visitor accepts key-value dictioneries that map types to object it looks like:
dic.Add(object value)

and T is not necessarily the type of the value… a lot of times it isn’t…
I know it’s written poorly, but i can’t fix it…
I can work around it, but only at runtime…

I already did it with reflections, but if there’s a better way to do it without them i would be happy to learn…

Thank you

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

    This is a bit of a hack but you can get dynamic to do the reflection work for you by something like,

    class Program
    {
        static void Main(string[] args)
        {
            var b = 6;
            var t = (dynamic)new T();
            var n = t.Foo(b);
        }
    
        class T
        {
            public string Foo<T>(T a)
            {
                return typeof(T).Name;
            }
        }
    }
    

    Here the dynamic call will extract the type of b and use it as a type parameter for Foo().

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

Sidebar

Related Questions

Ts there any way to call a static member on type when I only
Is there a way in android to invoke a numeric only keypad, i.e. a
It's there a way to invoke a method and the return type is strong
Is there a way to Invoke an overloaded method using reflection in .NET (2.0).
Is there a way to dynamically invoke a method in the same class for
Is there way in next piece of code to only get the first record?
is there way how to get name ov event from Lambda expression like with
I need to get the type of a property that will only be known
Is there a way to invoke a method on a background thread ? I
Is there a way to invoke the garbage collector on a specific object within

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.