Bug 795 - System.MethodAccessException when creating generic class
Summary: System.MethodAccessException when creating generic class
Alias: None
Product: Runtime
Classification: Mono
Component: General ()
Version: unspecified
Hardware: PC Windows
: High major
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2011-09-13 21:15 UTC by Jonathan Pryor
Modified: 2017-08-28 15:05 UTC (History)
6 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 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 Jonathan Pryor 2011-09-13 21:15:31 UTC
Reported from Nicklas Petersson:

Add the following to a Mono for Android application, then run:

    public abstract class BaseClass<T> {}
    public class ClassA : BaseClass<ClassB> { }
    public class ClassB : ClassA {}

    public class TestClass
        public void TestMethod()
            var x = new ClassA();

Expected results: It works.

Actual results:

E/mono ( 788): Unhandled Exception: System.MethodAccessException: Method `Mono.Samples.Hello.BaseClass`1<Mono.Samples.Hello.ClassB>:.ctor ()' is inaccessible from method `Mono.Samples.Hello.ClassA:.ctor ()'
E/mono ( 788): 
E/mono ( 788): at Mono.Samples.Hello.TestClass.TestMethod () [0x00000] in <filename unknown>:0 
E/mono ( 788): at Mono.Samples.Hello.HelloActivity.OnCreate (Android.OS.Bundle bundle) [0x00000] in <filename unknown>:0 
E/mono ( 788): at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (IntPtr jnienv, IntPtr native__this, IntPtr native_savedInstanceState) [0x00000] in <filename unknown>:0 
E/mono ( 788): at (wrapper dynamic-method) object:72e09863-b888-4aa4-96b8-099474ae1d79 (intptr,intptr,intptr)
Comment 1 Atsushi Eno 2011-10-04 21:35:08 UTC
I wonder if should even compile.
Comment 2 Jonathan Pryor 2012-01-16 16:12:41 UTC
The same code, with a "driver" Main, also dies on mono master/d901061:

    // same code as above...
    class Demo {
        public static void Main ()
            new TestClass ().TestMethod ();

$ gmcs bxc795.cs
> bxc795.cs(18,21): warning CS0219: The variable `x' is assigned but its value is never used
> Compilation succeeded - 1 warning(s)

$ jon@melchior:csharp$ mono bxc795.exe 
> Unhandled Exception: System.MethodAccessException: Method `BaseClass`1<ClassB>:.ctor ()' is inaccessible from method `ClassA:.ctor ()'
>   at TestClass.TestMethod () [0x00000] in <filename unknown>:0 
>   at Demo.Main () [0x00000] in <filename unknown>:0 

Looks like a JIT bug.
Comment 3 Rodrigo Kumpera 2012-01-23 20:46:34 UTC
Fixed on master, 2-10 and MM.