Bug 23437 - PCL Assembly TypeLoadException: Could not load type 'System.Object' from assembly 'System.Runtime
Summary: PCL Assembly TypeLoadException: Could not load type 'System.Object' from asse...
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib ()
Version: 3.8.0
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2014-09-28 07:37 UTC by Johannes Rudolph
Modified: 2018-03-15 09:37 UTC (History)
6 users (show)

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

Sample project reproducing the issue (6.72 MB, application/zip)
2014-09-28 07:37 UTC, Johannes Rudolph

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 GitHub or Developer Community 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 Johannes Rudolph 2014-09-28 07:37:14 UTC
Created attachment 8219 [details]
Sample project reproducing the issue

The attached project reproduces an issue in the following Scenario: 

* PCL projet (Profile 47) uses various standard Nuget packages (HttpClient, Async support etc.)
* NUnit test project (.NET 4.5) references and executes PCL, using NitoAsyncEx.AsyncContext (NitoAsyncEx is a PCL itself).

When running the test, I receive the following error: 

TypeLoadException: Could not load type 'System.Object' from assembly 'System.Runtime, Version=, Culture...'
  at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)
  at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00054] in /private/tmp/source-mono-mac-3.10.0-branch/bockbuild-mono-3.10.0-branch/profiles/mono-mac-xamarin/build-root/mono-3.10.0/mcs/class/corlib/System.Reflection/MonoMethod.cs:230 

The compiler should under no circumstances emit a reference to System.Object in the System.Runtime assembly anywhere (it's a primitive type after all) and even then we should see it unified to the mscorlib type. So something's seriously wrong here... and I have a feeling that it is related to the PCL build in some way.
Comment 1 Johannes Rudolph 2014-09-28 08:31:52 UTC
I have taken a further look at the NitoAsyncEx assembly and narrowed it down that there is a problem with Mono executing PCL assemblies compiled for Profile 259. 

In particular, the NitoAsyncEx.dll referenced from the test project contains this (ildasm output):

.class public auto ansi sealed beforefieldinit Nito.AsyncEx.AsyncContext
       extends [System.Runtime]System.Object
       implements [System.Runtime]System.IDisposable

So what I assume happens is that the Mono runtime takes this literally and tries to resolve the System.Runtime.dll from the Library.Test/bin/Debug folder, where it actually finds one that is a PCL shim assembly from the Microsoft.Bcl package that was referenced by the Library project. Library.csproj contains

    <Reference Include="System.Runtime">

So to summarize: Mono has a problem executing assemblies compiled for Profile 259, when as shim System.Runtime.dll is present. 

I can't exactly say whether this is "by design" or a bug in the mono runtime. Will check what the MS CLR does in this case.

PCLs and properly building projects that have them makes my head hurt some times.
Comment 2 Rodrigo Kumpera 2014-11-12 10:15:39 UTC
Martin, any news on this bug?
Comment 3 Johannes Rudolph 2014-11-21 04:15:37 UTC
Any update? Just hit this issue again today in a different context. 

I can confirm this problem still persists in  mono --version
Mono JIT compiler version 3.10.0 ((detached/92c4884 Thu Nov 13 23:27:38 EST 2014)
Comment 4 Johannes Rudolph 2015-02-17 08:34:38 UTC
Any updates?
Comment 5 Toni 2015-06-13 03:41:09 UTC

the same problem here, in this case using System.IO.Stream.

I do not include a reproducing project, but situation is as follows

    - a PCL library with a base class that uses System.IO.Stream.

    - a NUnit test project which derives the base class and also makes use of System.IO.Stream.

In all cases, everything compiles fins, but:

    - if the PCL library is set to profile 259, a TypeLoadException is thrown when executing the tests using NUnit, previous to any test execution, stating that the base class (from the PCL library) cannot be loaded  (both from within Xamarin Studio and invoking nunit-console).

    - if the PCL library is set to profile 78, no exception is thrown and the tests execute.

All this happens with following component versions, under Mac OS:

    - Mono JIT compiler version 4.0.1 ((detached/ed1d3ec Tue May 26 17:22:37 EDT 2015)

    - NUnit-Console version

This also happens with NUnit 2.4.8 (which is shipped by default with this version of mono).
Comment 6 Charlie 2017-03-02 09:14:41 UTC
I'm still having this issue with NitoAsyncEx on Xamarin Studio 6.2 (build 1778) for Mac. Is a fix likely at any point in the future?
Comment 7 Marek Safar 2018-03-15 09:37:39 UTC
This is down to incorrect System.Runtime.dll in bin/Debug folder. I don't know how you got it there but deleting everything and clean build does not copy it there.

We still have an issue as we should not load the assembly but I made a separate ticket for that.