TypeLoadException when using Newtonsoft.Json inside of a PCL

Viewed 377

I'm facing a blocking problem... I have a project (iOS, in this case), referencing a PCL (made by me), which itself references Newtonsoft.Json portable version

App --> My PCL --> Newtonsoft.Json

Everything compiles just fine, but at runtime, when using a method of my PCL which calls Newtonsoft.Json, I get a TypeLoadException

Example: A simple PCL, referencing Newtonsoft.Json Portable, with a single class like this:

public class MyClass {
    public int Test() {
        var json = @"{""t"":16,""s"":""test""}";
        var o = JObject.Parse(json);
        return o["t"].Value<int>();
    }
}

A simple App, with a single button, with this event handler:

partial void Test() {
    var c = new MyClass();
    var i = c.Test();
    new UIAlertView("", i.ToString(), null, "Close").Show();
}

I get a TypeLoadException when tapping the button, on line "var i = c.Test();" !

Even weirder, when replacing this:

return o["t"].Value<int>();

with this:

return (int)o["t"];

I'm not getting a TypeLoadException anymore, but a MissingMemberException, with this message:

Missing method Newtonsoft.Json.Linq.JToken::op_Explicit(JToken) in assembly /Users/Ingham/Projects/TestPCL-iOS/TestPCL-iOS/bin/iPhoneSimulator/Debug/Newtonsoft.Json.dll, referenced in assembly /Users/Ingham/Projects/TestPCL-iOS/TestPCL/bin/Debug/TestPCL.dll

I really can't see what I'm doing wrong...

I've tried many combinations of possible profiles for my PCL, but nothing works...

Any idea ? Thanks in advance !

You can download the sample project here (Xamarin.iOS) : https://www.dropbox.com/s/lsclrg5d1cor1dk/TestPCL.zip

0 Answers
Related