From 1c56d40d8553a879c6144676bd03deb935a2baa3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Michal=20Strehovsk=C3=BD?= Date: Tue, 23 Jul 2019 14:14:16 +0200 Subject: [PATCH] Allow creating typehandles to `TypedReference*` (dotnet/coreclr#25817) It's unclear why this was disallowed - we allow them in method signatures and there's a comment a couple lines below the code I'm touching saying we thought about blocking this for byref-like types, but there's no reason to do that. `TypedReference` is the original byref-like type. Commit migrated from https://github.com/dotnet/coreclr/commit/2354d0b92f703b79f184a07557f9221dc11224d9 --- src/coreclr/src/vm/clsload.cpp | 3 +- .../reflection/regression/github_25697/25697.cs | 30 ++++++++++++++++++ .../regression/github_25697/25697.csproj | 36 ++++++++++++++++++++++ 3 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 src/coreclr/tests/src/reflection/regression/github_25697/25697.cs create mode 100644 src/coreclr/tests/src/reflection/regression/github_25697/25697.csproj diff --git a/src/coreclr/src/vm/clsload.cpp b/src/coreclr/src/vm/clsload.cpp index ff4d0a0..1aef6b4 100644 --- a/src/coreclr/src/vm/clsload.cpp +++ b/src/coreclr/src/vm/clsload.cpp @@ -3339,8 +3339,7 @@ TypeHandle ClassLoader::CreateTypeHandleForTypeKey(TypeKey* pKey, AllocMemTracke else { // no parameterized type allowed on a reference - if (paramType.GetInternalCorElementType() == ELEMENT_TYPE_BYREF || - paramType.GetInternalCorElementType() == ELEMENT_TYPE_TYPEDBYREF) + if (paramType.GetInternalCorElementType() == ELEMENT_TYPE_BYREF) { ThrowTypeLoadException(pKey, IDS_CLASSLOAD_GENERAL); } diff --git a/src/coreclr/tests/src/reflection/regression/github_25697/25697.cs b/src/coreclr/tests/src/reflection/regression/github_25697/25697.cs new file mode 100644 index 0000000..3628460 --- /dev/null +++ b/src/coreclr/tests/src/reflection/regression/github_25697/25697.cs @@ -0,0 +1,30 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Reflection; + +unsafe class Program +{ + public static void AsTypedReference(ref T value, TypedReference* output) + { + *output = __makeref(value); + value = (T)(object)"Hello"; + } + + static int Main() + { + // In this test, we try to reflect on a signature of a method that takes a TypedReference*. + // This is not useful for much else than Reflection.Emit or Delegate.CreateDelegate. + // (Do not Reflection.Invoke this - the TypedReference is likely going to point to a dead + // temporary when the method returns.) + var method = typeof(Program).GetMethod(nameof(Program.AsTypedReference)); + var s = method.ToString(); + Console.WriteLine(s); + if (s != "Void AsTypedReference[T](T ByRef, TypedReference*)") + return 1; + + return 100; + } +} diff --git a/src/coreclr/tests/src/reflection/regression/github_25697/25697.csproj b/src/coreclr/tests/src/reflection/regression/github_25697/25697.csproj new file mode 100644 index 0000000..a2ded7e --- /dev/null +++ b/src/coreclr/tests/src/reflection/regression/github_25697/25697.csproj @@ -0,0 +1,36 @@ + + + + + Debug + AnyCPU + 2.0 + {CE893CE4-177E-47D3-A5DA-DF4D64E76812} + Exe + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + ..\..\ + true + BuildAndRun + 1 + + + + + + + + + False + + + + + + + + + + + + + \ No newline at end of file -- 2.7.4