diff --git a/src/libraries/System.Private.CoreLib/src/System/Activator.RuntimeType.cs b/src/libraries/System.Private.CoreLib/src/System/Activator.RuntimeType.cs index e9ba8477c222fd..b8dbd0a7e24237 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Activator.RuntimeType.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Activator.RuntimeType.cs @@ -100,6 +100,8 @@ public static partial class Activator [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", Justification = "Implementation detail of Activator that linker intrinsically recognizes")] + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", + Justification = "Implementation detail of Activator that linker intrinsically recognizes")] private static ObjectHandle? CreateInstanceInternal(string assemblyString, string typeName, bool ignoreCase, @@ -143,7 +145,7 @@ public static partial class Activator } [System.Runtime.CompilerServices.Intrinsic] - public static T CreateInstance() + public static T CreateInstance<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]T>() { return (T)((RuntimeType)typeof(T)).CreateInstanceDefaultCtor(publicOnly: true, skipCheckThis: true, fillCache: true, wrapExceptions: true)!; } diff --git a/src/libraries/System.Private.CoreLib/src/System/Lazy.cs b/src/libraries/System.Private.CoreLib/src/System/Lazy.cs index e071b16cc7c258..80bc830c20463d 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Lazy.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Lazy.cs @@ -153,7 +153,7 @@ internal static LazyHelper Create(LazyThreadSafetyMode mode, bool useDefaultCons } } - internal static T CreateViaDefaultConstructor() + internal static T CreateViaDefaultConstructor<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]T>() { try { @@ -184,7 +184,7 @@ internal static LazyThreadSafetyMode GetModeFromIsThreadSafe(bool isThreadSafe) /// [DebuggerTypeProxy(typeof(LazyDebugView<>))] [DebuggerDisplay("ThreadSafetyMode={Mode}, IsValueCreated={IsValueCreated}, IsValueFaulted={IsValueFaulted}, Value={ValueForDebugDisplay}")] - public class Lazy + public class Lazy<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]T> { private static T CreateViaDefaultConstructor() => LazyHelper.CreateViaDefaultConstructor(); diff --git a/src/libraries/System.Private.CoreLib/src/System/LazyOfTTMetadata.cs b/src/libraries/System.Private.CoreLib/src/System/LazyOfTTMetadata.cs index ae778bd7ba68ca..1e78b3c4ba32f2 100644 --- a/src/libraries/System.Private.CoreLib/src/System/LazyOfTTMetadata.cs +++ b/src/libraries/System.Private.CoreLib/src/System/LazyOfTTMetadata.cs @@ -2,11 +2,12 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System.Diagnostics.CodeAnalysis; using System.Threading; namespace System { - public class Lazy : Lazy + public class Lazy<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]T, TMetadata> : Lazy { private readonly TMetadata _metadata; diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Assembly.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Assembly.cs index 6f188d08e18612..cba06116e14d60 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/Assembly.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Assembly.cs @@ -25,6 +25,7 @@ protected Assembly() { } public virtual IEnumerable DefinedTypes { + [RequiresUnreferencedCode("Types might be removed")] get { Type[] types = GetTypes(); @@ -41,6 +42,7 @@ public virtual IEnumerable DefinedTypes } } + [RequiresUnreferencedCode("Types might be removed")] public virtual Type[] GetTypes() { Module[] m = GetModules(false); @@ -70,8 +72,14 @@ public virtual Type[] GetTypes() return ret; } - public virtual IEnumerable ExportedTypes => GetExportedTypes(); + public virtual IEnumerable ExportedTypes + { + [RequiresUnreferencedCode("Types might be removed")] + get => GetExportedTypes(); + } + [RequiresUnreferencedCode("Types might be removed")] public virtual Type[] GetExportedTypes() { throw NotImplemented.ByDesign; } + [RequiresUnreferencedCode("Types might be removed")] public virtual Type[] GetForwardedTypes() { throw NotImplemented.ByDesign; } public virtual string? CodeBase => throw NotImplemented.ByDesign; @@ -93,8 +101,11 @@ public virtual Type[] GetTypes() public virtual AssemblyName GetName() => GetName(copiedName: false); public virtual AssemblyName GetName(bool copiedName) { throw NotImplemented.ByDesign; } + [RequiresUnreferencedCode("Types might be removed")] public virtual Type? GetType(string name) => GetType(name, throwOnError: false, ignoreCase: false); + [RequiresUnreferencedCode("Types might be removed")] public virtual Type? GetType(string name, bool throwOnError) => GetType(name, throwOnError: throwOnError, ignoreCase: false); + [RequiresUnreferencedCode("Types might be removed")] public virtual Type? GetType(string name, bool throwOnError, bool ignoreCase) { throw NotImplemented.ByDesign; } public virtual bool IsDefined(Type attributeType, bool inherit) { throw NotImplemented.ByDesign; } @@ -135,6 +146,7 @@ public virtual Type[] GetTypes() public Module[] GetLoadedModules() => GetLoadedModules(getResourceModules: false); public virtual Module[] GetLoadedModules(bool getResourceModules) { throw NotImplemented.ByDesign; } + [RequiresUnreferencedCode("Assembly references might be removed")] public virtual AssemblyName[] GetReferencedAssemblies() { throw NotImplemented.ByDesign; } public virtual Assembly GetSatelliteAssembly(CultureInfo culture) { throw NotImplemented.ByDesign; } diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/AsyncMethodBuilderCore.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/AsyncMethodBuilderCore.cs index f8aad2f0178834..5811f0cc662187 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/AsyncMethodBuilderCore.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/AsyncMethodBuilderCore.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Diagnostics.Tracing; using System.Reflection; using System.Threading; @@ -92,6 +93,8 @@ internal static bool TrackAsyncMethodCompletion /// Gets a description of the state of the state machine object, suitable for debug purposes. /// The state machine object. /// A description of the state machine. + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", + Justification = "It's okay if unused fields disappear from debug views")] internal static string GetAsyncStateMachineDescription(IAsyncStateMachine stateMachine) { Debug.Assert(stateMachine != null); diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/ConditionalWeakTable.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/ConditionalWeakTable.cs index ae749f45b8d2ba..3beb45fb1d73c2 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/ConditionalWeakTable.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/ConditionalWeakTable.cs @@ -11,7 +11,7 @@ namespace System.Runtime.CompilerServices { - public sealed class ConditionalWeakTable : IEnumerable> + public sealed class ConditionalWeakTable : IEnumerable> where TKey : class where TValue : class? { diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.cs index 8bc04a2e9a138b..38921c54875322 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.cs @@ -574,7 +574,7 @@ public static void PtrToStructure(IntPtr ptr, [DisallowNull] T structure) } [return: MaybeNull] - public static T PtrToStructure(IntPtr ptr) => (T)PtrToStructure(ptr, typeof(T))!; + public static T PtrToStructure<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]T>(IntPtr ptr) => (T)PtrToStructure(ptr, typeof(T))!; public static void DestroyStructure(IntPtr ptr) => DestroyStructure(ptr, typeof(T)); diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/Loader/AssemblyLoadContext.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/Loader/AssemblyLoadContext.cs index 20533fb43a846a..d79266deb66b65 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/Loader/AssemblyLoadContext.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/Loader/AssemblyLoadContext.cs @@ -710,6 +710,8 @@ private static void OnAssemblyLoad(RuntimeAssembly assembly) } #endif // !CORERT + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", + Justification = "Satellite assemblies have no code in them and loading is not a problem")] private Assembly? ResolveSatelliteAssembly(AssemblyName assemblyName) { // Called by native runtime when CultureName is not empty diff --git a/src/libraries/System.Private.CoreLib/src/System/RuntimeType.cs b/src/libraries/System.Private.CoreLib/src/System/RuntimeType.cs index c74821918b165b..c1cb08f0bfea88 100644 --- a/src/libraries/System.Private.CoreLib/src/System/RuntimeType.cs +++ b/src/libraries/System.Private.CoreLib/src/System/RuntimeType.cs @@ -72,6 +72,13 @@ public override IList GetCustomAttributesData() // GetDefaultMembers // This will return a MemberInfo that has been marked with the [DefaultMemberAttribute] + [DynamicallyAccessedMembers( + DynamicallyAccessedMemberTypes.PublicFields + | DynamicallyAccessedMemberTypes.PublicMethods + | DynamicallyAccessedMemberTypes.PublicEvents + | DynamicallyAccessedMemberTypes.PublicProperties + | DynamicallyAccessedMemberTypes.PublicConstructors + | DynamicallyAccessedMemberTypes.PublicNestedTypes)] public override MemberInfo[] GetDefaultMembers() { // See if we have cached the default member name diff --git a/src/libraries/System.Private.CoreLib/src/System/Threading/LazyInitializer.cs b/src/libraries/System.Private.CoreLib/src/System/Threading/LazyInitializer.cs index 8e2e63c2d06705..7b405669849b65 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Threading/LazyInitializer.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Threading/LazyInitializer.cs @@ -48,7 +48,7 @@ public static class LazyInitializer /// if an object was not used and to then dispose of the object appropriately. /// /// - public static T EnsureInitialized([NotNull] ref T? target) where T : class => + public static T EnsureInitialized<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]T>([NotNull] ref T? target) where T : class => Volatile.Read(ref target!) ?? EnsureInitializedCore(ref target); /// @@ -57,7 +57,7 @@ public static T EnsureInitialized([NotNull] ref T? target) where T : class => /// The reference type of the reference to be initialized. /// The variable that need to be initialized /// The initialized variable - private static T EnsureInitializedCore([NotNull] ref T? target) where T : class + private static T EnsureInitializedCore<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]T>([NotNull] ref T? target) where T : class { try { @@ -136,7 +136,7 @@ private static T EnsureInitializedCore([NotNull] ref T? target, Func value /// . If is null, and if the target hasn't already /// been initialized, a new object will be instantiated. /// The initialized value of type . - public static T EnsureInitialized([AllowNull] ref T target, ref bool initialized, [NotNullIfNotNull("syncLock")] ref object? syncLock) + public static T EnsureInitialized<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]T>([AllowNull] ref T target, ref bool initialized, [NotNullIfNotNull("syncLock")] ref object? syncLock) { // Fast path. if (Volatile.Read(ref initialized)) @@ -158,7 +158,7 @@ public static T EnsureInitialized([AllowNull] ref T target, ref bool initiali /// a new object will be instantiated. /// /// The initialized object. - private static T EnsureInitializedCore([AllowNull] ref T target, ref bool initialized, [NotNull] ref object? syncLock) + private static T EnsureInitializedCore<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]T>([AllowNull] ref T target, ref bool initialized, [NotNull] ref object? syncLock) { // Lazily initialize the lock if necessary and then double check if initialization is still required. lock (EnsureLockInitialized(ref syncLock)) diff --git a/src/libraries/System.Private.CoreLib/src/System/Type.cs b/src/libraries/System.Private.CoreLib/src/System/Type.cs index a5086ff2ec26d9..57c2ae19525920 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Type.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Type.cs @@ -150,7 +150,7 @@ public ConstructorInfo? TypeInitializer [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", - Justification = "Linker doesn't recongnize GetConstructors(BindingFlags.Public) but this is what the body is doing")] + Justification = "Linker doesn't recognize GetConstructors(BindingFlags.Public) but this is what the body is doing")] public ConstructorInfo[] GetConstructors() => GetConstructors(BindingFlags.Public | BindingFlags.Instance); [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] @@ -164,7 +164,7 @@ public ConstructorInfo? TypeInitializer [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents)] [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", - Justification = "Linker doesn't recongnize GetEvents(BindingFlags.Public) but this is what the body is doing")] + Justification = "Linker doesn't recognize GetEvents(BindingFlags.Public) but this is what the body is doing")] public virtual EventInfo[] GetEvents() => GetEvents(Type.DefaultLookup); [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypes.NonPublicEvents)] @@ -178,13 +178,21 @@ public ConstructorInfo? TypeInitializer [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", - Justification = "Linker doesn't recongnize GetFields(BindingFlags.Public) but this is what the body is doing")] + Justification = "Linker doesn't recognize GetFields(BindingFlags.Public) but this is what the body is doing")] public FieldInfo[] GetFields() => GetFields(Type.DefaultLookup); [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] public abstract FieldInfo[] GetFields(BindingFlags bindingAttr); - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] + [DynamicallyAccessedMembers( + DynamicallyAccessedMemberTypes.PublicFields + | DynamicallyAccessedMemberTypes.PublicMethods + | DynamicallyAccessedMemberTypes.PublicEvents + | DynamicallyAccessedMemberTypes.PublicProperties + | DynamicallyAccessedMemberTypes.PublicConstructors + | DynamicallyAccessedMemberTypes.PublicNestedTypes)] + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", + Justification = "Linker doesn't recognize GetMember(BindingFlags.Public) but this is what the body is doing")] public MemberInfo[] GetMember(string name) => GetMember(name, Type.DefaultLookup); [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] @@ -268,7 +276,7 @@ public ConstructorInfo? TypeInitializer [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", - Justification = "Linker doesn't recongnize GetMethods(BindingFlags.Public) but this is what the body is doing")] + Justification = "Linker doesn't recognize GetMethods(BindingFlags.Public) but this is what the body is doing")] public MethodInfo[] GetMethods() => GetMethods(Type.DefaultLookup); [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] @@ -282,7 +290,7 @@ public ConstructorInfo? TypeInitializer [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes)] [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", - Justification = "Linker doesn't recongnize GetNestedTypes(BindingFlags.Public) but this is what the body is doing")] + Justification = "Linker doesn't recognize GetNestedTypes(BindingFlags.Public) but this is what the body is doing")] public Type[] GetNestedTypes() => GetNestedTypes(Type.DefaultLookup); [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes | DynamicallyAccessedMemberTypes.NonPublicNestedTypes)] @@ -301,7 +309,7 @@ public ConstructorInfo? TypeInitializer [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", - Justification = "Linker doesn't recongnize GetPropertyImpl(BindingFlags.Public) but this is what the body is doing")] + Justification = "Linker doesn't recognize GetPropertyImpl(BindingFlags.Public) but this is what the body is doing")] public PropertyInfo? GetProperty(string name, Type? returnType) { if (name == null) @@ -333,12 +341,19 @@ public ConstructorInfo? TypeInitializer [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", - Justification = "Linker doesn't recongnize GetProperties(BindingFlags.Public) but this is what the body is doing")] + Justification = "Linker doesn't recognize GetProperties(BindingFlags.Public) but this is what the body is doing")] public PropertyInfo[] GetProperties() => GetProperties(Type.DefaultLookup); [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)] public abstract PropertyInfo[] GetProperties(BindingFlags bindingAttr); + [DynamicallyAccessedMembers( + DynamicallyAccessedMemberTypes.PublicFields + | DynamicallyAccessedMemberTypes.PublicMethods + | DynamicallyAccessedMemberTypes.PublicEvents + | DynamicallyAccessedMemberTypes.PublicProperties + | DynamicallyAccessedMemberTypes.PublicConstructors + | DynamicallyAccessedMemberTypes.PublicNestedTypes)] public virtual MemberInfo[] GetDefaultMembers() => throw NotImplemented.ByDesign; public virtual RuntimeTypeHandle TypeHandle => throw new NotSupportedException(); diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index 7fa3dac5f21062..79a16eec4f3b0d 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -4125,6 +4125,7 @@ protected Type() { } public System.Reflection.ConstructorInfo[] GetConstructors() { throw null; } [System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicConstructors | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)] public abstract System.Reflection.ConstructorInfo[] GetConstructors(System.Reflection.BindingFlags bindingAttr); + [System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicEvents | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicFields | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicNestedTypes | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)] public virtual System.Reflection.MemberInfo[] GetDefaultMembers() { throw null; } public abstract System.Type? GetElementType(); public virtual string? GetEnumName(object value) { throw null; } @@ -4155,7 +4156,7 @@ protected Type() { } public abstract System.Type? GetInterface(string name, bool ignoreCase); public virtual System.Reflection.InterfaceMapping GetInterfaceMap(System.Type interfaceType) { throw null; } public abstract System.Type[] GetInterfaces(); - [System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)] + [System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicEvents | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicFields | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicNestedTypes | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)] public System.Reflection.MemberInfo[] GetMember(string name) { throw null; } [System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)] public virtual System.Reflection.MemberInfo[] GetMember(string name, System.Reflection.BindingFlags bindingAttr) { throw null; } @@ -7499,10 +7500,10 @@ public abstract partial class Assembly : System.Reflection.ICustomAttributeProvi protected Assembly() { } public virtual string? CodeBase { get { throw null; } } public virtual System.Collections.Generic.IEnumerable CustomAttributes { get { throw null; } } - public virtual System.Collections.Generic.IEnumerable DefinedTypes { get { throw null; } } + public virtual System.Collections.Generic.IEnumerable DefinedTypes { [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed")] get { throw null; } } public virtual System.Reflection.MethodInfo? EntryPoint { get { throw null; } } public virtual string EscapedCodeBase { get { throw null; } } - public virtual System.Collections.Generic.IEnumerable ExportedTypes { get { throw null; } } + public virtual System.Collections.Generic.IEnumerable ExportedTypes { [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed")] get { throw null; } } public virtual string? FullName { get { throw null; } } public virtual bool GlobalAssemblyCache { get { throw null; } } public virtual long HostContext { get { throw null; } } @@ -7531,10 +7532,12 @@ public virtual event System.Reflection.ModuleResolveEventHandler? ModuleResolve public virtual System.Collections.Generic.IList GetCustomAttributesData() { throw null; } public static System.Reflection.Assembly? GetEntryAssembly() { throw null; } public static System.Reflection.Assembly GetExecutingAssembly() { throw null; } + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed")] public virtual System.Type[] GetExportedTypes() { throw null; } public virtual System.IO.FileStream? GetFile(string name) { throw null; } public virtual System.IO.FileStream[] GetFiles() { throw null; } public virtual System.IO.FileStream[] GetFiles(bool getResourceModules) { throw null; } + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed")] public virtual System.Type[] GetForwardedTypes() { throw null; } public override int GetHashCode() { throw null; } public System.Reflection.Module[] GetLoadedModules() { throw null; } @@ -7549,12 +7552,17 @@ public virtual event System.Reflection.ModuleResolveEventHandler? ModuleResolve public virtual System.Reflection.AssemblyName GetName() { throw null; } public virtual System.Reflection.AssemblyName GetName(bool copiedName) { throw null; } public virtual void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { } + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Assembly references might be removed")] 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")] public virtual System.Type? GetType(string name) { throw null; } + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed")] public virtual System.Type? GetType(string name, bool throwOnError) { throw null; } + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed")] 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; } public virtual bool IsDefined(System.Type attributeType, bool inherit) { throw null; } [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types and members the loaded assembly depends on might be removed")] diff --git a/src/mono/netcore/System.Private.CoreLib/src/System/Reflection/RuntimeModule.cs b/src/mono/netcore/System.Private.CoreLib/src/System/Reflection/RuntimeModule.cs index 1cbf688928d19f..0ce9f4c3e63034 100644 --- a/src/mono/netcore/System.Private.CoreLib/src/System/Reflection/RuntimeModule.cs +++ b/src/mono/netcore/System.Private.CoreLib/src/System/Reflection/RuntimeModule.cs @@ -21,6 +21,7 @@ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // +using System.Diagnostics.CodeAnalysis; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Runtime.CompilerServices; @@ -106,6 +107,7 @@ bool IsResource() return is_resource; } + [RequiresUnreferencedCode("Types might be removed")] public override Type[] FindTypes(TypeFilter? filter, object? filterCriteria) { @@ -128,6 +130,7 @@ object[] GetCustomAttributes(Type attributeType, bool inherit) return CustomAttribute.GetCustomAttributes(this, attributeType, inherit); } + [RequiresUnreferencedCode("Fields might be removed")] public override FieldInfo? GetField(string name, BindingFlags bindingAttr) { @@ -141,6 +144,7 @@ public override return globalType?.GetField(name, bindingAttr); } + [RequiresUnreferencedCode("Fields might be removed")] public override FieldInfo[] GetFields(BindingFlags bindingFlags) { @@ -160,6 +164,7 @@ int MetadataToken } } + [RequiresUnreferencedCode("Methods might be removed")] protected override MethodInfo? GetMethodImpl(string name, BindingFlags bindingAttr, Binder? binder, CallingConventions callConvention, Type[]? types, ParameterModifier[]? modifiers) @@ -175,6 +180,7 @@ int MetadataToken return globalType.GetMethod(name, bindingAttr, binder, callConvention, types, modifiers); } + [RequiresUnreferencedCode("Methods might be removed")] public override MethodInfo[] GetMethods(BindingFlags bindingFlags) diff --git a/src/mono/netcore/System.Private.CoreLib/src/System/RuntimeType.Mono.cs b/src/mono/netcore/System.Private.CoreLib/src/System/RuntimeType.Mono.cs index 06a117f9d048d5..ca347c87dafa9b 100644 --- a/src/mono/netcore/System.Private.CoreLib/src/System/RuntimeType.Mono.cs +++ b/src/mono/netcore/System.Private.CoreLib/src/System/RuntimeType.Mono.cs @@ -153,6 +153,7 @@ public void Add(T item) #region Internal + [RequiresUnreferencedCode("Types might be removed")] internal static RuntimeType? GetType(string typeName, bool throwOnError, bool ignoreCase, bool reflectionOnly, ref StackCrawlMark stackMark) { @@ -800,11 +801,13 @@ public override MemberInfo[] GetMembers(BindingFlags bindingAttr) #endregion + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] protected override MethodInfo? GetMethodImpl(string name, BindingFlags bindingAttr, Binder? binder, CallingConventions callConvention, Type[]? types, ParameterModifier[]? modifiers) { return GetMethodImpl(name, -1, bindingAttr, binder, callConvention, types, modifiers); } + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] protected override MethodInfo? GetMethodImpl(string name, int genericParamCount, BindingFlags bindingAttr, Binder? binder, CallingConventions callConv, Type[]? types, ParameterModifier[]? modifiers) diff --git a/src/mono/netcore/System.Private.CoreLib/src/System/RuntimeTypeHandle.cs b/src/mono/netcore/System.Private.CoreLib/src/System/RuntimeTypeHandle.cs index 1b5592444e54d4..9f59c10420eccb 100644 --- a/src/mono/netcore/System.Private.CoreLib/src/System/RuntimeTypeHandle.cs +++ b/src/mono/netcore/System.Private.CoreLib/src/System/RuntimeTypeHandle.cs @@ -283,6 +283,7 @@ internal static bool IsTypeDefinition(RuntimeType type) [MethodImplAttribute(MethodImplOptions.InternalCall)] private static extern RuntimeType internal_from_name(string name, ref StackCrawlMark stackMark, Assembly? callerAssembly, bool throwOnError, bool ignoreCase, bool reflectionOnly); + [RequiresUnreferencedCode("Types might be removed")] internal static RuntimeType? GetTypeByName(string typeName, bool throwOnError, bool ignoreCase, bool reflectionOnly, ref StackCrawlMark stackMark, bool loadTypeFromPartialName) { diff --git a/src/mono/netcore/System.Private.CoreLib/src/System/TypeNameParser.cs b/src/mono/netcore/System.Private.CoreLib/src/System/TypeNameParser.cs index 939ae1c5d4b045..85e0cfd3d38c73 100644 --- a/src/mono/netcore/System.Private.CoreLib/src/System/TypeNameParser.cs +++ b/src/mono/netcore/System.Private.CoreLib/src/System/TypeNameParser.cs @@ -4,6 +4,7 @@ using System.Text; using System.IO; +using System.Diagnostics.CodeAnalysis; using System.Reflection; using System.Collections.Generic; using System.Threading; @@ -14,6 +15,7 @@ internal static class TypeNameParser { private static readonly char[] SPECIAL_CHARS = { ',', '[', ']', '&', '*', '+', '\\' }; + [RequiresUnreferencedCode("Types might be removed")] internal static Type? GetType( string typeName, Func? assemblyResolver, @@ -36,6 +38,7 @@ internal static class TypeNameParser return ConstructType(pname, assemblyResolver, typeResolver, throwOnError, ignoreCase, ref stackMark); } + [RequiresUnreferencedCode("Types might be removed")] private static Type? ConstructType( ParsedName pname, Func? assemblyResolver, @@ -137,6 +140,7 @@ internal static class TypeNameParser } } + [RequiresUnreferencedCode("Types might be removed")] private static Type? ResolveType(Assembly assembly, List names, Func? typeResolver, bool throwOnError, bool ignoreCase, ref StackCrawlMark stackMark) { Type? type = null;