6

Is it possible to workaround this error:

public static class LayoutExtensions
{
    /// <summary>
    /// Verifies if an object is DynamicNull or just has a null value.
    /// </summary>
    public static bool IsDynamicNull(this dynamic obj)
    {
        return (obj == null || obj is DynamicNull);
    }

Compile time

Error: The first parameter of an extension method 
       cannot be of type 'dynamic'  
serge
  • 13,940
  • 35
  • 121
  • 205

2 Answers2

5

No. See https://stackoverflow.com/a/5311527/613130

When you use a dynamic object, you can't call an extension method through the "extension method syntax". To make it clear:

int[] arr = new int[5];
int first1 = arr.First(); // extension method syntax, OK
int first2 = Enumerable.First(arr); // plain syntax, OK

Both of these are ok, but with dynamic

dynamic arr = new int[5];
int first1 = arr.First(); // BOOM!
int first2 = Enumerable.First(arr); // plain syntax, OK

This is logical if you know how dynamic objects work. A dynamic variable/field/... is just an object variable/field/... (plus an attribute) that the C# compiler knows that should be treated as dynamic. And what does "treating as dynamic" means? It means that generated code, instead of using directly the variable, uses reflection to search for required methods/properties/... inside the type of the object (so in this case, inside the int[] type). Clearly reflection can't go around all the loaded assemblies to look for extension methods that could be anywhere.

Community
  • 1
  • 1
xanatos
  • 109,618
  • 12
  • 197
  • 280
  • with the "this object" approach, what will not work: the intellisense or runtime error? – serge Apr 20 '15 at 10:10
  • @Serge *When you use a dynamic object, you can't call an extension method through the "extension method syntax".* It is irrelevant the type of extension method. If you have `dynamic b = something;` you can't use **any** `b.ExtensionMethod()` – xanatos Apr 20 '15 at 10:10
  • what do you mean "I can't"? I can, I just ask what will be the result! ;) – serge Apr 20 '15 at 10:11
  • 1
    @Serge You get a `RuntimeBinderException` thrown in your face. BOOM – xanatos Apr 20 '15 at 10:14
1

All classes derived by object class. Maybe try this code

public static bool IsDynamicNull(this object obj)
{
    return (obj == null || obj is DynamicNull);
}
Jacek
  • 11,661
  • 23
  • 69
  • 123
  • yes... is cool... but don't really need to do `"Hello".IsDynamicNull()`... or `myInt.IsDynamicNull`... I would like to limit this behavior for dynamic variables... could be strings, sure... but declared as dynamic.... – serge Apr 20 '15 at 09:16
  • 1
    @Jacek Won't work... See https://ideone.com/HrfsF0 You can't use directly extension methods on `dynamic` objects (see (see http://stackoverflow.com/a/5311527/613130) – xanatos Apr 20 '15 at 09:53
  • @xanatos can you suggest how to save this issue, as i am facing the problem of RuntimeBinderException, i have created extensions for the object class, i was expecting that during runtime it will be able to find the extension and be able to execute it but unfortunately i got the RuntimeBinderException, any suggestions what to do other than passing the variable as a parameter ?? – Syed Abdul Qadeer Nov 06 '18 at 17:35
  • @SyedAbdulQadeer You can't do it. The response from Skeet in the linked post is clear enough. dynamic and extension methods don't play well together. Eric Lippert's response explains why. – xanatos Nov 07 '18 at 10:39