Skip to content

Commit

Permalink
Improve the trim incompatibility message for Assembly.GetType (#91876)
Browse files Browse the repository at this point in the history
Currently calling `Assembly.GetType` is always a warning when trimming. But typically the code can be changed to use `Type.GetType` with assembly qualified type name, which is a trim compatible pattern.

This modifies the warning message on the `RequiresUnreferencedCode` attribute for `Assembly.GetType` to suggest using `Type.GetType` instead.
  • Loading branch information
vitek-karas committed Sep 26, 2023
1 parent f276c51 commit fc5c296
Show file tree
Hide file tree
Showing 14 changed files with 30 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public override IList<CustomAttributeData> GetCustomAttributesData() =>

public override string? FullName => InternalAssembly.FullName;

[RequiresUnreferencedCode("Types might be removed")]
[RequiresUnreferencedCode("Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead.")]
public override Type? GetType(string name, bool throwOnError, bool ignoreCase) =>
InternalAssembly.GetType(name, throwOnError, ignoreCase);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,19 +542,19 @@ internal Type[] GetTypesNoLock()
return typeList;
}

[RequiresUnreferencedCode("Types might be removed")]
[RequiresUnreferencedCode("Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead.")]
public override Type? GetType(string className)
{
return GetType(className, false, false);
}

[RequiresUnreferencedCode("Types might be removed")]
[RequiresUnreferencedCode("Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead.")]
public override Type? GetType(string className, bool ignoreCase)
{
return GetType(className, false, ignoreCase);
}

