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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:42:35+00:00 2026-05-27T21:42:35+00:00

Is it in anyway possible ( preferably without using any third party libs), to

  • 0

Is it in anyway possible ( preferably without using any third party libs), to create a function whose type is determined at runtime in C#?

e.g

public static void myfunc(var x)
{
     System.Windows.Forms.MessageBox.Show(x); //just an example
}

NOTE: I want the runtime to determine the type of the parameter and do not want to later cast the parameter to another type, as would be necessary if I use generics. e.g I don’t want:

 myfunc<T>(T x) 
 // and then :
 MessageBox.Show((string)m);

UPDATE:
I am actually making a function parser for my programming language, which translates to C# code. In my language, I wanted the parameter types to be determined at runtime always. I was looking for some good C# feature for easy translation.
e.g
in my language syntax:

 function msg << x
     MessageBox.Show x
 end

needed to be translated to something that didn’t ask for a type at compile time, but would need one at runtime.
e.g

public static void msg(var x)
{
     System.Windows.Forms.MessageBox.Show(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-05-27T21:42:36+00:00Added an answer on May 27, 2026 at 9:42 pm

    The keyword introduced for runtime binding in C# 4 is dynamic.

    public static void myfunc(dynamic x)
    

    This allows you to make assumptions about x that are unchecked at compile time but will fail at runtime if those assumptions prove invalid.

    public static void MakeTheDuckQuack(dynamic duck)
    {
        Console.WriteLine(duck.Quack()); 
    }
    

    The assumption made here is that the parameter will have a method named Quack that accepts no arguments and returns a value that can then be used as the argument to Console.WriteLine. If any of those assumptions are invalid, you will get a runtime failure.

    Given classes defined as

    class Duck
    {
        public string Quack()
        {
            return "Quack!";
        }
    }
    
    class FakeDuck
    {
        public string Quack()
        {
            return "Moo!";
        }
    }
    

    And method calls

    MakeTheDuckQuack(new Duck());
    MakeTheDuckQuack(new FakeDuck());
    MakeTheDuckQuack(42);
    

    The first two succeed, as runtime binding succeeds, and the third results in an exception, as System.Int32 does not have a method named Quack.

    Generally speaking, you would want to avoid this if possible, as you’re essentially stipulating that an argument fulfill an interface of some sort without strictly defining it. If you are working in an interop scenario, then perhaps this is what you have to do. If you are working with types that you control, then you would be better served trying to achieve compile time safety via interfaces and/or base classes. You can even use different strategies (such as the Adapter Pattern) to make types you do not control (or cannot change) conform to a given interface.

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

Sidebar

Related Questions

Is there anyway possible to create a way for tomcat to render files out
Is it anyway possible to observe if a UIAlertView is being displayed and call
Is it possible in anyway to bundle an ASP.Net as an installable application which
Is there anyway to make it possible to use .net 3.0 namespaces in a
This is weird and probably not possible but I'll ask anyway. I'm making this
It probably isn't even possible to do this, but I will ask anyway. Is
I know it's possible to reset the My.Settings with Reset() method. Is there anyway
I don't think this is possible, but I'll ask anyway... Is there some way
I was wondering, in java, is it possible to in anyway, simulate pass by
Is this possible in any way? My project folder structure looks like this: my_project:

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.