dotnet / dotnet/linker

.NET 6.0 removing ctor for Activator.CreateInstance()-created types

Open
#1,633 13 comments 0 reactions 1 assignee Claimed by @vitek-karas View on GitHub
area-Linker: Steps needs more info
Dominant language
C#
Stars
392
Forks
128
Avg merge
2d 10h
Merged PRs (30d)
2

Description

In getting our tests passing for .NET 6, we have a test where the linker is removing the `.ctor` for this type:

https://github.com/xamarin/xamarin-android/blob/9bc00032eebc30e91a66114eff9026c6a3b0e4d7/tests/MSBuildDeviceIntegration/Tests/InstallAndRunTests.cs#L282-L284
https://github.com/xamarin/xamarin-android/blob/9bc00032eebc30e91a66114eff9026c6a3b0e4d7/tests/MSBuildDeviceIntegration/Resources/LinkDescTest/MainActivityReplacement.cs#L35-L45

Given the type:
```csharp
namespace Library1 { public class SomeClass { } }
```
It is only used with code such as:
```csharp
var asm = typeof(Library1.SomeClass).Assembly;
var o = Activator.CreateInstance(asm.GetType("Library1.SomeClass"));
```

`typeof()` works fine, but the `.ctor` has been linked away.

This seems like it might be a common case that works today with current Xamarin.

Here are the assemblies from the linker output: [linked.zip](https://github.com/mono/linker/files/5554045/linked.zip)

`UnnamedProject.dll` is the root assembly, and `Library1.dll` contains this type.

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.