[RequiresUnreferencedCode("Types might be removed")]
[RequiresUnreferencedCode("Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead.")]
public override Type? GetType(string className, bool throwOnError, bool ignoreCase)
{
lock (SyncRoot)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ private static partial void GetTypeCoreIgnoreCase(QCallAssembly assembly,
return type;
}

[RequiresUnreferencedCode("Types might be removed")]
[RequiresUnreferencedCode("Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead.")]
public override Type? GetType(
string name, // throw on null strings regardless of the value of "throwOnError"
bool throwOnError, bool ignoreCase)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ public override void GetObjectData(SerializationInfo info, StreamingContext cont
throw new PlatformNotSupportedException();
}

[RequiresUnreferencedCode("Types might be removed")]
[RequiresUnreferencedCode("Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead.")]
public override Type? GetType(
string className, // throw on null strings regardless of the value of "throwOnError"
bool throwOnError, bool ignoreCase)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- https://learn.microsoft.com/en-us/dotnet/fundamentals/package-validation/diagnostic-ids -->
<Suppressions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Suppression>
<DiagnosticId>CP0001</DiagnosticId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public sealed override void GetObjectData(SerializationInfo info, StreamingConte

public abstract override int MetadataToken { get; }

[RequiresUnreferencedCode("Types might be removed")]
[RequiresUnreferencedCode("Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead.")]
public sealed override Type GetType(string name, bool throwOnError, bool ignoreCase)
{
return Assembly.GetType(name, throwOnError, ignoreCase);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ public virtual IEnumerable<Type> ExportedTypes
public virtual AssemblyName GetName() => GetName(copiedName: false);
public virtual AssemblyName GetName(bool copiedName) { throw NotImplemented.ByDesign; }

[RequiresUnreferencedCode("Types might be removed")]
[RequiresUnreferencedCode("Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead.")]
public virtual Type? GetType(string name) => GetType(name, throwOnError: false, ignoreCase: false);
[RequiresUnreferencedCode("Types might be removed")]
[RequiresUnreferencedCode("Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead.")]
public virtual Type? GetType(string name, bool throwOnError) => GetType(name, throwOnError: throwOnError, ignoreCase: false);
[RequiresUnreferencedCode("Types might be removed")]
[RequiresUnreferencedCode("Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead.")]
public virtual Type? GetType(string name, bool throwOnError, bool ignoreCase) { throw NotImplemented.ByDesign; }

public virtual bool IsDefined(Type attributeType, bool inherit) { throw NotImplemented.ByDesign; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ protected Module() { }
[RequiresUnreferencedCode("Types might be removed")]
public virtual Type[] GetTypes() { throw NotImplemented.ByDesign; }

[RequiresUnreferencedCode("Types might be removed")]
[RequiresUnreferencedCode("Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead.")]
public virtual Type? GetType(string className) => GetType(className, throwOnError: false, ignoreCase: false);
[RequiresUnreferencedCode("Types might be removed")]
[RequiresUnreferencedCode("Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead.")]
public virtual Type? GetType(string className, bool ignoreCase) => GetType(className, throwOnError: false, ignoreCase: ignoreCase);
[RequiresUnreferencedCode("Types might be removed")]
[RequiresUnreferencedCode("Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead.")]
public virtual Type? GetType(string className, bool throwOnError, bool ignoreCase) { throw NotImplemented.ByDesign; }

[RequiresUnreferencedCode("Types might be removed")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected AssemblyBuilder() { }
public override System.Reflection.AssemblyName[] GetReferencedAssemblies() { throw null; }
public override System.Reflection.Assembly GetSatelliteAssembly(System.Globalization.CultureInfo culture) { throw null; }
public override System.Reflection.Assembly GetSatelliteAssembly(System.Globalization.CultureInfo culture, System.Version? version) { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead.")]
public override System.Type? GetType(string name, bool throwOnError, bool ignoreCase) { throw null; }
public override bool IsDefined(System.Type attributeType, bool inherit) { throw null; }
public void SetCustomAttribute(System.Reflection.ConstructorInfo con, byte[] binaryAttribute) { }
Expand Down Expand Up @@ -441,11 +441,11 @@ public void CreateGlobalFunctions() { }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Methods might be removed")]
public override System.Reflection.MethodInfo[] GetMethods(System.Reflection.BindingFlags bindingFlags) { throw null; }
public override void GetPEKind(out System.Reflection.PortableExecutableKinds peKind, out System.Reflection.ImageFileMachine machine) { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead.")]
public override System.Type? GetType(string className) { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead.")]
public override System.Type? GetType(string className, bool ignoreCase) { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead.")]
public override System.Type? GetType(string className, bool throwOnError, bool ignoreCase) { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed")]
public override System.Type[] GetTypes() { throw null; }
Expand Down
12 changes: 6 additions & 6 deletions src/libraries/System.Runtime/ref/System.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11032,11 +11032,11 @@ public virtual void GetObjectData(System.Runtime.Serialization.SerializationInfo
public virtual System.Reflection.AssemblyName[] GetReferencedAssemblies() { throw null; }
public virtual System.Reflection.Assembly GetSatelliteAssembly(System.Globalization.CultureInfo culture) { throw null; }
public virtual System.Reflection.Assembly GetSatelliteAssembly(System.Globalization.CultureInfo culture, System.Version? version) { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead.")]
public virtual System.Type? GetType(string name) { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead.")]
public virtual System.Type? GetType(string name, bool throwOnError) { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead.")]
public virtual System.Type? GetType(string name, bool throwOnError, bool ignoreCase) { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed")]
public virtual System.Type[] GetTypes() { throw null; }
Expand Down Expand Up @@ -11859,11 +11859,11 @@ protected Module() { }
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
public virtual void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { }
public virtual void GetPEKind(out System.Reflection.PortableExecutableKinds peKind, out System.Reflection.ImageFileMachine machine) { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead.")]
public virtual System.Type? GetType(string className) { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead.")]
public virtual System.Type? GetType(string className, bool ignoreCase) { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead.")]
public virtual System.Type? GetType(string className, bool throwOnError, bool ignoreCase) { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed")]
public virtual System.Type[] GetTypes() { throw null; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ protected override void SetCustomAttributeCore(ConstructorInfo con, ReadOnlySpan
internal static Type MakeGenericType(Type gtd, Type[] typeArguments) =>
new TypeBuilderInstantiation(gtd, typeArguments);

[RequiresUnreferencedCode("Types might be removed")]
[RequiresUnreferencedCode("Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead.")]
public override Type? GetType(string name, bool throwOnError, bool ignoreCase)
{
ArgumentException.ThrowIfNullOrEmpty(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,13 +296,13 @@ protected override EnumBuilder DefineEnumCore(string name, TypeAttributes visibi
return eb;
}

[RequiresUnreferencedCode("Types might be removed")]
[RequiresUnreferencedCode("Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead.")]
public override Type? GetType(string className)
{
return GetType(className, false, false);
}

[RequiresUnreferencedCode("Types might be removed")]
[RequiresUnreferencedCode("Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead.")]
public override Type? GetType(string className, bool ignoreCase)
{
return GetType(className, false, ignoreCase);
Expand Down Expand Up @@ -347,7 +347,7 @@ protected override EnumBuilder DefineEnumCore(string name, TypeAttributes visibi
return result;
}

[RequiresUnreferencedCode("Types might be removed")]
[RequiresUnreferencedCode("Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead.")]
public override Type? GetType(string className, bool throwOnError, bool ignoreCase)
{
ArgumentException.ThrowIfNullOrEmpty(className);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ public override AssemblyName GetName(bool copiedName)
return AssemblyName.Create(_mono_assembly, GetInfo(AssemblyInfoKind.CodeBase));
}

[RequiresUnreferencedCode("Types might be removed")]
[RequiresUnreferencedCode("Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead.")]
public override Type GetType(string name, bool throwOnError, bool ignoreCase)
{
ArgumentException.ThrowIfNullOrEmpty(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ void GetPEKind(out PortableExecutableKinds peKind, out ImageFileMachine machine)
GetPEKind(_impl, out peKind, out machine);
}

[RequiresUnreferencedCode("Types might be removed")]
[RequiresUnreferencedCode("Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead.")]
public override
Type GetType(string className, bool throwOnError, bool ignoreCase)
{
Expand Down

0 comments on commit fc5c296

Please sign in to comment.