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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:11:36+00:00 2026-06-02T05:11:36+00:00

How can I use a dynamic as a generic? This var x = something

  • 0

How can I use a dynamic as a generic?

This

var x = something not strongly typed;
callFunction<x>();

and this

dynamic x = something not strongly typed;
callFunction<x>();

both produce this error

Error   1   The type or namespace name 'x' 
could not be found (are you missing a using directive or an assembly reference?)

What can I do to x to make it legitimate enough to be used in <x>?

  • 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-06-02T05:11:38+00:00Added an answer on June 2, 2026 at 5:11 am

    You could use type inference to sort of trampoline the call:

    dynamic x = something not strongly typed;
    CallFunctionWithInference(x);
    
    ...
    
    static void CallFunctionWithInference<T>(T ignored)
    {
        CallFunction<T>();
    }
    
    static void CallFunction<T>()
    {
        // This is the method we really wanted to call
    }
    

    This will determine the type argument at execution time based on the execution-time type of the value of x, using the same kind of type inference it would use if x had that as its compile-time type. The parameter is only present to make type inference work.

    Note that unlike Darin, I believe this is a useful technique – in exactly the same situations where pre-dynamic you’d end up calling the generic method with reflection. You can make this one part of the code dynamic, but keep the rest of the code (from the generic type on downwards) type-safe. It allows one step to be dynamic – just the single bit where you don’t know the type.

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

Sidebar

Related Questions

Can I use dynamic data type in WF 4.0? And if this is possible
I am having trouble setting up this Dynamic Linq Library so i can use
If I want to make a rule dynamic so i can use assert after
how can I create a dynamic lambda expression to pass to use in my
How can be possible to use Google analytic with blogger dynamic views? I cannot
I have a method that given a delegate Type argument (not a generic), it
I use Entity Framework 4. How can I perform a Generic Where Lambda Clause.?
I can use dynamic find_by_* methods in rails, such as Company.find_by_name_and_zip_and_phone , but how
When I have an IQueryable<T> (generic) I can use the Take method to return
I can use FireFox and FireBug, in a pane, I can open a .css

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.