-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MissingMethodException when pointer to TypedReference is used #13077
Comments
Can you provide more detail so that we can repro this? I tried class Program
{
public static unsafe void AsTypedReference<T>(ref T managedPtr, TypedReference* reference)
{
*reference = __makeref(managedPtr);
}
static unsafe void Main()
{
int x = default;
AsTypedReference(ref x, null);
}
} Then I also tried this with InlineIL.Fody and that works too: using System;
using System.Runtime.CompilerServices;
using static InlineIL.IL;
using static InlineIL.IL.Emit;
namespace FodySample
{
using typedref = TypedReference; //IL compliant alias to TypedReference
unsafe class Program
{
public static readonly void* NullPtr = IntPtr.Zero.ToPointer();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void AsTypedReference<T>(ref T managedPtr, TypedReference* reference)
{
if (reference == NullPtr)
throw new ArgumentNullException(nameof(reference));
Push(reference);
Push(ref managedPtr);
Mkrefany(typeof(T));
Stobj(typeof(typedref));
Ret();
}
static void Main(string[] args)
{
var reference = default(TypedReference);
var i = 20;
AsTypedReference(ref i, &reference);
Console.WriteLine(__reftype(reference));
Console.WriteLine(__refvalue(reference, int));
}
}
} |
@MichalStrehovsky I tried to execute this method from XUnit test. I'll try to reproduce this from Main method without XUnit. |
@MichalStrehovsky, the bug is reproducible outside of Xunit test. Did you tried it on the same version of .NET Core and OS as specified in my first post? |
Could you attach project file(s) with a repro? It will make things easier. |
I get a different result in the sandbox with simple solution containing .NET Standard library and Core App:
CS057 error is produced by compiler:
I think this happening because my main project uses Fody Weaver to rewrite assembly and somehow deceives the compiler. However, I went the other way and decided to resolve such method through Reflection. Voila, now the error is reproducible at runtime whenever compilation successful. A new error is
That's a root exception which causes Repro solution is attached to this post. |
Thanks for the repro! This is reproducible even without the netstandard library. We just block type handles for As for the C# compiler error you were seeing, I would bet a dollar that the assembly rewriting damaged the method signature in some way. It would be helpful if you could attach the assembly that has the problem. |
Method signature is Ok, I checked it using |
I found strange runtime behavior that causes
MissingMethodException
but Roslyn compiles everything successfully.I'm developing a library which has a method that allow to convert
T&
intoTypedReference
. This typed cannot be as return type as well asref TypedReference
so I decided to use pointer to this type.This code compiles successfully but when I trying to call it I get
System.MissingMethodException : Method not found: 'Void AsTypedReference(!!0 ByRef, System.TypedReference*)'
To fix this error, I replaced
TypedReference*
withvoid*
. Now everything is fine.However, I think that this problem should be fixed at .NET level:
TypedReference
TypedReference&
should by allowed by Roslyn Compiler as well.The issue is reproduced on Windows 10 and Ubuntu Linux 18.04.2 LTS, x86_64, .NET Core version 2.2.301.
The text was updated successfully, but these errors were encountered: