Bug 6031 - Operation is not valid due to the current state of the object - when using RestSharp in 5.3.4
Summary: Operation is not valid due to the current state of the object - when using Re...
Alias: None
Product: iOS
Classification: Xamarin
Component: XI runtime ()
Version: 5.3.x
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2012-07-07 14:35 UTC by Nic Wise
Modified: 2012-07-11 14:42 UTC (History)
3 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Nic Wise 2012-07-07 14:35:21 UTC
I'm using RestSharp, which uses SimpleJson.

From time to time, I get this when it's deserializing an object (not always, and not always the same object). I get this in the simulator and on the phone. MT 5.3.4.

Operation is not valid due to the current state of the object

  at System.MonoType.GetGenericTypeDefinition () [0x0000d] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System/MonoType.cs:673 
  at RestSharp.Deserializers.JsonDeserializer.<BuildList>m__31 (System.Type x) [0x00000] in /Users/nic/code/git/GitHub/RestSharp/RestSharp/Deserializers/JsonDeserializer.cs:202 
  at System.Linq.Enumerable.First[Type] (IEnumerable`1 source, System.Func`2 predicate, Fallback fallback) [0x00013] in /Developer/MonoTouch/Source/mono/mcs/class/System.Core/System.Linq/Enumerable.cs:812 
  at System.Linq.Enumerable.First[Type] (IEnumerable`1 source, System.Func`2 predicate) [0x00007] in /Developer/MonoTouch/Source/mono/mcs/class/System.Core/System.Linq/Enumerable.cs:843 
  at RestSharp.Deserializers.JsonDeserializer.BuildList (System.Type type, System.Object parent) [0x0000c] in /Users/nic/code/git/GitHub/RestSharp/RestSharp/Deserializers/JsonDeserializer.cs:202 
  at RestSharp.Deserializers.JsonDeserializer.Map (System.Object target, IDictionary`2 data) [0x0031d] in /Users/nic/code/git/GitHub/RestSharp/RestSharp/Deserializers/JsonDeserializer.cs:153 
  at RestSharp.Deserializers.JsonDeserializer.Deserialize[Categories] (IRestResponse response) [0x000bf] in /Users/nic/code/git/GitHub/RestSharp/RestSharp/Deserializers/JsonDeserializer.cs:51 
  at RestSharp.RestClient.Deserialize[Categories] (IRestRequest request, IRestResponse raw) [0x0004a] in /Users/nic/code/git/GitHub/RestSharp/RestSharp/RestClient.cs:505 

(does it have anything to do with the Enumerable.First[Type] bit? that was an old bug/issue in MT I think?)

The code in question is from RestSharp, but it doesn't happen all the time - it's intermittant.

private IList BuildList(Type type, object parent)
			var list = (IList)Activator.CreateInstance(type);
>>>>		var listType = type.GetInterfaces().First(x => x.GetGenericTypeDefinition() == typeof(IList<>));
			var itemType = listType.GetGenericArguments()[0];

			if (parent is IList) {

https://github.com/restsharp/RestSharp/blob/master/RestSharp/Deserializers/JsonDeserializer.cs, line 202.

Any ideas? is it something I'm doing wrong (or RestSharp is), or a MT bug?

I'll try to confirm it tomorrow in 5.2.12.
Comment 1 Nic Wise 2012-07-08 07:16:58 UTC
OK, I've just tried this with 5.2.12, and I get the same error in the same place. So I suspect it's not a MT problem - more likely a problem with RestSharp. Is anyone from Xamarin able to comment?
Comment 2 Nic Wise 2012-07-08 09:12:43 UTC
Appears to be the result of a recent change in RestSharp

Comment 3 Nic Wise 2012-07-11 10:00:56 UTC
Anyone from Xamarin home/around?
Comment 4 Marek Safar 2012-07-11 13:23:16 UTC
We need more details.

Could you modify RestSharp code and find out what are values of following in the case of the exception.

x.GetType ()
x.ToString ()
Comment 5 Nic Wise 2012-07-11 13:49:56 UTC
Hi Marek

I've unrolled the Linq thing, and got this:

private IList BuildList(Type type, object parent)
			var list = (IList)Activator.CreateInstance(type);
//var listType = type.GetInterfaces().First(x => x.GetGenericTypeDefinition() == typeof(IList<>));

            var types = type.GetInterfaces();
            Type listType;
            foreach(var t in types)

                if (t.GetGenericTypeDefinition() == typeof(IList<>))
                    listType = t;
                } catch (Exception ex)


And results are:

2012-07-11 18:47:14.784 MobileAgent[58721:22103] System.MonoType
2012-07-11 18:47:14.784 MobileAgent[58721:22103] System.Collections.IList
2012-07-11 18:47:14.785 MobileAgent[58721:22103] System.MonoType
2012-07-11 18:47:20.995 MobileAgent[58721:22103] System.Collections.Generic.List`1[FreeAgent.BankAccount]

Sometimes, it happens on the first or second item (I'm connecting to a REST service about 9 times, for different calls). Sometimes the 9th. It's kinda random.

Is that any help? Happy to try other stuff.
Comment 6 Nic Wise 2012-07-11 13:54:01 UTC
Oh, and that was in the simulator, BTW. I think it happens more often on the device.
Comment 7 Marek Safar 2012-07-11 14:09:58 UTC
Yep, that's buggy RestSharp.

There is type.GetInterfaces() call on any type followed by immediate t.GetGenericTypeDefinition() which is wrong because GetGenericTypeDefinition will throw when type is not generic.

e.g. typeof (System.Collections.IList).GetGenericTypeDefinition ()

The reason why it's failing only sometimes is that GetInterfaces can return implemented interfaces in any order. If you are unlucky and any non-generic interface is before IList<> in the array you get the crash.
Comment 8 Nic Wise 2012-07-11 14:20:08 UTC
OK, however this works in Mono and in the full framework. So....?

Should I (they) do:


loop over: if (type is Generic) => GetGenericTypeDefinition = typeof(ILIst<>)


Currently, it's only an issue with MonoTouch (and maybe MonoDroid, but I'm not sure)
Comment 9 Nic Wise 2012-07-11 14:20:46 UTC
BTW, that pseudo code kinda ment: check each one to see if it's generic. If it is, call it. If it's not, dont.
Comment 10 Marek Safar 2012-07-11 14:24:47 UTC
yes, change the lambda body to

x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IList<>)
Comment 11 Nic Wise 2012-07-11 14:42:49 UTC
Brilliant. That works - thanks. I'll submit a PR to RestSharp.

Thanks Marek. Much appreciated.