Closed static delegates serialization incorrectly throws InvalidCastExceptions
This code should compile and run correctly on CoreCLR, just like it does on the Desktop CLR:
`using System; using System.IO; using System.Reflection; using System.Runtime.Serialization;
public static class B279085Extensions { public static string StaticExt(this Program thisPtr, string a, string b) { Console.WriteLine(a + b); return thisPtr._id + " = RES = " + a + b; } public static string StaticExt2(this Program thisPtr, string a, string b) { Console.WriteLine(b + a); return thisPtr._id + " = RES = " + b + a; } }
[Serializable] public class Program { internal sealed class DummyFormatterConverter : IFormatterConverter { public object Convert(object value, TypeCode typeCode) { throw new NotImplementedException(); }
public object Convert(object value, Type type)
{
throw new NotImplementedException();
}
public bool ToBoolean(object value)
{
throw new NotImplementedException();
}
public byte ToByte(object value)
{
throw new NotImplementedException();
}
public char ToChar(object value)
{
throw new NotImplementedException();
}
public DateTime ToDateTime(object value)
{
throw new NotImplementedException();
}
public decimal ToDecimal(object value)
{
throw new NotImplementedException();
}
public double ToDouble(object value)
{
throw new NotImplementedException();
}
public short ToInt16(object value)
{
throw new NotImplementedException();
}
public int ToInt32(object value)
{
throw new NotImplementedException();
}
public long ToInt64(object value)
{
throw new NotImplementedException();
}
public sbyte ToSByte(object value)
{
throw new NotImplementedException();
}
public float ToSingle(object value)
{
throw new NotImplementedException();
}
public string ToString(object value)
{
throw new NotImplementedException();
}
public ushort ToUInt16(object value)
{
throw new NotImplementedException();
}
public uint ToUInt32(object value)
{
throw new NotImplementedException();
}
public ulong ToUInt64(object value)
{
throw new NotImplementedException();
}
}
public string _id;
public static string StaticDel(string a, string b)
{
Console.WriteLine(a + b);
return "RES = " + a + b;
}
public static string StaticDel2(string a, string b)
{
Console.WriteLine(b + a);
return "RES = " + b + a;
}
public string InstanceDel(string a, string b)
{
Console.WriteLine(a + a + b + b);
return _id + " = RES = " + a + a + b + b;
}
public string InstanceDel2(string a, string b)
{
Console.WriteLine(b + b + a + a);
return _id + " = RES = " + b + b + a + a;
}
public static int Main()
{
Func<string, string, string> sdel = StaticDel;
sdel += StaticDel2;
var res = sdel("aa", "bb");
Console.WriteLine(res);
SerializationInfo si = new SerializationInfo(typeof(Delegate), new DummyFormatterConverter());
StreamingContext sc = new StreamingContext(StreamingContextStates.All);
sdel.GetObjectData(si, sc);
Type[] constructorArgs = { typeof(SerializationInfo), typeof(StreamingContext) };
Type objectType = si.ObjectType;
ConstructorInfo ci = objectType.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, constructorArgs, null);
// OPEN STATIC
{
si = new SerializationInfo(typeof(Delegate), new DummyFormatterConverter());
((ISerializable)sdel).GetObjectData(si, sc);
var objRef = (IObjectReference)ci.Invoke(new object[] { si, sc });
var sdel2 = (Func<string, string, string>)objRef.GetRealObject(sc);
res = sdel2("cc", "dd");
Console.WriteLine(res);
}
Console.WriteLine("========================");
// CLOSED STATIC
{
Program inst = new Program { _id = "ClosedStatic" };
sdel = inst.StaticExt;
sdel += inst.StaticExt2;
res = sdel("11", "22");
Console.WriteLine(res);
si = new SerializationInfo(typeof(Delegate), new DummyFormatterConverter());
sdel.GetObjectData(si, sc);
var objRef = (IObjectReference)ci.Invoke(new object[] { si, sc });
var sdel2 = (Func<string, string, string>)objRef.GetRealObject(sc);
res = sdel2("33", "44");
Console.WriteLine(res);
}
Console.WriteLine("========================");
// CLOSED INSTANCE
{
Program inst = new Program { _id = "ClosedInstance" };
sdel = inst.InstanceDel;
sdel += inst.InstanceDel2;
res = sdel("11", "22");
Console.WriteLine(res);
si = new SerializationInfo(typeof(Delegate), new DummyFormatterConverter());
sdel.GetObjectData(si, sc);
var objRef = (IObjectReference)ci.Invoke(new object[] { si, sc });
var sdel2 = (Func<string, string, string>)objRef.GetRealObject(sc);
res = sdel2("33", "44");
Console.WriteLine(res);
}
Console.WriteLine("========================");
// OPEN INSTANCE
{
Program inst = new Program { _id = "OpenInstance" };
MethodInfo m1 = typeof(Program).GetTypeInfo().GetDeclaredMethod("InstanceDel");
MethodInfo m2 = typeof(Program).GetTypeInfo().GetDeclaredMethod("InstanceDel2");
Func<Program, string, string, string> idel = (Func<Program, string, string, string>)
Delegate.CreateDelegate(typeof(Func<Program, string, string, string>), null, m1);
idel += (Func<Program, string, string, string>)
Delegate.CreateDelegate(typeof(Func<Program, string, string, string>), null, m2);
res = idel(inst, "11", "22");
Console.WriteLine(res);
si = new SerializationInfo(typeof(Delegate), new DummyFormatterConverter());
idel.GetObjectData(si, sc);
var objRef = (IObjectReference)ci.Invoke(new object[] { si, sc });
var idel2 = (Func<Program, string, string, string>)objRef.GetRealObject(sc);
res = idel2(inst, "33", "44");
Console.WriteLine(res);
}
return 100;
}
}`