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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:13:55+00:00 2026-06-03T14:13:55+00:00

Im trying to add function on runtime , something like that : static void

  • 0

Im trying to add function on runtime , something like that :

static void Main()
{
 dynamic d = new Duck();
 d.Quack =(Action) (() => Console.WriteLine("1")); //decalre a new method on runtime ??
 d.Quack(); 

}

public class Duck : System.Dynamic.DynamicObject
{
   //...
}

‘UserQuery.Duck’ does not contain a definition for ‘Quack’

Isnt dynamic should allow me to do it ?

does brand new ExpandoObject is the only solution ?

i have my Duck class already. how can i make it Expando ? – can i make duck act like expando ?

  • 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-03T14:13:57+00:00Added an answer on June 3, 2026 at 2:13 pm

    You can’t add properties of any type (even functions) to an existing type.

    dynamic d = new Duck();
    d.Quack = "Quack";//fails because there is no such property on duck
    

    You could use ExpandoObject though:

    dynamic d = new ExpandoObject();
    d.Quack = (Action)(() => System.Console.WriteLine("1"));
    d.Quack(); 
    

    Don’t be confused with what the dynamic type does.

    void speak(dynamic d)
    {
     d.Quack();//this is NOT checked at compile time
    }
    

    Now I can do : speak(new Duck()); and speak(new Goose()); , it will compile and run if both Duck and Goose have the method Quack(), if they don’t, it raises an exception. (The same one you get)

    When you call a method/property on a dynamic type, it only resolves that at runtime and doesn’t do a compiler check.

    The ExpandoObject allows you to create properties on the fly.


    To answer your question on the comment, the way I see it is, if you need your own class which needed the ability to create new properties you could inherit from DynamicObject. Like this (adapted from this msdn page):

    class DynamicDuck : DynamicObject 
    {
          Dictionary<string, object> dictionary
               = new Dictionary<string, object>();
        public int Count
        {
            get
            {
               return dictionary.Count;
            }
         }
        public override bool TryGetMember(
              GetMemberBinder binder, out object result)
        {
              string name = binder.Name.ToLower();
                return dictionary.TryGetValue(name, out result);
        }
        public override bool TrySetMember(
             SetMemberBinder binder, object value)
        {
              dictionary[binder.Name.ToLower()] = value;
              return true;
        }
     }
    

    Then you could do:

    dynamic d = new DynamicDuck();
    d.firstname = "Gideon";
    d.Quack = (Action)(() => System.Console.WriteLine("Quack"));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to add a function that will be accessible throughout all parts of
I am trying to set the compilation-error-regexp-alist in a function that I add as
I'm trying to add a function in that would make all text boxes a
I'm trying to add the JavaScript function setTimeout() to my jQuery expression. Without the
I have a jQuery plugin I am trying to add a callback function for.
At www.euroworker.no/order I have been trying to add a tooltip function but it seems
I'm trying to extend jWysiwyg with an function to add a map from Google
I am trying to have a method in my code behind at runtime add
I'm trying to add a code to the_content() function. I tried the following function
I know that polymorphism can add a noticeable overhead. Calling a virtual function is

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.