0

I have this json object that I can access the elements/nodes using intellisense from within the method but I can't access them outside of it. How do you still make the intellisense work when typing it outside the method? I'm using net core 3.1.

enter image description here

enter image description here

Edit: I've got a dirty alternative to this. If you know a better one let me know it.

enter image description here

Sol
  • 53
  • 7
  • 3
    For dynamic datatype, it will not work, because dynamic will execute at runtime. So here in your case, you need to map into any object – Shashwat Prakash Sep 18 '21 at 08:38
  • When a reference to an object is typed [`dynamic`](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/types/using-type-dynamic), it means that it *is assumed to support any operation.* Then at runtime compiler logic is invoked to determine how to resolve any given operation given the runtime type of the `dynamic` object. Thus there's no real way to support intellisense on a `dynamic` variable since, using purely static analysis, it could be anything and supports anything. See [Dynamic Object Intellisense](https://stackoverflow.com/q/11289995/3744182) for confirmation. – dbc Sep 18 '21 at 17:20
  • For comparison an anonymously typed variable is ***not dynamically referenced***, it is statically typed and all operations using it are resolved at compile type. It's just that the static type is anonymous -- i.e. inferred by the compiler from the code rather than named and declared by the code. See [How does VS intellisense know about the properties an anonymous class have at compile time?](https://stackoverflow.com/q/23916450/3744182). When you assign an anonymous type object to a `dynamic` variable, you strip away the static knowledge of the anonymous type of the object referenced. – dbc Sep 18 '21 at 17:23
  • 1
    In fact I'd say your question is a duplicate of [Dynamic Object Intellisense](https://stackoverflow.com/q/11289995/3744182) and maybe additionally [How does VS intellisense know about the properties an anonymous class have at compile time?](https://stackoverflow.com/q/23916450/3744182). Agree? – dbc Sep 18 '21 at 17:26
  • 1
    But your "dirty alternative", which is to use [named types](https://stackoverflow.com/q/1541728/3744182), isn't a dirty alternative at all. Explicit c# [classes](https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/types/classes) are designed to package and export properties and methods to consumer code which is what you want to do here. Only question is whether to use static classes or instances of non-static classes. The latter is usually better for testing and dependency injection, see [When to use static classes in C#](https://stackoverflow.com/q/241339/3744182). – dbc Sep 18 '21 at 19:31

0 Answers0