Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"));
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions src/tests/nativeaot/SmokeTests/Reflection/Reflection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ private static int Main()
TestInvokeMethodMetadata.Run();
TestVTableOfNullableUnderlyingTypes.Run();
TestInterfaceLists.Run();
TestMethodConsistency.Run();

//
// Mostly functionality tests
Expand Down Expand Up @@ -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)
Expand Down