dotnet / dotnet/linker

Linker is warning for Assembly.GetType for hard-coded type

Open
#1,884 8 comments 0 reactions 0 assignees View on GitHub
question
Dominant language
C#
Stars
392
Forks
128
Avg merge
2d 10h
Merged PRs (30d)
2

Description

In dotnet/runtime we have https://github.com/dotnet/runtime/blob/main/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/DynamicDebuggerProxy.cs#L420

```csharp
private static readonly Type ComObjectType = typeof(object).Assembly.GetType("System.__ComObject");
```

with a single usage of that field https://github.com/dotnet/runtime/blob/main/src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/DynamicDebuggerProxy.cs#L445 :

```csharp
else if (obj != null && ComObjectType.IsAssignableFrom(obj.GetType()))
```

it seems that linker should be able to figure out what to do here but it warns instead:

```
D:\src\runtime\src\libraries\Microsoft.CSharp\src\Microsoft\CSharp\RuntimeBinder\DynamicDebuggerProxy.cs(420,9): Trim analysis warning IL2026: Microsoft.CSharp.RuntimeBinder.DynamicMetaObjectProviderDebugView..cctor(): Using method 'System.Reflection.Assembly.GetType(String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Types might be removed. [d:\src\runtime\src\libraries\src.proj]
```

cc: @eerhardt

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.