Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The DAM properties does not propagate from BaseType #110237

Closed
kant2002 opened this issue Nov 28, 2024 · 5 comments
Closed

The DAM properties does not propagate from BaseType #110237

kant2002 opened this issue Nov 28, 2024 · 5 comments
Labels
area-Tools-ILLink .NET linker development as well as trimming analyzers

Comments

@kant2002
Copy link
Contributor

Description

I think it's possible to propagate DAM properties in following scenarios.

public static System.Reflection.MethodInfo FindMethod(
#if NET8_0_OR_GREATER
  [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
#endif
  Type objectType, string method, int parameterCount)
{
  // walk up the inheritance hierarchy looking
  // for a method with the right number of
  // parameters
  System.Reflection.MethodInfo result = null;
  Type currentType = objectType;
  do
  {
    // !!!!!!!!!!!!      HERE IL2075
    System.Reflection.MethodInfo info = currentType.GetMethod(method, oneLevelFlags);
    if (info != null)
    {
      var infoParams = info.GetParameters();
      var pCount = infoParams.Length;
      if (pCount > 0 &&
         ((pCount == 1 && infoParams[0].ParameterType.IsArray) ||
         (infoParams[pCount - 1].GetCustomAttributes(typeof(ParamArrayAttribute), true).Length > 0)))
      {
        // last param is a param array or only param is an array
        if (parameterCount >= pCount - 1)
        {
          // got a match so use it
          result = info;
          break;
        }
      }
      else if (pCount == parameterCount)
      {
        // got a match so use it
        result = info;
        break;
      }
    }
    // !!!!!!!!!!!!      HERE I Want propagation
    currentType = currentType.BaseType;
  } while (currentType != null);

  return result;
}

Is there way to teach illinker to do this kind of analysis?

Reproduction Steps

see description

Expected behavior

When variable of type Type reassigned by call myself.BaseType then attempt to preserve some DAM properties.

Actual behavior

IL2075 warning happens.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Nov 28, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-reflection
See info in area-owners.md if you want to be subscribed.

@MichalStrehovsky
Copy link
Member

What's the definition of oneLevelFlags? If it includes BindingFlags.NonPublic, the warning is correct. DynamicallyAccessedMemberTypes.NonPublicMethods matches the semantic of BindingFlags.NonPublic and will not include non-public methods in bases. They can and will be trimmed.

Currently, the only thing you can do is to use DynamicallyAccessedMemberTypes.All. You shouldn't see a warning if you do that. Starting with .NET 10, you'll be able to use DynamicallyAccessedMemberTypes.AllMethods that will work how you expect (include all methods, including non-publics in bases).

@MichalStrehovsky MichalStrehovsky added area-Tools-ILLink .NET linker development as well as trimming analyzers and removed area-System.Reflection labels Nov 28, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/illink
See info in area-owners.md if you want to be subscribed.

@kant2002
Copy link
Contributor Author

Thank you. that make sense.

@MichalStrehovsky
Copy link
Member

Closing as answered.

@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Nov 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-Tools-ILLink .NET linker development as well as trimming analyzers
Projects
Status: No status
Development

No branches or pull requests

2 participants