Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using Dynamic to add methods?

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 ?

like image 530
Royi Namir Avatar asked May 09 '12 09:05

Royi Namir


People also ask

Is it good practice to use dynamic in C#?

The short answer is YES, it is a bad practice to use dynamic.

What are the uses of dynamic methods?

The dynamic method is a procedure for the determination of the masses of asteroids. The procedure gets its name from its use of the Newtonian laws of the dynamics, or motion, of asteroids as they move around the Solar System.


1 Answers

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"));
like image 130
gideon Avatar answered Oct 12 '22 22:10

gideon