Skip to content

Commit fc5c296

Browse files
authored
Improve the trim incompatibility message for Assembly.GetType (#91876)
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.
1 parent f276c51 commit fc5c296

File tree

14 files changed

+30
-29
lines changed

14 files changed

+30
-29
lines changed

src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeAssemblyBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ public override IList<CustomAttributeData> GetCustomAttributesData() =>
236236

237237
public override string? FullName => InternalAssembly.FullName;
238238

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

src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeModuleBuilder.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -542,19 +542,19 @@ internal Type[] GetTypesNoLock()
542542
return typeList;
543543
}
544544

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

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

557-
[RequiresUnreferencedCode("Types might be removed")]
557+
[RequiresUnreferencedCode("Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead.")]
558558
public override Type? GetType(string className, bool throwOnError, bool ignoreCase)
559559
{
560560
lock (SyncRoot)

src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeAssembly.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ private static partial void GetTypeCoreIgnoreCase(QCallAssembly assembly,
242242
return type;
243243
}
244244

245-
[RequiresUnreferencedCode("Types might be removed")]
245+
[RequiresUnreferencedCode("Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead.")]
246246
public override Type? GetType(
247247
string name, // throw on null strings regardless of the value of "throwOnError"
248248
bool throwOnError, bool ignoreCase)

src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeModule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ public override void GetObjectData(SerializationInfo info, StreamingContext cont
418418
throw new PlatformNotSupportedException();
419419
}
420420

421-
[RequiresUnreferencedCode("Types might be removed")]
421+
[RequiresUnreferencedCode("Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead.")]
422422
public override Type? GetType(
423423
string className, // throw on null strings regardless of the value of "throwOnError"
424424
bool throwOnError, bool ignoreCase)

src/coreclr/nativeaot/System.Private.CoreLib/src/CompatibilitySuppressions.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- https://learn.microsoft.com/en-us/dotnet/fundamentals/package-validation/diagnostic-ids -->
23
<Suppressions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
34
<Suppression>
45
<DiagnosticId>CP0001</DiagnosticId>

src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/Modules/RuntimeModule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public sealed override void GetObjectData(SerializationInfo info, StreamingConte
6565

6666
public abstract override int MetadataToken { get; }
6767

68-
[RequiresUnreferencedCode("Types might be removed")]
68+
[RequiresUnreferencedCode("Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead.")]
6969
public sealed override Type GetType(string name, bool throwOnError, bool ignoreCase)
7070
{
7171
return Assembly.GetType(name, throwOnError, ignoreCase);

src/libraries/System.Private.CoreLib/src/System/Reflection/Assembly.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,11 @@ public virtual IEnumerable<Type> ExportedTypes
105105
public virtual AssemblyName GetName() => GetName(copiedName: false);
106106
public virtual AssemblyName GetName(bool copiedName) { throw NotImplemented.ByDesign; }
107107

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

115115
public virtual bool IsDefined(Type attributeType, bool inherit) { throw NotImplemented.ByDesign; }

src/libraries/System.Private.CoreLib/src/System/Reflection/Module.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ protected Module() { }
7979
[RequiresUnreferencedCode("Types might be removed")]
8080
public virtual Type[] GetTypes() { throw NotImplemented.ByDesign; }
8181

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

8989
[RequiresUnreferencedCode("Types might be removed")]

src/libraries/System.Reflection.Emit/ref/System.Reflection.Emit.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ protected AssemblyBuilder() { }
5151
public override System.Reflection.AssemblyName[] GetReferencedAssemblies() { throw null; }
5252
public override System.Reflection.Assembly GetSatelliteAssembly(System.Globalization.CultureInfo culture) { throw null; }
5353
public override System.Reflection.Assembly GetSatelliteAssembly(System.Globalization.CultureInfo culture, System.Version? version) { throw null; }
54-
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed")]
54+
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead.")]
5555
public override System.Type? GetType(string name, bool throwOnError, bool ignoreCase) { throw null; }
5656
public override bool IsDefined(System.Type attributeType, bool inherit) { throw null; }
5757
public void SetCustomAttribute(System.Reflection.ConstructorInfo con, byte[] binaryAttribute) { }
@@ -441,11 +441,11 @@ public void CreateGlobalFunctions() { }
441441
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Methods might be removed")]
442442
public override System.Reflection.MethodInfo[] GetMethods(System.Reflection.BindingFlags bindingFlags) { throw null; }
443443
public override void GetPEKind(out System.Reflection.PortableExecutableKinds peKind, out System.Reflection.ImageFileMachine machine) { throw null; }
444-
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed")]
444+
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead.")]
445445
public override System.Type? GetType(string className) { throw null; }
446-
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed")]
446+
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead.")]
447447
public override System.Type? GetType(string className, bool ignoreCase) { throw null; }
448-
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed")]
448+
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead.")]
449449
public override System.Type? GetType(string className, bool throwOnError, bool ignoreCase) { throw null; }
450450
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed")]
451451
public override System.Type[] GetTypes() { throw null; }

src/libraries/System.Runtime/ref/System.Runtime.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11032,11 +11032,11 @@ public virtual void GetObjectData(System.Runtime.Serialization.SerializationInfo
1103211032
public virtual System.Reflection.AssemblyName[] GetReferencedAssemblies() { throw null; }
1103311033
public virtual System.Reflection.Assembly GetSatelliteAssembly(System.Globalization.CultureInfo culture) { throw null; }
1103411034
public virtual System.Reflection.Assembly GetSatelliteAssembly(System.Globalization.CultureInfo culture, System.Version? version) { throw null; }
11035-
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed")]
11035+
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead.")]
1103611036
public virtual System.Type? GetType(string name) { throw null; }
11037-
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed")]
11037+
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead.")]
1103811038
public virtual System.Type? GetType(string name, bool throwOnError) { throw null; }
11039-
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed")]
11039+
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead.")]
1104011040
public virtual System.Type? GetType(string name, bool throwOnError, bool ignoreCase) { throw null; }
1104111041
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed")]
1104211042
public virtual System.Type[] GetTypes() { throw null; }
@@ -11859,11 +11859,11 @@ protected Module() { }
1185911859
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
1186011860
public virtual void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { }
1186111861
public virtual void GetPEKind(out System.Reflection.PortableExecutableKinds peKind, out System.Reflection.ImageFileMachine machine) { throw null; }
11862-
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed")]
11862+
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead.")]
1186311863
public virtual System.Type? GetType(string className) { throw null; }
11864-
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed")]
11864+
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead.")]
1186511865
public virtual System.Type? GetType(string className, bool ignoreCase) { throw null; }
11866-
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed")]
11866+
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead.")]
1186711867
public virtual System.Type? GetType(string className, bool throwOnError, bool ignoreCase) { throw null; }
1186811868
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed")]
1186911869
public virtual System.Type[] GetTypes() { throw null; }

0 commit comments

Comments
 (0)