2

If dynamic resolves to object at compile time, and all .NET types extend object, why does dynamic not act like an object with regards to IntelliSense? Whenever I use dynamic I get a message saying "dynamic expression. this will be resolved at runtime". Surely it should also display object members?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Matthew Layton
  • 39,871
  • 52
  • 185
  • 313

3 Answers3

5

Intellisense do not work in dynamic type. It is resolved at Runtime. Dynamic type work for static types as well as anonymous types.

If intellisense would have worked, it would have defied the very purpose of dynamicity.

I think you should read Jon Skeet answer about object vs dynamic here

Community
  • 1
  • 1
Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
  • 4
    *"If intellisense would have worked, it would have defied the very purpose of dynamicity."* -- Interestingly enough, it does work for Javascript, and everything in Javascript is dynamic. What properties and methods IntelliSense offers for auto-completion is determined by actually _executing_ the code, which could be done in the case of C# as well, I believe. – John Weisz Jun 09 '16 at 09:54
  • 2
    Agree with @JohnWhite we could have intellisense for js and typescript. In this sense intellisense is just a hint that it most likely be the structure we define in jsdoc. And C# dynamic object should have something similar – Thaina Yu Dec 19 '16 at 11:00
1

I would suspect it doesn't provide these members because there could be an arbitrary number of overloads to any of the methods on object - which it obviously can't know of at intellisense time. So it could be displaying the wrong intellisense information for a particular method invocation.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
0

Ultimately because all dynamic operations use dynamic dispatch, i.e. not compile-time linking, and therefore there really is no guarantee that any member will actually exist - even ToString - because the dynamic layer of an object is free to intercept/replace/remove even the basic operations of object. As a result, for the intellisense window to attempt to display anything would be disingenuous.

Yes it's compiled as object, but that's more because (nearly) all objects are object (or can be boxed as such) and so therefore the runtime knows that, whatever the dynamic object is, it can be held as an object reference.

But if you want to use the object members, or if you want intellisense to show them, you'd have to cast to object first - which in itself would be a dynamic operation as well.

That's not to say it's not possible to display intellisense members for dynamic languages, of course it is (I believe Iron Python can), it's just that in C# it's not - and reasonably so.

Andras Zoltan
  • 41,961
  • 13
  • 104
  • 160