IL2058: Parameters passed to 'Assembly.CreateInstance' cannot be statically analyzed

Cause

A call to CreateInstance was found in the analyzed code.

Rule description

Trimmer does not analyze assembly instances and thus does not know on which assembly CreateInstance was called.

Example

void TestMethod()
{
    // IL2058 Trim analysis: Parameters passed to method 'Assembly.CreateInstance(string)' cannot be analyzed. Consider using methods 'System.Type.GetType' and `System.Activator.CreateInstance` instead.
    AssemblyLoadContext.Default.Assemblies.First(a => a.Name == "MyAssembly").CreateInstance("MyType");

    // This can be replaced by
    Activator.CreateInstance(Type.GetType("MyType, MyAssembly"));
}

How to fix

Trimmer has support for Type.GetType(String). The result can be passed to CreateInstance to create an instance of the type.