You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Aug 24, 2022. It is now read-only.
The in C# 7.2 introduced "in" keyword for method parameters results in a crash when used in an interface, but only if the class that is implementing the interface is cast back to the interface.
The code throws a System.InvalidCastException at runtime (Firefox 57.0.4).
using System;
public struct TestStruct
{
public int Field;
}
public interface TestInterface
{
void TestMethodRef(ref TestStruct testStruct);
void TestMethodIn(in TestStruct testStruct);
}
public class TestClass : TestInterface
{
public void TestMethodRef(ref TestStruct testStruct) { }
public void TestMethodIn(in TestStruct testStruct) { }
}
class Program
{
static void Main(string[] args)
{
TestStruct testStruct = new TestStruct();
TestClass interfaceClass = new TestClass();
Console.WriteLine("1:");
interfaceClass.TestMethodRef(ref testStruct); //Works
Console.WriteLine("2:");
interfaceClass.TestMethodIn(testStruct); //Works
Console.WriteLine("3:");
((TestInterface)interfaceClass).TestMethodRef(ref testStruct); //Works
Console.WriteLine("4:");
((TestInterface)interfaceClass).TestMethodIn(testStruct); //Crash: throws System.InvalidCastException: Unable to cast object of type 'TestStruct' to type 'ref TestStruct'
Console.WriteLine("End");
}
}
There is no problem when using "ref" which is strange because in both cases the parameter is passed as valuetype TestStruct& but with a System.Runtime.InteropServices.InAttribute for the "in" parameter version. However the in keyword results in two definitions for TestMethodIn()
First version of TestMethodIn :
.method public hidebysig
instance void TestMethodIn (
valuetype TestStruct& testStruct
) cil managed
{
.param [1]
.custom instance void [mscorlib]System.Runtime.CompilerServices.IsReadOnlyAttribute::.ctor() = (
01 00 00 00
)
// Method begins at RVA 0x2050
// Code size 2 (0x2)
.maxstack 8
IL_0000: nop
IL_0001: ret
} // end of method TestClass::TestMethodIn
The in C# 7.2 introduced "in" keyword for method parameters results in a crash when used in an interface, but only if the class that is implementing the interface is cast back to the interface.
The code throws a System.InvalidCastException at runtime (Firefox 57.0.4).
Bug test project:
https://github.com/baehny/Bugs/tree/master/JSIL/JSIL_InParameters/JSIL_InParameters
There is no problem when using "ref" which is strange because in both cases the parameter is passed as valuetype TestStruct& but with a System.Runtime.InteropServices.InAttribute for the "in" parameter version. However the in keyword results in two definitions for TestMethodIn()
First version of TestMethodIn :
Second version of TestMethodIn :
TestMethodRef :
Calling code:
The text was updated successfully, but these errors were encountered: