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.

DynamicallyAccessedMembers does not mark the type if passed in as string #1537

Closed

Description

@vitek-karas

The below has to be built into a classlib which is then aggressively trimmed:

namespace lib
{
    public class Test
    {
        public static void TestIt()
        {
            object o = CreateInstanceOf("lib.GenOut`1[[lib.GenIn]]");
            Console.WriteLine(o);
        }

        static object CreateInstanceOf([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] string n)
        {
            return Activator.CreateInstance(Type.GetType(n));
        }
    }


    class GenOut<T> where T : new()
    {
        T GetValue() { return new T(); }
    }

    class GenIn
    {
    }
}

Per annotations, it is perfectly valid to call Activator.CreateInstance due to the annotation on the parameter of CreateInstanceOf. Linker doesn't produce any warnings from the code above.

But when executed, the application fails with:

Unhandled exception. System.ArgumentNullException: Value cannot be null. (Parameter 'type')
   at System.Activator.CreateInstance(Type type, Boolean nonPublic, Boolean wrapExceptions)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at System.Activator.CreateInstance(Type type)
   at lib.Test.CreateInstanceOf(String n) in F:\ILLinker\repro\TypeNameRep\lib\Class1.cs:line 16
   at lib.Test.TestIt() in F:\ILLinker\repro\TypeNameRep\lib\Class1.cs:line 10
   at Program.Main(String[] args) in F:\ILLinker\repro\TypeNameRep\TypeNameRep\Program.cs:line 17

The failure is because Type.GetType returns null. In the trimmed assembly there's actually no type GenOut.
There are two weird things here:

  1. GenOut<>.ctor() should be kept, but for some reason we leave it out
  2. Even if I force include GenOut<> (by having a direct typeref to it), GenIn also needs to be kept, which won't happen either

Activity

  1. vitek-karas commented on Oct 6, 2020

    @vitek-karas
    MemberAuthor

    Interestingly changing the code to call CreateInstanceOf("lib.GenOut`1") actually works, the GenOut<>.ctor is kept and the app fails because it's invalid to instantiate open generic type.

  2. vitek-karas commented on Oct 6, 2020

    @vitek-karas
    MemberAuthor

    I also found dotnet/sdk#13999 when working on this bug.

  3. vitek-karas commented on Sep 16, 2022

    @vitek-karas
    MemberAuthor

    This has been fixed, the repro from the issue above works now.

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

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions