public static unsafe void TestByRefLikeRefReturn() { ByRefLike brl = new ByRefLike(); ByRefLike *pBrl = &brl; MethodInfo mi = typeof(TestClass <int>).GetMethod(nameof(TestClass <int> .ByRefLikeRefReturningMethod)); try { // Don't use Assert.Throws because that will make a lambda and invalidate the pointer object o = mi.Invoke(null, new object[] { Pointer.Box(pBrl, typeof(ByRefLike *)) }); // If this is reached, it means `o` is a boxed byref-like type. That's a GC hole right there. throw new Xunit.Sdk.XunitException("Boxed a byref-like type."); } catch (NotSupportedException) { // We expect a NotSupportedException from the Invoke call. Methods returning byref-like types by reference // are not reflection invokable. } }
public static void Run() { Console.WriteLine(nameof(TestByRefLikeTypeMethod)); // Ensure things we reflect on are in the static callgraph if (string.Empty.Length > 0) { default(ByRefLike).ToString(); ToStringDelegate s = null; s = s.Invoke; } Type byRefLikeType = GetTestType(nameof(TestByRefLikeTypeMethod), nameof(ByRefLike)); MethodInfo toStringMethod = byRefLikeType.GetMethod("ToString"); var toString = (ToStringDelegate)toStringMethod.CreateDelegate(typeof(ToStringDelegate)); ByRefLike foo = new ByRefLike(123); if (toString(ref foo) != "123") { throw new Exception(); } }