Skip to content

Should we mark MethodBase.GetCurrentMethod() and friends RequiresUnreferencedCode? #53242

Description

@MichalStrehovsky

Illink started trimming bits of metadata off things that are not seen as reflected on. We have APIs that allow reflecting on basically everything in the program (e.g. MethodBase.GetCurrentMethod()). One of the bits illinker strips is parameter names.

While MethodBase.GetCurrentMethod is not "trim unsafe" per se, the MethodInfos returned from it might be "damaged" and will result in different runtime behavior. We have been tagging places like that so that the developer can decide whether it's acceptable. Tagging ParameterInfo.Name as trim-unsafe feels like unnecessarily big hammer - it will work if the MethodBase was retrieved through more standard means. It feels more appropriate to just tag MethodBase.GetCurrentMethod and friends as RequiresUnreferenced.

E.g. the Illinker optimization is going to cause different behavior in this code. I expect we're fine with a different behavior there, but it should be a conscious decision, not a random result of an unsafe optimization. E.g. we might want to revise what happens when the parameter name was null in that codepath.

// arguments printing
sb.Append('(');
bool fFirstParam = true;
for (int j = 0; j < pi.Length; j++)
{
if (!fFirstParam)
sb.Append(", ");
else
fFirstParam = false;
string typeName = "<UnknownType>";
if (pi[j].ParameterType != null)
typeName = pi[j].ParameterType.Name;
sb.Append(typeName);
sb.Append(' ');
sb.Append(pi[j].Name);
}
sb.Append(')');

This would apply to APIs like: Exception.TargetSite and StackFrame.GetMethod too.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions