0

I'm trying to wrap my head around how types and generic methods work with Newtonsoft.Json.
I have the following example base class:

public class Foo
{
    public Type Expected;
    public object Result;
    public Foo()
    {
        Expected = typeof(RootObject);
    }
    public void Process()
    {
        string json = @"{
        'Foo': 'oof',
        'Baz': 0,
        'Bar': [
        'Yeet',
        'Skeet'
        ]
        }";
        Result = JsonConvert.DeserializeObject<Expected>(json); // -> doesnt work
    }
}

I'm planning on calling the Process() method from a derived class after I might've changed the Expected class type.
However, as this question points out I found after a little research, passing a variable Type to a generic method isn't that easy. After looking into it a little more, I updated the affected line to this:

Result = ((Func<string, object>)JsonConvert.DeserializeObject).Method.MakeGenericMethod(Expected).Invoke(null, new object[]{json});

It works! •ᴗ•
But the resulting variable still is a plain object, which is quite difficult to handle. I've been trying to cast the result to the Expected type, but it just won't let me.

Result = (Expected)((Func<string, object>)JsonConvert.DeserializeObject).Method.MakeGenericMethod(Expected).Invoke(null, new object[]{json});

tells me that the type or namespace Expected couldn't be found. As a more naive approach, I tried to declare my Result property like so

public Expected Result;

but my dreams didn't come true.
I'm not sure how I should further approach this to get the Result object to be of type Expected so I can access its properties easily, or if there is a better way to go about this variable-type-generic-method problem as a whole.
I have the full (sample) markup on dotnetfiddle, thank you for taking your time!

3x071c
  • 955
  • 10
  • 40
  • The type of the variable named `Result` is determined at compile time, and you have declared it to be `object`. What do you hope to accomplish by "casting" the deserialized object that you are assigning to it? – John Wu Mar 27 '20 at 22:32
  • @JohnWu As I said, I’m hoping to be able to read out and change properties of the class I passed to the deserializer function (with dot notation). I know I’m currently defining the Result as an object since I can’t define it to be of a variable type (Expected), which is why I’m seeking assistance. Thanks for asking anyway, I hope I was able to clarify my original question. Feel free to ask more :) – 3x071c Mar 27 '20 at 22:38
  • @JohnWu you might want to look at my dotnetfiddle markup I linked in my question – 3x071c Mar 27 '20 at 22:39
  • 2
    Any reason to not just make `Foo` a generic `Foo` and skip the whole complicated stuff you're trying to do here? –  Mar 27 '20 at 22:47
  • Let's say there were some way to define `Result` as having a type of "Whatever `Expected` is at run time." What methods would you expect to pop up in the intellisense as you write the code? If you then write code that treats it as an `ExtendedObject`, but at runtime it's actually a `RootObject`, what do you expect the method call to do? – John Wu Mar 27 '20 at 22:47
  • 2
    Changed the fiddle to an example using generics: https://dotnetfiddle.net/NlrZQj –  Mar 27 '20 at 22:55
  • @Knoop the updated fiddle looks very promising, I hope it won’t bother you if I check back in ~12 hrs? You could add it as an answer in the meantime... – 3x071c Mar 27 '20 at 23:01
  • 1
    you can use user-defined implicit casting operator here as well..https://dotnetfiddle.net/uExw5M – Brett Caswell Mar 27 '20 at 23:04
  • 1
    probably better to do it on the base though https://dotnetfiddle.net/NJatMQ – Brett Caswell Mar 27 '20 at 23:11
  • @Knoop could you post this as answer so I can accept it? Thanks! – 3x071c Mar 28 '20 at 15:30
  • 1
    @BrettCaswell Thank you, that sounds really interesting! However, Knoops answer suits the question better. I couldn't be more grateful about the quick and miraculous responses! – 3x071c Mar 28 '20 at 15:31
  • @SearchingSolutions Sadly I'm lying in bed at the moment, hopefully I'll be able to post an answer tomorrow. Otherwise feel free to post an answer yourself from my code:-) –  Mar 28 '20 at 17:35

0 Answers0