0

I am a little confused as to the use of the keyword "object" using in C#. What can and can't it be used for. Can you access a member?

For example, I have made a small snippet of code to deserialize a JSON string.

public static object DeserializeString(string jsonstring)
{
    // Deserialize the data
    object testObject = JsonConvert.DeserializeObject<object>(jsonstring);
    return testObject;
}

When I use the deserializer to create an object I can not access any memebers. I know that this may be a silly question because I haven't defined a class with any members to want to access it. I am trying to avoid making dozens of classes.

Thanks in advance!

4

1 Answer 1

4

I am a little confused as to the use of the keyword "object" using in C#. What can and can't it be used for. Can you access a member?

C# is by default a statically typed language. That is, if you have an expression of a particular compile-time type you may access only the members of that type. This is a safety system; it is saying "I only have a guarantee that the members of this type are available, so prevent me from accessing any other members".

If you have an expression of type object then you may access the members of object -- ToString, GetType and so on.

If you want to turn off this safety system, use dynamic instead of object, and you can then call any member you like; if you call a member that does not exist, your program will crash. When you turn off a safety system the safety system is turned off and you become responsible for guaranteeing safety.


CLARIFICATION: Commenter Brian correctly points out that in the specific case of JSON objects, the default behaviour of a bad dynamic member access is to produce null rather than crashing; of course, much of the time after producing null because a member is unexpectedly missing, the next thing that will happen is a null dereference crash, so there's actually not a whole lot of additional safety margin there, but there is at least some.


My question to you was:

If you call GetType().ToString() on the object, what do you get?

The answer was Newtonsoft.Json.Linq.JObject. So in this case you can access the members of that type like this:

JObject jo = (JObject)testObject;

(Assuming you have using Newtonsoft.Json.Linq; in your directives.)

Again, your program will crash if the object is not actually of that type.

You can do a safe type test with:

if (testObject is JObject jo)
{
  var r = jo.Root;
  ...

I am trying to avoid making dozens of classes.

C# is a statically-typed language; it encourages creation of classes. Again, this is a safety system. Invest in its correct usage!

4
  • 1
    Thanks for your help!
    – hbhojwani
    Feb 7, 2020 at 20:14
  • 1
    Good answer! I agree with all of your responses, however the last one seems short. I think a more full answer would be to also find the correct usage via the correct design patterns just for those who don't know what correct is. Study design patterns is a great next starting point for the readers. Again, that's a better answer than I could have come up with. Feb 7, 2020 at 20:18
  • 2
    I'll note that JObject implements IDynamicMetaObjectProvider, so using dynamic allows access to properties of the json itself; it's giving you more than you'd get out of casting to JObject. Assuming explicit types are used to avoid ambiguity, trying to dynamically access missing properties of a JObject responds with null, rather than throwing an exception. Nonetheless, I would rather use JsonConvert.DeserializeObject<MyCustomObject>. Even if you don't care about type safety, you might care about IDE features (find references, intellisense, refactoring, etc.).
    – Brian
    Feb 7, 2020 at 22:56
  • 1
    @Brian: Good points all. IDynamicMetaObjectProvider is my all time favourite interface name. I was asked to review the implementation and documentation when it was first created and my initial reaction was "that is the worst name I've ever heard"; I mean, sure, it does what it says on the tin -- it provides dynamic meta objects! -- but it is so intimidating and jargonish. Then I reflected a bit more. That's exactly what we want. We want the name to say "if you don't understand exactly what this is doing, do not touch it! This is a sharp tool!". :) Feb 7, 2020 at 23:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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