Bug 26518 - MethodAccessException raised by DynamicMethod.CreateDelegate
Summary: MethodAccessException raised by DynamicMethod.CreateDelegate
Status: RESOLVED FIXED
Alias: None
Product: Runtime
Classification: Mono
Component: JIT ()
Version: unspecified
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Alexander Kyte
URL:
Depends on:
Blocks:
 
Reported: 2015-01-28 16:19 UTC by Miguel de Icaza [MSFT]
Modified: 2015-02-05 12:48 UTC (History)
3 users (show)

Tags:
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:
Status:
RESOLVED FIXED

Description Miguel de Icaza [MSFT] 2015-01-28 16:19:40 UTC
The Microsoft System.Runtiem.Serialization code (available in the wip-serialization branch of Mono) uses System.Reflection.Emit to generate the serializers but throws the following exception:

Unhandled Exception:
System.MethodAccessException: Method `System.Runtime.Serialization.XmlObjectSerializerWriteContext:StoreIsGetOnlyCollection ()' is inaccessible from method `(wrapper dynamic-method) object:WriteCollectionContainerToXml (System.Runtime.Serialization.XmlWriterDelegator,object,System.Runtime.Serialization.XmlObjectSerializerWriteContext,System.Runtime.Serialization.ClassDataContract)'

  at (wrapper managed-to-native) System.Delegate:CreateDelegate_internal (System.Type,object,System.Reflection.MethodInfo,bool)
  at System.Delegate.CreateDelegate (System.Type type, System.Object firstArgument, System.Reflection.MethodInfo method, Boolean throwOnBindFailure, Boolean allowClosed) [0x00000] in <filename unknown>:0 
  at System.Delegate.CreateDelegate (System.Type type, System.Reflection.MethodInfo method, Boolean throwOnBindFailure) [0x00000] in <filename unknown>:0 
  at System.Delegate.CreateDelegate (System.Type type, System.Reflection.MethodInfo method) [0x00000] in <filename unknown>:0 
  at System.Reflection.Emit.DynamicMethod.CreateDelegate (System.Type delegateType) [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.CodeGenerator.EndMethod () [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.XmlFormatWriterGenerator+CriticalHelper.GenerateClassWriter (System.Runtime.Serialization.ClassDataContract classContract) [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.XmlFormatWriterGenerator.GenerateClassWriter (System.Runtime.Serialization.ClassDataContract classContract) [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.ClassDataContract.get_XmlFormatWriterDelegate () [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.ClassDataContract.WriteXmlValue (System.Runtime.Serialization.XmlWriterDelegator xmlWriter, System.Object obj, System.Runtime.Serialization.XmlObjectSerializerWriteContext context) [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue (System.Runtime.Serialization.DataContract dataContract, System.Runtime.Serialization.XmlWriterDelegator xmlWriter, System.Object obj, RuntimeTypeHandle declaredTypeHandle) [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType (System.Runtime.Serialization.DataContract dataContract, System.Runtime.Serialization.XmlWriterDelegator xmlWriter, System.Object obj, RuntimeTypeHandle declaredTypeHandle) [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent (System.Runtime.Serialization.XmlWriterDelegator writer, System.Object graph, System.Runtime.Serialization.DataContractResolver dataContractResolver) [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.DataContractSerializer.InternalWriteObject (System.Runtime.Serialization.XmlWriterDelegator writer, System.Object graph, System.Runtime.Serialization.DataContractResolver dataContractResolver) [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions (System.Runtime.Serialization.XmlWriterDelegator writer, System.Object graph, System.Runtime.Serialization.DataContractResolver dataContractResolver) [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions (System.Runtime.Serialization.XmlWriterDelegator writer, System.Object graph) [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.DataContractSerializer.WriteObject (System.Xml.XmlWriter writer, System.Object graph) [0x00000] in <filename unknown>:0 
  at MonoTests.System.Runtime.Serialization.X.SerializeReadOnlyCollectionMember () [0x00000] in <filename unknown>:0 
  at MonoTests.System.Runtime.Serialization.X.Main () [0x00000] in <filename unknown>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: System.MethodAccessException: Method `System.Runtime.Serialization.XmlObjectSerializerWriteContext:StoreIsGetOnlyCollection ()' is inaccessible from method `(wrapper dynamic-method) object:WriteCollectionContainerToXml (System.Runtime.Serialization.XmlWriterDelegator,object,System.Runtime.Serialization.XmlObjectSerializerWriteContext,System.Runtime.Serialization.ClassDataContract)'

  at (wrapper managed-to-native) System.Delegate:CreateDelegate_internal (System.Type,object,System.Reflection.MethodInfo,bool)
  at System.Delegate.CreateDelegate (System.Type type, System.Object firstArgument, System.Reflection.MethodInfo method, Boolean throwOnBindFailure, Boolean allowClosed) [0x00000] in <filename unknown>:0 
  at System.Delegate.CreateDelegate (System.Type type, System.Reflection.MethodInfo method, Boolean throwOnBindFailure) [0x00000] in <filename unknown>:0 
  at System.Delegate.CreateDelegate (System.Type type, System.Reflection.MethodInfo method) [0x00000] in <filename unknown>:0 
  at System.Reflection.Emit.DynamicMethod.CreateDelegate (System.Type delegateType) [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.CodeGenerator.EndMethod () [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.XmlFormatWriterGenerator+CriticalHelper.GenerateClassWriter (System.Runtime.Serialization.ClassDataContract classContract) [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.XmlFormatWriterGenerator.GenerateClassWriter (System.Runtime.Serialization.ClassDataContract classContract) [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.ClassDataContract.get_XmlFormatWriterDelegate () [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.ClassDataContract.WriteXmlValue (System.Runtime.Serialization.XmlWriterDelegator xmlWriter, System.Object obj, System.Runtime.Serialization.XmlObjectSerializerWriteContext context) [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue (System.Runtime.Serialization.DataContract dataContract, System.Runtime.Serialization.XmlWriterDelegator xmlWriter, System.Object obj, RuntimeTypeHandle declaredTypeHandle) [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType (System.Runtime.Serialization.DataContract dataContract, System.Runtime.Serialization.XmlWriterDelegator xmlWriter, System.Object obj, RuntimeTypeHandle declaredTypeHandle) [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent (System.Runtime.Serialization.XmlWriterDelegator writer, System.Object graph, System.Runtime.Serialization.DataContractResolver dataContractResolver) [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.DataContractSerializer.InternalWriteObject (System.Runtime.Serialization.XmlWriterDelegator writer, System.Object graph, System.Runtime.Serialization.DataContractResolver dataContractResolver) [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions (System.Runtime.Serialization.XmlWriterDelegator writer, System.Object graph, System.Runtime.Serialization.DataContractResolver dataContractResolver) [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions (System.Runtime.Serialization.XmlWriterDelegator writer, System.Object graph) [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.DataContractSerializer.WriteObject (System.Xml.XmlWriter writer, System.Object graph) [0x00000] in <filename unknown>:0 
  at MonoTests.System.Runtime.Serialization.X.SerializeReadOnlyCollectionMember () [0x00000] in <filename unknown>:0 
  at MonoTests.System.Runtime.Serialization.X.Main () [0x00000] in <filename unknown>:0 

This probably means that the code generator in .NET generates dynamic code that calls into their own internal methods, but our checks are too strict.

You can just checkout the branch, and install the "System.ServiceModel.Internals" and "System.Runtime.Serialization" libraries, and run the included program with the following command line:

mcs c.cs -pkg:dotnet -r:System.Runtime.Serialization

using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization;
using System.Xml;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace MonoTests.System.Runtime.Serialization
{
	[DataContract]
	public class CollectionContainer
	{
		Collection<string> items = new Collection<string> ();

		[DataMember]
		public Collection<string> Items {
			get { return items; }
		}
	}


	class X {
	static public void SerializeReadOnlyCollectionMember ()
		{
			XmlWriterSettings settings = new XmlWriterSettings ();
			settings.OmitXmlDeclaration = true;

			DataContractSerializer ser =
				new DataContractSerializer (typeof (CollectionContainer));

			StringWriter sw = new StringWriter ();
			using (XmlWriter w = XmlWriter.Create (sw, settings)) {
				ser.WriteObject (w, null);
			}
			
			Console.WriteLine ("T1\n{0}\n{1}", "<CollectionContainer i:nil='true' xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization' />".Replace ('\'', '"'), sw.ToString (), "#1");

			sw = new StringWriter ();
			var c = new CollectionContainer ();
			c.Items.Add ("foo");
			c.Items.Add ("bar");
			using (XmlWriter w = XmlWriter.Create (sw, settings)) {
				ser.WriteObject (w, c);
			}
					   Console.WriteLine ("T2\n{0}\n{1}", "<CollectionContainer xmlns:i='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://schemas.datacontract.org/2004/07/MonoTests.System.Runtime.Serialization'><Items xmlns:d2p1='http://schemas.microsoft.com/2003/10/Serialization/Arrays'><d2p1:string>foo</d2p1:string><d2p1:string>bar</d2p1:string></Items></CollectionContainer>".Replace ('\'', '"'), sw.ToString (), "#2");
		}

	static void Main ()

	{
		SerializeReadOnlyCollectionMember ();
	}
}
}