Skip to content

Fix MA0182 false positive on types with DynamicallyAccessedMembers attribute#963

Merged
meziantou merged 2 commits intomainfrom
copilot/fix-ma0182-false-positive
Jan 11, 2026
Merged

Fix MA0182 false positive on types with DynamicallyAccessedMembers attribute#963
meziantou merged 2 commits intomainfrom
copilot/fix-ma0182-false-positive

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jan 11, 2026

MA0182 incorrectly flags internal types as unused when marked with [DynamicallyAccessedMembers], which indicates the type is accessed via reflection.

Changes

  • Analyzer: Added check in IsPotentialUnusedType to exclude types with DynamicallyAccessedMembersAttribute

    • Store attribute type reference in AnalyzerContext constructor from compilation
    • Pass attribute to validation method for filtering
  • Tests: Added coverage for internal classes, private nested classes, and structs with the attribute

Example

// Previously flagged as unused, now correctly ignored
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
internal sealed class FakeTaskHandler : ITaskHandler
{
    // Accessed via reflection at runtime
}

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • www.nuget.org
    • Triggering command: /usr/share/dotnet/dotnet /usr/share/dotnet/dotnet exec --runtimeconfig /home/REDACTED/work/Meziantou.Analyzer/Meziantou.Analyzer/tests/Meziantou.Analyzer.Test/bin/Debug/net10.0/Meziantou.Analyzer.Test.runtimeconfig.json --depsfile /home/REDACTED/work/Meziantou.Analyzer/Meziantou.Analyzer/tests/Meziantou.Analyzer.Test/bin/Debug/net10.0/Meziantou.Analyzer.Test.deps.json /home/REDACTED/work/Meziantou.Analyzer/Meziantou.Analyzer/tests/Meziantou.Analyzer.Test/bin/Debug/net10.0/testhost.dll --port 46883 --endpoint 127.0.0.1:046883 --role client --parentprocessid 4373 --datacollectionport 36869 --telemetryoptedin false (dns block)
    • Triggering command: /usr/share/dotnet/dotnet /usr/share/dotnet/dotnet exec --runtimeconfig /home/REDACTED/work/Meziantou.Analyzer/Meziantou.Analyzer/tests/Meziantou.Analyzer.Test/bin/Debug/net10.0/Meziantou.Analyzer.Test.runtimeconfig.json --depsfile /home/REDACTED/work/Meziantou.Analyzer/Meziantou.Analyzer/tests/Meziantou.Analyzer.Test/bin/Debug/net10.0/Meziantou.Analyzer.Test.deps.json /home/REDACTED/work/Meziantou.Analyzer/Meziantou.Analyzer/tests/Meziantou.Analyzer.Test/bin/Debug/net10.0/testhost.dll --port 45901 --endpoint 127.0.0.1:045901 --role client --parentprocessid 4552 --datacollectionport 41269 --telemetryoptedin false (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>MA0182 false positive on [DynamicallyAccessedMembers]</issue_title>
<issue_description>### Version of the Meziantou.Analyzer NuGet package

2.0.275

Rule Identifier

MA0182

Target Framework

net10.0

C# Language version

C# 12

Description

MA0182 report the follow error

21>LocalTaskHandlerTypeUnitTests.cs(31,23): Error MA0182 : Internal type 'FakeTaskHandler1' is apparently never used. If so, remove it from the assembly. If this type is intended to contain only static members, make it 'static'. (https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0182.md)

but it marked with DynamicallyAccessedMembers attribute

	[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
	private sealed class FakeTaskHandler1 : ITaskHandler

According to docs

Indicates that certain members on a specified Type are accessed dynamically, for example, through Reflection.

Reproduction Steps

Unfortunately, I can't reproduce the issue on a sample project

Other information

No response</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI self-assigned this Jan 11, 2026
…tribute

Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix MA0182 false positive on [DynamicallyAccessedMembers] Fix MA0182 false positive on types with DynamicallyAccessedMembers attribute Jan 11, 2026
Copilot AI requested a review from meziantou January 11, 2026 22:12
@meziantou meziantou marked this pull request as ready for review January 11, 2026 22:20
@meziantou meziantou enabled auto-merge (squash) January 11, 2026 22:20
@meziantou meziantou merged commit 908db3a into main Jan 11, 2026
6 of 13 checks passed
@meziantou meziantou deleted the copilot/fix-ma0182-false-positive branch January 11, 2026 22:24
This was referenced Jan 12, 2026
This was referenced May 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MA0182 false positive on [DynamicallyAccessedMembers]

2 participants