Skip to content
This repository was archived by the owner on Sep 25, 2026. It is now read-only.
This repository was archived by the owner on Sep 25, 2026. It is now read-only.

Improve IL2006 warning #1157

Closed
Closed

Description

@marek-safar

Today the users are facing with a message like

Unrecognized reflection pattern warning IL2006: The parameter 'attributeType' of method 'System.Attribute System.Diagnostics.Tracing.EventSource::GetCustomAttributeHelper(System.Reflection.MemberInfo,System.Type,System.Diagnostics.Tracing.EventManifestOptions)' with dynamically accessed member kinds 'None' is passed into the parameter 'type' of method 'System.Object System.Activator::CreateInstance(System.Type)' which requires dynamically accessed member kinds `DefaultConstructor`. To fix this add DynamicallyAccessedMembersAttribute to it and specify at least these member kinds 'DefaultConstructor'.

I'd suggest

  • Add location information (at least assembly name)
  • Drop return types from the method signatures
  • Don't use parameter name but parameter index
  • Split the text to be easier to understand
    • a first warning for missing attribute
    • a second warning for incomplete MemberKinds

An example of the proposed change

(foo.dll) warning IL2006: The parameter '#1' in method 'System.Diagnostics.Tracing.EventSource::GetCustomAttributeHelper(System.Reflection.MemberInfo,System.Type,System.Diagnostics.Tracing.EventManifestOptions)' is missing 'DynamicallyAccessedMembersAttribute'. Consider adding it with 'DefaultConstructor' value.

/cc @vitek-karas

Activity

  1. added this to the .NET5.0 milestone on May 1, 2020
  2. vitek-karas commented on Jul 8, 2020

    @vitek-karas
    Member

    Part of this should be to avoid printing out potentially long list of enum values. The DynamicallyAccessedMemberType is a flags enum, the warning message may potentially need to include representation of multiple such flags which would make it hard to read.

    See #1315 (comment) for more details.

  3. vitek-karas commented on Jul 8, 2020

    @vitek-karas
    Member

    @marek-safar I don't understand why this is desirable:

    Don't use parameter name but parameter index

    I think that this will be rather confusing to users:

    • It's unclear if the numbering starts with 0 or 1
    • Parameter names are considered part of the public API surface (several languages can refer to them by name even in the callsite, not just the callee body)
    • The code will fallback to index when it can't determine the parameter name (never ran into that case though)
  4. marek-safar commented on Jul 8, 2020

    @marek-safar
    ContributorAuthor

    I'm not sure this is confusing. To me syntax #N means it starts with 1 or does #0 make sense to you like a number?

    Parameter names are considered part of the public API

    I don't think parameter names as part of the public API surface matter here, the error handling can happen anywhere, for private stuff, compiler-generated methods (maybe not for this code but in general).

    The code will fallback to index when it can't determine the parameter name

    Not sure we need this inconsistency. It could be just me but I look at the parameters by index so if the error message tells me it's #3 I go to 3rd parameter. When it tells me it's parameter position I need to read all parameters and find if there one with such name.

    Not a big deal in general just not sure we need this complexity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions