103

I am going to summarize my problem into the following code snippet.

List<int> list = new List<int>() { 5, 56, 2, 4, 63, 2 };
Console.WriteLine(list.First());

Above code is working fine.

Now I tried the following

dynamic dList = list;
 Console.WriteLine(dList.First());

but I am getting RuntimeBinderException.Why is it so?

3

3 Answers 3

150

To expand on Jon's answer, the reason this doesn't work is because in regular, non-dynamic code extension methods work by doing a full search of all the classes known to the compiler for a static class that has an extension method that matches. The search goes in order based on the namespace nesting and available using directives in each namespace.

That means that in order to get a dynamic extension method invocation resolved correctly, somehow the DLR has to know at runtime what all the namespace nestings and using directives were in your source code. We do not have a mechanism handy for encoding all that information into the call site. We considered inventing such a mechanism, but decided that it was too high cost and produced too much schedule risk to be worth it.

7
  • 3
    Is such a feature in the offing? It would certainly be a breaking change; calls currently throwing RunTimeBinderExceptions would suddenly start working upon recompiling source. Also, would there be any security risks associated with implementing such a feature?
    – Ani
    Mar 15, 2011 at 23:45
  • 5
    @ani: Are we planning on implementing that feature? No. Are there any security risks? I am not aware of any; what sort of security risk did you have in mind? Start by saying who is the attacker and what threat they are making to the user. Feb 1, 2012 at 16:08
  • @EricLippert, I have understood that all dynamic objects are equal to C#: DynamicObject, so there is no way to differentiate them and is one of the reasons why is not possible add extension methods to dynamic, that's right? Oct 28, 2012 at 17:25
  • @EricLippert consider to expand this answer a bit more and add sentence along the lines of "When any of the parameters are dynamic, then all resolutions are deferred until run-time". While it obvious to you this important bit is hard to find anywhere else on SO (see stackoverflow.com/questions/48324768 for example) Jan 18, 2018 at 16:12
  • @AlexeiLevenkov: Though perfectly valid, that point has nothing to do with this question, which is narrowly about extension methods. If you feel that it is important that there be a canonical answer on SO that has the information you mentioned, and there's no existing question that fits, I'd suggest you post a new question that specifically addresses that scenario. Jan 18, 2018 at 16:19
141

To expand on Stecya's answer... extension methods aren't supported by dynamic typing in the form of extension methods, i.e. called as if they were instance methods. However, this will work:

dynamic dList = list;
Console.WriteLine(Enumerable.First(dList));

Of course, that may or may not be useful. If you could give more information about why and how you're trying to use dynamic typing, we may be able to help more.

3
  • I was playing with dynamic object and got this exception.Do you have written any article on this topic, where to use or not use dynamic object Mar 15, 2011 at 12:27
  • 20
    @geek: Personally my rule of thumb is only to use dynamic where you really need to... basically if you would otherwise be accessing members with reflection, that's a big sign. On the other hand, I'm a die-hard static typer - others may suggest less pessimistic policies :)
    – Jon Skeet
    Mar 15, 2011 at 12:36
  • 2
    It might be more readable to cast back to the know type, this works: Console.WriteLine(((List<int>)dList).First()); Or Console.WriteLine((dList as List<int>).First());.
    – AVee
    Jun 18, 2019 at 13:46
20

Because First() is not a method of List. It is defined in Linq Extension to IEnumerable<>

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.