fork(1) download
  1. using System;
  2.  
  3. public class Test
  4. {
  5. public static void Main()
  6. {
  7. Console.WriteLine(5.IsDynamicNull());
  8. Console.WriteLine(new DynamicNull().IsDynamicNull());
  9. Console.WriteLine(((object)null).IsDynamicNull());
  10.  
  11. try
  12. {
  13. dynamic d = 5;
  14. Console.WriteLine(Extension.IsDynamicNull(d));
  15. Console.WriteLine("And now the fail");
  16. d.IsDynamicNull();
  17. // your code goes here
  18. }
  19. catch (Exception ex)
  20. {
  21. Console.WriteLine(ex);
  22. }
  23. }
  24. }
  25.  
  26. public class DynamicNull
  27. {
  28.  
  29. }
  30.  
  31. public static class Extension
  32. {
  33. public static bool IsDynamicNull(this object objParam)
  34. {
  35. dynamic obj = objParam;
  36. return (obj == null || obj is DynamicNull);
  37. }
  38. }
Success #stdin #stdout 0.53s 32272KB
stdin
Standard input is empty
stdout
False
True
True
False
And now the fail
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: `int' does not contain a definition for `IsDynamicNull'
at (wrapper dynamic-method) object.CallSite.Target (System.Runtime.CompilerServices.Closure,System.Runtime.CompilerServices.CallSite,object) <0x000bf>
at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid1<object> (System.Runtime.CompilerServices.CallSite,object) <0x00431>
at Test.Main () <0x0032e>