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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:56:23+00:00 2026-05-12T13:56:23+00:00

Consider the following code: class Program { void Foo<T>() { } static void Main(string[]

  • 0

Consider the following code:

class Program {
    void Foo<T>() { }

    static void Main(string[] args) {
        dynamic p = new Program();
        p.Foo();
    }
}

Note surprisingly, the call to p.Foo() is not valid because the dynamic binder has no way of knowing what type to use for T. The specific failure is:

“The type arguments for method ‘ConsoleApplication1.Program.Foo()’ cannot be inferred from the usage. Try specifying the type arguments explicitly.“

Now my question is: is there a way to specify the generic type, or is such method simply not callable using ‘dynamic’?

  • 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-12T13:56:23+00:00Added an answer on May 12, 2026 at 1:56 pm

    As Jared says, you can specify it in code just as you would for a static call:

    using System;
    
    class Program {
        void Foo<T>() {
            Console.WriteLine(typeof(T));
        }
    
        static void Main(string[] args) {
            dynamic p = new Program();
            p.Foo<string>();
        }
    }
    

    The above code prints System.String.

    Now if you only know T at execution time, it’s slightly harder. If you have an instance of it though, you could use dynamic typing and type inference together:

    using System;
    
    class Program {
        void Foo<T>() {
            Console.WriteLine(typeof(T));
        }
    
        static void Main(string[] args) {
            dynamic p = new Program();
            dynamic v = GetRandomInstance();
    
            // Now to call p.Foo<T> where T is the type of v's value...
            Dummy(v, p);
        }
    
        static void Dummy<T>(T t, Program p) {
            p.Foo<T>();
        }
    
        static object GetRandomInstance() {
            return DateTime.Now.Hour > 10 ? "hello" : (object) 10;
        }
    }
    

    EDIT: Pavel came up with an amazing idea in the comments. You don’t need to come up with an instance of T, just an array. This means you can even use type arguments where you wouldn’t normally be able to get an instance of T (e.g. due to a private constructor):

    using System;
    
    class PrivateConstructor {
        private PrivateConstructor() {}
    }
    
    class Program {
        static void Foo<T>() {
            Console.WriteLine(typeof(T));
        }
    
        static void CallFooProxy<T>(T[] array) {
            Foo<T>();
        }
    
        static void CallFoo(Type t) {
            dynamic array = Array.CreateInstance(t, 0);
            CallFooProxy(array);
        }
    
        static void Main(string[] args) {
            CallFoo(typeof(PrivateConstructor));
        }
    }
    

    Before anyone asks – no, this doesn’t let you call Foo<Enumerable> dynamically – you still can’t use a static class as a type argument, even if you try to delay the attempt until execution time 🙂

    If all of that fails for some reason, it’s back to reflection as normal… get the method info, call MakeGenericMethod and invoke it.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Question 1a : How to calculate the new average rating:… May 12, 2026 at 8:18 pm
  • Editorial Team
    Editorial Team added an answer Yes, they are included in source code available at http://pecl.php.net/package/APC.… May 12, 2026 at 8:18 pm
  • Editorial Team
    Editorial Team added an answer AntiForgeryToken prevents a malicious site to trick a user to… May 12, 2026 at 8:18 pm

Related Questions

Consider the following code: class Program { static void Main(string[] args) { Department deathStar
The idea behind this code is that it uses an extension method to instantiate
Consider the console application below, featuring a method with a generic catch handler that
I already posted a question regarding it, but at that time I haven't have

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.