Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net 4: How to reference a dynamic object with property named "return"

Tags:

json

c#

.net-4.0

I'm retrieving json from a public api and converting it into a dynamic object using JsonFx.

JsonFx.Json.JsonReader reader = new JsonFx.Json.JsonReader();
dynamic response = reader.Read(jsonAsString);

The json contains a property named return. e.g.

{"result":"success","return":{"high":{"value":"3.85001","value_int":"385001","display":"3.85001\u00a0\u20ac","currency":"EUR"}}

JsonFx creates the dynamic object fine and I can also debug into it and see the values. The problem is when I try to reference the property in my code the compiler complains:

response.return.high.currency
Identifier expected; 'return' is a keyword  

How can I reference the return property without the compiler complaining?

like image 656
AyKarsi Avatar asked Sep 28 '11 06:09

AyKarsi


People also ask

How do you dynamically access an object property in C#?

In C#, you can access dynamic properties by obtaining a PropertyObject reference from the specific object reference using the AsPropertyObject method on the object.

What is dynamic return type in C#?

C# 4.0 (. NET 4.5) introduced a new type called dynamic that avoids compile-time type checking. A dynamic type escapes type checking at compile-time; instead, it resolves type at run time. A dynamic type variables are defined using the dynamic keyword.

What is ExpandoObject in C#?

The ExpandoObject class enables you to add and delete members of its instances at run time and also to set and get values of these members. This class supports dynamic binding, which enables you to use standard syntax like sampleObject. sampleMember instead of more complex syntax like sampleObject.

What is the difference between dynamic type variables and object type variables in C#?

Dynamic types are similar to object types except that type checking for object type variables takes place at compile time, whereas that for the dynamic type variables takes place at runtime.


1 Answers

Try [email protected].

You need to append @ at the begining of any field wich name is the same as C# keywords.

like image 140
Samich Avatar answered Nov 07 '22 04:11

Samich