Add handling of method parameter custom attributes (#5188)
We need to apply the same rules as elsewhere to make the attributes reflectable.
This commit is contained in:
Родитель
55b4a7edef
Коммит
42e586596d
|
@ -23,7 +23,16 @@ namespace ILCompiler.DependencyAnalysis
|
|||
{
|
||||
MetadataReader reader = method.MetadataReader;
|
||||
MethodDefinition methodDef = reader.GetMethodDefinition(method.Handle);
|
||||
|
||||
// Handle custom attributes on the method
|
||||
AddDependenciesDueToCustomAttributes(ref dependencies, factory, method.Module, methodDef.GetCustomAttributes());
|
||||
|
||||
// Handle custom attributes on method parameters
|
||||
foreach (ParameterHandle parameterHandle in methodDef.GetParameters())
|
||||
{
|
||||
Parameter parameter = reader.GetParameter(parameterHandle);
|
||||
AddDependenciesDueToCustomAttributes(ref dependencies, factory, method.Module, parameter.GetCustomAttributes());
|
||||
}
|
||||
}
|
||||
|
||||
public static void AddDependenciesDueToCustomAttributes(ref DependencyList dependencies, NodeFactory factory, EcmaType type)
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
#endif
|
||||
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Reflection;
|
||||
|
||||
[assembly: TestAssembly]
|
||||
|
@ -34,6 +35,7 @@ internal class ReflectionTest
|
|||
TestStringConstructor.Run();
|
||||
TestAssemblyAndModuleAttributes.Run();
|
||||
TestAttributeExpressions.Run();
|
||||
TestParameterAttributes.Run();
|
||||
|
||||
//
|
||||
// Mostly functionality tests
|
||||
|
@ -212,6 +214,46 @@ internal class ReflectionTest
|
|||
}
|
||||
}
|
||||
|
||||
class TestParameterAttributes
|
||||
{
|
||||
#if OPTIMIZED_MODE_WITHOUT_SCANNER
|
||||
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
|
||||
#endif
|
||||
public static bool Method([Parameter] ParameterType parameter)
|
||||
{
|
||||
return parameter == null;
|
||||
}
|
||||
|
||||
public class ParameterType { }
|
||||
|
||||
class ParameterAttribute : Attribute
|
||||
{
|
||||
public ParameterAttribute([CallerMemberName] string memberName = null)
|
||||
{
|
||||
MemberName = memberName;
|
||||
}
|
||||
|
||||
public string MemberName { get; }
|
||||
}
|
||||
|
||||
public static void Run()
|
||||
{
|
||||
Console.WriteLine(nameof(TestParameterAttributes));
|
||||
|
||||
// Ensure things we reflect on are in the static callgraph
|
||||
if (string.Empty.Length > 0)
|
||||
{
|
||||
Method(null);
|
||||
}
|
||||
|
||||
MethodInfo method = typeof(TestParameterAttributes).GetMethod(nameof(Method));
|
||||
|
||||
var attribute = method.GetParameters()[0].GetCustomAttribute<ParameterAttribute>();
|
||||
if (attribute.MemberName != nameof(Method))
|
||||
throw new Exception();
|
||||
}
|
||||
}
|
||||
|
||||
class TestAttributeExpressions
|
||||
{
|
||||
struct FirstNeverUsedType { }
|
||||
|
|
Загрузка…
Ссылка в новой задаче