未验证 提交 753d12fa 编写于 作者: M Michal Strehovský 提交者: GitHub

Ensure methods on different instantiations are reflectable (#73749)

上级 17154bd7
......@@ -465,9 +465,9 @@ public override void GetConditionalDependenciesDueToEETypePresence(ref CombinedD
// ObjectGetTypeFlowDependencies don't need to be conditional in that case. They'll be added as needed.
}
// Ensure fields can be consistently reflection set & get.
if (type.HasInstantiation && !type.IsTypeDefinition && !IsReflectionBlocked(type))
{
// Ensure fields can be consistently reflection set & get.
foreach (FieldDesc field in type.GetFields())
{
// Tiny optimization: no get/set for literal fields since they only exist in metadata
......@@ -481,7 +481,24 @@ public override void GetConditionalDependenciesDueToEETypePresence(ref CombinedD
dependencies.Add(new DependencyNodeCore<NodeFactory>.CombinedDependencyListEntry(
factory.ReflectableField(field),
factory.ReflectableField(field.GetTypicalFieldDefinition()),
"GetType called on the interface"));
"Fields have same reflectability"));
}
// Ensure methods can be consistently reflection-accessed
foreach (MethodDesc method in type.GetMethods())
{
if (IsReflectionBlocked(method))
continue;
// Generic methods need to be instantiated over something.
if (method.HasInstantiation)
continue;
dependencies ??= new CombinedDependencyList();
dependencies.Add(new DependencyNodeCore<NodeFactory>.CombinedDependencyListEntry(
factory.ReflectableMethod(method),
factory.ReflectableMethod(method.GetTypicalMethodDefinition()),
"Methods have same reflectability"));
}
}
}
......
......@@ -62,6 +62,7 @@ private static int Main()
TestInvokeMethodMetadata.Run();
TestVTableOfNullableUnderlyingTypes.Run();
TestInterfaceLists.Run();
TestMethodConsistency.Run();
//
// Mostly functionality tests
......@@ -2021,6 +2022,25 @@ public static void Run()
}
}
class TestMethodConsistency
{
class MyGenericType<T>
{
public static string MyMethod() => typeof(T).Name;
}
struct Atom { }
public static void Run()
{
object returned = Grab<Atom>().Invoke(null, null);
if ((string)returned != nameof(Atom))
throw new Exception();
static MethodInfo Grab<T>() => typeof(MyGenericType<T>).GetMethod(nameof(MyGenericType<T>.MyMethod));
}
}
#region Helpers
private static Type SecretGetType(string testName, string typeName)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册