Bug 35856 - SerializationException when deserializing a generic static method in a non-generic class
Summary: SerializationException when deserializing a generic static method in a non-ge...
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib ()
Version: 4.0.0
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2015-11-13 22:10 UTC by Kai
Modified: 2015-11-19 09:44 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 Kai 2015-11-13 22:10:09 UTC
When deserializing a generic static method of a non-generic class, Mono system.reflection throws a "method not found" exception. The detailed code and error message are attached below. The same code runs without error on .NET 4.5.2

==============================================Example Code====================================

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace SerDeTest
{
    class Program
    {
        private class FuncHelper
        {
            public static IEnumerable<T> GetValue<T>(IEnumerable<KeyValuePair<int, T>> kvs)
            {
                return kvs.Select(x => x.Value);
            }
        }

        static void Main(string[] args)
        {
            Func<IEnumerable<KeyValuePair<int, int>>, IEnumerable<int>> func = FuncHelper.GetValue<int>;

            var formatter = new BinaryFormatter();

            var outStream = new MemoryStream();
            formatter.Serialize(outStream, func);

            byte[] bytes = outStream.ToArray();

            var inStream = new MemoryStream(bytes);
            var f = (Func<IEnumerable<KeyValuePair<int, int>>, IEnumerable<int>>)formatter.Deserialize(inStream);

            Console.WriteLine(f);
        }
    }
}

=======================================Exception=========================================
The exception when running on Mono 4.0.5:

Unhandled Exception:
System.Runtime.Serialization.SerializationException: Could not find method 'System.Collections.Generic.IEnumerable`1[System.Int32] GetValue[Int32](Int32, System.Collections.Generic.IEnumerable`1[System.Collections.Generic.KeyValuePair`2[System.Int32,System.Int32]])' in type 'SerDeTest.Program+ZK'
  at System.Reflection.MemberInfoSerializationHolder.GetRealObject (StreamingContext context) [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.ObjectRecord.LoadData (System.Runtime.Serialization.ObjectManager manager, ISurrogateSelector selector, StreamingContext context) [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.ObjectManager.DoFixups () [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadNextObject (System.IO.BinaryReader reader) [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadObjectGraph (BinaryElement elem, System.IO.BinaryReader reader, Boolean readHeaders, System.Object& result, System.Runtime.Remoting.Messaging.Header[]& headers) [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.NoCheckDeserialize (System.IO.Stream serializationStream, System.Runtime.Remoting.Messaging.HeaderHandler handler) [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize (System.IO.Stream serializationStream) [0x00000] in <filename unknown>:0 
  at SerDeTest.Program.Main (System.String[] args) [0x00000] in <filename unknown>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: System.Runtime.Serialization.SerializationException: Could not find method 'System.Collections.Generic.IEnumerable`1[System.Int32] GetValue[Int32](Int32, System.Collections.Generic.IEnumerable`1[System.Collections.Generic.KeyValuePair`2[System.Int32,System.Int32]])' in type 'SerDeTest.Program+ZK'
  at System.Reflection.MemberInfoSerializationHolder.GetRealObject (StreamingContext context) [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.ObjectRecord.LoadData (System.Runtime.Serialization.ObjectManager manager, ISurrogateSelector selector, StreamingContext context) [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.ObjectManager.DoFixups () [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadNextObject (System.IO.BinaryReader reader) [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadObjectGraph (BinaryElement elem, System.IO.BinaryReader reader, Boolean readHeaders, System.Object& result, System.Runtime.Remoting.Messaging.Header[]& headers) [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.NoCheckDeserialize (System.IO.Stream serializationStream, System.Runtime.Remoting.Messaging.HeaderHandler handler) [0x00000] in <filename unknown>:0 
  at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize (System.IO.Stream serializationStream) [0x00000] in <filename unknown>:0 
  at SerDeTest.Program.Main (System.String[] args) [0x00000] in <filename unknown>:0 
===============================================================================================
Comment 1 Marek Safar 2015-11-19 09:44:29 UTC
Already fixed in Mono 4.2