Skip to content

Annotate unsupported APIs in .Net core as Obsolete - Reflection area #50941

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Apr 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/project/list-of-diagnostics.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ The PR that reveals the implementation of the `<IncludeInternalObsoleteAttribute
| __`SYSLIB0014`__ | WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead. |
| __`SYSLIB0015`__ | DisablePrivateReflectionAttribute has no effect in .NET 6.0+ applications. |
| __`SYSLIB0016`__ | Use the Graphics.GetContextInfo overloads that accept arguments for better performance and fewer allocations. |
| __`SYSLIB0017`__ | Strong name signing is not supported and throws PlatformNotSupportedException. |
| __`SYSLIB0018`__ | ReflectionOnly loading is not supported and throws PlatformNotSupportedException. |
| __`SYSLIB0019`__ | RuntimeEnvironment members SystemConfigurationFile, GetRuntimeInterfaceAsIntPtr, and GetRuntimeInterfaceAsObject are no longer supported and throw PlatformNotSupportedException. |
| __`SYSLIB0020`__ | JsonSerializerOptions.IgnoreNullValues is obsolete. To ignore null values when serializing, set DefaultIgnoreCondition to JsonIgnoreCondition.WhenWritingNull. |

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ internal AssemblyName(string? name,
AssemblyHashAlgorithm hashAlgorithm,
AssemblyVersionCompatibility versionCompatibility,
string? codeBase,
AssemblyNameFlags flags,
StrongNameKeyPair? keyPair) // Null if ref, matching Assembly if def
AssemblyNameFlags flags)
{
_name = name;
_publicKey = publicKey;
Expand All @@ -43,7 +42,6 @@ internal AssemblyName(string? name,
_versionCompatibility = versionCompatibility;
_codeBase = codeBase;
_flags = flags;
_strongNameKeyPair = keyPair;
}

[MethodImpl(MethodImplOptions.InternalCall)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ public override AssemblyName GetName(bool copiedName)
GetHashAlgorithm(),
AssemblyVersionCompatibility.SameMachine,
codeBase,
GetFlags() | AssemblyNameFlags.PublicKey,
null); // strong name key pair
GetFlags() | AssemblyNameFlags.PublicKey);

Module manifestModule = ManifestModule;
if (manifestModule.MDStreamVersion > 0x10000)
Expand Down
1 change: 0 additions & 1 deletion src/coreclr/vm/assemblyspec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,6 @@ void AssemblySpec::AssemblyNameInit(ASSEMBLYNAMEREF* pAsmName, PEImage* pImageIn
(ARG_SLOT) 1, // AssemblyVersionCompatibility.SameMachine
ObjToArgSlot(gc.CodeBase),
(ARG_SLOT) m_dwFlags,
(ARG_SLOT) NULL // key pair
};

init.Call(MethodArgs);
Expand Down
4 changes: 1 addition & 3 deletions src/coreclr/vm/corelib.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ DEFINE_FIELD_U(_hashAlgorithm, AssemblyNameBaseObject, _hashAlgorith
DEFINE_FIELD_U(_versionCompatibility, AssemblyNameBaseObject, _versionCompatibility)
DEFINE_FIELD_U(_flags, AssemblyNameBaseObject, _flags)
DEFINE_CLASS(ASSEMBLY_NAME, Reflection, AssemblyName)
DEFINE_METHOD(ASSEMBLY_NAME, CTOR, .ctor, IM_Str_ArrB_ArrB_Ver_CI_AHA_AVC_Str_ANF_SNKP_RetV)
DEFINE_METHOD(ASSEMBLY_NAME, CTOR, .ctor, IM_Str_ArrB_ArrB_Ver_CI_AHA_AVC_Str_ANF_RetV)
DEFINE_METHOD(ASSEMBLY_NAME, SET_PROC_ARCH_INDEX, SetProcArchIndex, IM_PEK_IFM_RetV)

DEFINE_CLASS_U(System, Version, VersionBaseObject)
Expand Down Expand Up @@ -864,8 +864,6 @@ DEFINE_METHOD(STRING_BUILDER, INTERNAL_COPY, InternalCopy,
DEFINE_METHOD(STRING_BUILDER, REPLACE_BUFFER_INTERNAL,ReplaceBufferInternal, IM_PtrChar_Int_RetVoid)
DEFINE_METHOD(STRING_BUILDER, REPLACE_BUFFER_ANSI_INTERNAL,ReplaceBufferAnsiInternal, IM_PtrSByt_Int_RetVoid)

DEFINE_CLASS(STRONG_NAME_KEY_PAIR, Reflection, StrongNameKeyPair)

DEFINE_CLASS_U(Threading, SynchronizationContext, SynchronizationContextObject)
DEFINE_FIELD_U(_requireWaitNotification, SynchronizationContextObject, _requireWaitNotification)
DEFINE_CLASS(SYNCHRONIZATION_CONTEXT, Threading, SynchronizationContext)
Expand Down
4 changes: 2 additions & 2 deletions src/coreclr/vm/metasig.h
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,8 @@ DEFINE_METASIG(IM(RetInt, _, i))
DEFINE_METASIG_T(IM(RetAssemblyName, _, C(ASSEMBLY_NAME)))
DEFINE_METASIG_T(IM(RetAssemblyBase, _, C(ASSEMBLYBASE)))
DEFINE_METASIG_T(IM(RetModule, _, C(MODULE)))
DEFINE_METASIG_T(IM(Str_ArrB_ArrB_Ver_CI_AHA_AVC_Str_ANF_SNKP_RetV,
s a(b) a(b) C(VERSION) C(CULTURE_INFO) g(ASSEMBLY_HASH_ALGORITHM) g(ASSEMBLY_VERSION_COMPATIBILITY) s g(ASSEMBLY_NAME_FLAGS) C(STRONG_NAME_KEY_PAIR), v))
DEFINE_METASIG_T(IM(Str_ArrB_ArrB_Ver_CI_AHA_AVC_Str_ANF_RetV,
s a(b) a(b) C(VERSION) C(CULTURE_INFO) g(ASSEMBLY_HASH_ALGORITHM) g(ASSEMBLY_VERSION_COMPATIBILITY) s g(ASSEMBLY_NAME_FLAGS), v))
DEFINE_METASIG_T(IM(PEK_IFM_RetV,
g(PORTABLE_EXECUTABLE_KINDS) g(IMAGE_FILE_MACHINE), v))
DEFINE_METASIG(IM(RetObj, _, j))
Expand Down
2 changes: 0 additions & 2 deletions src/coreclr/vm/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -1535,7 +1535,6 @@ class AssemblyNameBaseObject : public Object
OBJECTREF _cultureInfo;
OBJECTREF _codeBase;
OBJECTREF _version;
OBJECTREF _strongNameKeyPair;
DWORD _hashAlgorithm;
DWORD _versionCompatibility;
DWORD _flags;
Expand All @@ -1548,7 +1547,6 @@ class AssemblyNameBaseObject : public Object
OBJECTREF GetSimpleName() { LIMITED_METHOD_CONTRACT; return _name; }
U1ARRAYREF GetPublicKey() { LIMITED_METHOD_CONTRACT; return _publicKey; }
U1ARRAYREF GetPublicKeyToken() { LIMITED_METHOD_CONTRACT; return _publicKeyToken; }
OBJECTREF GetStrongNameKeyPair() { LIMITED_METHOD_CONTRACT; return _strongNameKeyPair; }
OBJECTREF GetCultureInfo() { LIMITED_METHOD_CONTRACT; return _cultureInfo; }
OBJECTREF GetAssemblyCodeBase() { LIMITED_METHOD_CONTRACT; return _codeBase; }
OBJECTREF GetVersion() { LIMITED_METHOD_CONTRACT; return _version; }
Expand Down
6 changes: 6 additions & 0 deletions src/libraries/Common/src/System/Obsoletions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ internal static class Obsoletions
internal const string GetContextInfoMessage = "Use the Graphics.GetContextInfo overloads that accept arguments for better performance and fewer allocations.";
internal const string GetContextInfoDiagId = "SYSLIB0016";

internal const string StrongNameKeyPairMessage = "Strong name signing is not supported and throws PlatformNotSupportedException.";
internal const string StrongNameKeyPairDiagId = "SYSLIB0017";

internal const string ReflectionOnlyLoadingMessage = "ReflectionOnly loading is not supported and throws PlatformNotSupportedException.";
internal const string ReflectionOnlyLoadingDiagId = "SYSLIB0018";

internal const string RuntimeEnvironmentMessage = "RuntimeEnvironment members SystemConfigurationFile, GetRuntimeInterfaceAsIntPtr, and GetRuntimeInterfaceAsObject are no longer supported and throw PlatformNotSupportedException.";
internal const string RuntimeEnvironmentDiagId = "SYSLIB0019";

Expand Down
4 changes: 2 additions & 2 deletions src/libraries/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
<NoWarn Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework' or '$(TargetFrameworkIdentifier)' == '.NETStandard' or ('$(TargetFrameworkIdentifier)' == '.NETCoreApp' and $([MSBuild]::VersionLessThan($(TargetFrameworkVersion), '3.0')))">$(NoWarn);nullable</NoWarn>
<NoWarn Condition="'$(GeneratePlatformNotSupportedAssembly)' == 'true' or '$(GeneratePlatformNotSupportedAssemblyMessage)' != ''">$(NoWarn);nullable;CA1052</NoWarn>
<!-- Ignore Obsolete errors within the generated shims that type-forward types.
SYSLIB0003 is for CAS. SYSLIB0004 is the obsoletion of CER. -->
<NoWarn Condition="'$(IsPartialFacadeAssembly)' == 'true'">$(NoWarn);SYSLIB0003;SYSLIB0004;SYSLIB0015</NoWarn>
SYSLIB0003: Code Access Security (CAS). SYSLIB0004: Constrained Execution Region (CER). SYSLIB0017: Strong name signing. -->
<NoWarn Condition="'$(IsPartialFacadeAssembly)' == 'true'">$(NoWarn);SYSLIB0003;SYSLIB0004;SYSLIB0015;SYSLIB0017</NoWarn>
<!-- Reset these properties back to blank, since they are defaulted by Microsoft.NET.Sdk -->
<WarningsAsErrors Condition="'$(WarningsAsErrors)' == 'NU1605'" />
<!-- Set the documentation output file globally. -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,10 +372,13 @@ public static Assembly LoadFrom(string assemblyFile, byte[]? hashValue, Assembly
[RequiresUnreferencedCode("Types and members the loaded module depends on might be removed")]
public virtual Module LoadModule(string moduleName, byte[]? rawModule, byte[]? rawSymbolStore) { throw NotImplemented.ByDesign; }

[Obsolete(Obsoletions.ReflectionOnlyLoadingMessage, DiagnosticId = Obsoletions.ReflectionOnlyLoadingDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
[RequiresUnreferencedCode("Types and members the loaded assembly depends on might be removed")]
public static Assembly ReflectionOnlyLoad(byte[] rawAssembly) { throw new PlatformNotSupportedException(SR.PlatformNotSupported_ReflectionOnly); }
[Obsolete(Obsoletions.ReflectionOnlyLoadingMessage, DiagnosticId = Obsoletions.ReflectionOnlyLoadingDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
[RequiresUnreferencedCode("Types and members the loaded assembly depends on might be removed")]
public static Assembly ReflectionOnlyLoad(string assemblyString) { throw new PlatformNotSupportedException(SR.PlatformNotSupported_ReflectionOnly); }
[Obsolete(Obsoletions.ReflectionOnlyLoadingMessage, DiagnosticId = Obsoletions.ReflectionOnlyLoadingDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
[RequiresUnreferencedCode("Types and members the loaded assembly depends on might be removed")]
public static Assembly ReflectionOnlyLoadFrom(string assemblyFile) { throw new PlatformNotSupportedException(SR.PlatformNotSupported_ReflectionOnly); }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ public sealed partial class AssemblyName : ICloneable, IDeserializationCallback,
private string? _codeBase;
private Version? _version;

private StrongNameKeyPair? _strongNameKeyPair;

private AssemblyHashAlgorithm _hashAlgorithm;

private AssemblyVersionCompatibility _versionCompatibility;
Expand Down Expand Up @@ -200,10 +198,11 @@ public AssemblyVersionCompatibility VersionCompatibility
set => _versionCompatibility = value;
}

[Obsolete(Obsoletions.StrongNameKeyPairMessage, DiagnosticId = Obsoletions.StrongNameKeyPairDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
public StrongNameKeyPair? KeyPair
{
get => _strongNameKeyPair;
set => _strongNameKeyPair = value;
get => throw new PlatformNotSupportedException(SR.PlatformNotSupported_StrongNameSigning);
set => throw new PlatformNotSupportedException(SR.PlatformNotSupported_StrongNameSigning);
}

public string FullName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,14 @@

namespace System.Reflection
{
[Obsolete(Obsoletions.StrongNameKeyPairMessage, DiagnosticId = Obsoletions.StrongNameKeyPairDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
public class StrongNameKeyPair : IDeserializationCallback, ISerializable
{
// Build key pair from file.
public StrongNameKeyPair(FileStream keyPairFile)
{
if (keyPairFile == null)
throw new ArgumentNullException(nameof(keyPairFile));

int length = (int)keyPairFile.Length;
byte[] keyPairArray = new byte[length];
keyPairFile.Read(keyPairArray, 0, length);
}

// Build key pair from byte array in memory.
public StrongNameKeyPair(byte[] keyPairArray)
{
if (keyPairArray == null)
throw new ArgumentNullException(nameof(keyPairArray));
}
public StrongNameKeyPair(FileStream keyPairFile) =>
throw new PlatformNotSupportedException(SR.PlatformNotSupported_StrongNameSigning);

public StrongNameKeyPair(byte[] keyPairArray) =>
throw new PlatformNotSupportedException(SR.PlatformNotSupported_StrongNameSigning);

protected StrongNameKeyPair(SerializationInfo info, StreamingContext context) =>
throw new PlatformNotSupportedException();
Expand Down
1 change: 1 addition & 0 deletions src/libraries/System.Private.CoreLib/src/System/Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ public override int GetHashCode()
}
public virtual bool Equals(Type? o) => o == null ? false : object.ReferenceEquals(this.UnderlyingSystemType, o.UnderlyingSystemType);

[Obsolete(Obsoletions.ReflectionOnlyLoadingMessage, DiagnosticId = Obsoletions.ReflectionOnlyLoadingDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
public static Type? ReflectionOnlyGetType(string typeName, bool throwIfNotFound, bool ignoreCase) => throw new PlatformNotSupportedException(SR.PlatformNotSupported_ReflectionOnly);

public static Binder DefaultBinder
Expand Down
2 changes: 2 additions & 0 deletions src/libraries/System.Reflection/tests/AssemblyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,7 @@ public void AssemblyLoadFromBytesWithSymbols()
Assert.Equal(assembly.FullName, loadedAssembly.FullName);
}

#pragma warning disable SYSLIB0018 // ReflectionOnly loading is not supported and throws PlatformNotSupportedException.
[Fact]
public void AssemblyReflectionOnlyLoadFromString()
{
Expand All @@ -750,6 +751,7 @@ public void AssemblyReflectionOnlyLoadFromNeg()
Assert.Throws<PlatformNotSupportedException>(() => Assembly.ReflectionOnlyLoad(string.Empty));
Assert.Throws<PlatformNotSupportedException>(() => Assembly.ReflectionOnlyLoad((byte[])null));
}
#pragma warning restore SYSLIB0018

public static IEnumerable<object[]> GetModules_TestData()
{
Expand Down
6 changes: 6 additions & 0 deletions src/libraries/System.Runtime/ref/System.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4571,6 +4571,7 @@ protected Type() { }
public virtual System.Type MakePointerType() { throw null; }
public static bool operator ==(System.Type? left, System.Type? right) { throw null; }
public static bool operator !=(System.Type? left, System.Type? right) { throw null; }
[System.ObsoleteAttribute("ReflectionOnly loading is not supported and throws PlatformNotSupportedException.", DiagnosticId = "SYSLIB0018", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")]
public static System.Type? ReflectionOnlyGetType(string typeName, bool throwIfNotFound, bool ignoreCase) { throw null; }
public override string ToString() { throw null; }
}
Expand Down Expand Up @@ -7944,10 +7945,13 @@ public virtual void GetObjectData(System.Runtime.Serialization.SerializationInfo
public static System.Reflection.Assembly? LoadWithPartialName(string partialName) { throw null; }
public static bool operator ==(System.Reflection.Assembly? left, System.Reflection.Assembly? right) { throw null; }
public static bool operator !=(System.Reflection.Assembly? left, System.Reflection.Assembly? right) { throw null; }
[System.ObsoleteAttribute("ReflectionOnly loading is not supported and throws PlatformNotSupportedException.", DiagnosticId = "SYSLIB0018", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types and members the loaded assembly depends on might be removed")]
public static System.Reflection.Assembly ReflectionOnlyLoad(byte[] rawAssembly) { throw null; }
[System.ObsoleteAttribute("ReflectionOnly loading is not supported and throws PlatformNotSupportedException.", DiagnosticId = "SYSLIB0018", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types and members the loaded assembly depends on might be removed")]
public static System.Reflection.Assembly ReflectionOnlyLoad(string assemblyString) { throw null; }
[System.ObsoleteAttribute("ReflectionOnly loading is not supported and throws PlatformNotSupportedException.", DiagnosticId = "SYSLIB0018", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types and members the loaded assembly depends on might be removed")]
public static System.Reflection.Assembly ReflectionOnlyLoadFrom(string assemblyFile) { throw null; }
public override string ToString() { throw null; }
Expand Down Expand Up @@ -8067,6 +8071,7 @@ public AssemblyName(string assemblyName) { }
public System.Reflection.AssemblyNameFlags Flags { get { throw null; } set { } }
public string FullName { get { throw null; } }
public System.Configuration.Assemblies.AssemblyHashAlgorithm HashAlgorithm { get { throw null; } set { } }
[System.ObsoleteAttribute("Strong name signing is not supported and throws PlatformNotSupportedException.", DiagnosticId = "SYSLIB0017", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")]
public System.Reflection.StrongNameKeyPair? KeyPair { get { throw null; } set { } }
public string? Name { get { throw null; } set { } }
public System.Reflection.ProcessorArchitecture ProcessorArchitecture { get { throw null; } set { } }
Expand Down Expand Up @@ -8914,6 +8919,7 @@ public static partial class RuntimeReflectionExtensions
public static System.Collections.Generic.IEnumerable<System.Reflection.PropertyInfo> GetRuntimeProperties([System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicProperties | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)] this System.Type type) { throw null; }
public static System.Reflection.PropertyInfo? GetRuntimeProperty([System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)] this System.Type type, string name) { throw null; }
}
[System.ObsoleteAttribute("Strong name signing is not supported and throws PlatformNotSupportedException.", DiagnosticId = "SYSLIB0017", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")]
public partial class StrongNameKeyPair : System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
{
public StrongNameKeyPair(byte[] keyPairArray) { }
Expand Down
Loading