Limitation Of Callable Methods On Dynamic Type

You have to be a bit tricky or extra careful when working with the dynamic type, because this is a runtime type which never gets checked by the compiler at compile time.

Hence, writing the code with the dynamic type needs some care. Dynamic is not a safe type, because the compiler never checks type when compiling the code.

Following are the cases, that you can consider, when a method is called on the dynamic type.

  1. Explicitly implemented Interface

    If a class implements an interface explicitly and you want to call the explicitly implemented method using
    dynamic type, it will throw an exception i.e. RuntimeBinderException.
    1. interface IOrder {  
    2.     string ModifyOrderNumber();  
    3. }  
    4. public class Order: IOrder {  
    5.     public string OrderNumber {  
    6.         get;  
    7.         set;  
    8.     }  
    9.     public string IOrder.ModifyOrderNumber() // for explicitly implementation of interface , we define method with Interface.MethodName in the class we implementing interface.  
    10.         {  
    11.             return "FlipKart" + OrderNumber;  
    12.         }  
    13. }  
    14. If you  
    15. try to call your method like below: public class Program {  
    16.         public static void main(string[] args) {  
    17.             IOrder order = new Order() {  
    18.                 OrderNumber = "TestOrder"  
    19.             };  
    20.             dynamic dynamicOrder = order;  
    21.             dynamicOrder.ModifyOrderNumber(); // RuntimeBinder exception. Order does not contain the definition of ModifyOrderNumber().  
    22.         }  
  2. Extension methods

    We can not call the Extension method on the dynamic type. It will throw a RuntimeBinderException. The Extension methods are a compile time concept and not for run time. The Extension methods will give you a RuntimeBinderException error.
    1. static class ExtensionMethods {  
    2.     public static string ToString(this string s) {  
    3.         return string.format("Hi, {0}", s);  
    4.     }  
    5. }  
    6. public class Program {  
    7.     public static void main(string[] args) {  
    8.         dynamic s = "greeting";  
    9.         Console.WriteLine(s.ToString()); // RuntimeBinderException.  
    10.     }  
    11. }  
    12. What you can do isstatic class ExtensionMethods {  
    13.     public static string ToString(this string s) {  
    14.         return string.format("Hi, {0}", s);  
    15.     }  
    16. }  
    17. public class Program {  
    18.     public static void main(string[] args) {  
    19.         dynamic s = "greeting";  
    20.         string formattedString = ExtensionMethods.ToString(s);  
    21.         Console.WriteLine(formattedString);  
    22.     }  
    23. }  
  3. Working with void methods

    It will again give you a RuntimeBinderException if you are trying to call the methods whose return type is void on the dynamic type.
    1. public class Worker {  
    2.     public void DoSomething() {  
    3.         Console.WriteLine("I do nothing");  
    4.     }  
    5. }  
    6. public class Program {  
    7.     public static void main(string[] args) {  
    8.         Worker worker = new Worker();  
    9.         dynamic dynamicWorker = worker;  
    10.         dynamicWorker.DoSomething(); // RuntimeBinder exception. Order does not contain the definition of ModifyOrderNumber().  
    11.     }  
    12. }
Ebook Download
View all
Learn
View all