Skip to content
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

Add RequiresAssemblyFiles attribute #56196

Merged
merged 16 commits into from
Jul 30, 2021
Merged
Show file tree
Hide file tree
Changes from 11 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
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,13 @@ public override string[] GetManifestResourceNames()
throw new NotSupportedException(SR.NotSupported_DynamicAssembly);
}

[RequiresAssemblyFiles(ThrowingMessageInRAF)]
public override FileStream GetFile(string name)
{
throw new NotSupportedException(SR.NotSupported_DynamicAssembly);
}

[RequiresAssemblyFiles(ThrowingMessageInRAF)]
public override FileStream[] GetFiles(bool getResourceModules)
{
throw new NotSupportedException(SR.NotSupported_DynamicAssembly);
Expand All @@ -104,7 +106,7 @@ public override FileStream[] GetFiles(bool getResourceModules)

public override string Location => throw new NotSupportedException(SR.NotSupported_DynamicAssembly);

[RequiresAssemblyFiles("The code will throw for assemblies embedded in a single-file app")]
[RequiresAssemblyFiles(ThrowingMessageInRAF)]
public override string? CodeBase => throw new NotSupportedException(SR.NotSupported_DynamicAssembly);

[RequiresUnreferencedCode("Types might be removed")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,7 @@ internal Type[] GetTypesNoLock()
return GetType(parameters, baseType);
}

[RequiresAssemblyFiles(UnknownStringMessageInRAF)]
public override string FullyQualifiedName => _moduleData._moduleName;

[RequiresUnreferencedCode("Trimming changes metadata tokens")]
Expand Down Expand Up @@ -785,6 +786,7 @@ public override MethodInfo[] GetMethods(BindingFlags bindingFlags)

public override string ScopeName => InternalModule.ScopeName;

[RequiresAssemblyFiles(UnknownStringMessageInRAF)]
public override string Name => InternalModule.Name;

public override Assembly Assembly => _assemblyBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private static extern bool GetCodeBase(QCallAssembly assembly,
return null;
}

[RequiresAssemblyFiles("The code will throw for assemblies embedded in a single-file app")]
[RequiresAssemblyFiles(ThrowingMessageInRAF)]
public override string? CodeBase
{
get
Expand Down Expand Up @@ -372,6 +372,7 @@ private static extern void InternalLoad(ObjectHandleOnStack assemblyName,

// Returns the file in the File table of the manifest that matches the
// given name. (Name should not include path.)
[RequiresAssemblyFiles(ThrowingMessageInRAF)]
public override FileStream? GetFile(string name)
{
if (Location.Length == 0)
Expand All @@ -389,6 +390,7 @@ private static extern void InternalLoad(ObjectHandleOnStack assemblyName,
FileAccess.Read, FileShare.Read, FileStream.DefaultBufferSize, false);
}

[RequiresAssemblyFiles(ThrowingMessageInRAF)]
public override FileStream[] GetFiles(bool getResourceModules)
{
if (Location.Length == 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ public override void GetObjectData(SerializationInfo info, StreamingContext cont
return retType;
}

[RequiresAssemblyFiles(UnknownStringMessageInRAF)]
internal string GetFullyQualifiedName()
{
string? fullyQualifiedName = null;
Expand All @@ -458,9 +459,10 @@ internal string GetFullyQualifiedName()
return fullyQualifiedName!;
}

[RequiresAssemblyFiles(UnknownStringMessageInRAF)]
public override string FullyQualifiedName => GetFullyQualifiedName();

[RequiresUnreferencedCode("Types might be removed")]
[RequiresUnreferencedCode("Types might be removed")]
public override Type[] GetTypes()
{
return GetTypes(this);
Expand Down Expand Up @@ -527,6 +529,7 @@ public override string ScopeName
}
}

[RequiresAssemblyFiles(UnknownStringMessageInRAF)]
public override string Name
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ public virtual IEnumerable<Type> ExportedTypes
[RequiresUnreferencedCode("Types might be removed")]
public virtual Type[] GetForwardedTypes() { throw NotImplemented.ByDesign; }

[RequiresAssemblyFiles("The code will throw for assemblies embedded in a single-file app")]
internal const string ThrowingMessageInRAF = "This member throws an exception for assemblies embedded in a single-file app";

[RequiresAssemblyFiles(ThrowingMessageInRAF)]
public virtual string? CodeBase => throw NotImplemented.ByDesign;
public virtual MethodInfo? EntryPoint => throw NotImplemented.ByDesign;
public virtual string? FullName => throw NotImplemented.ByDesign;
Expand Down Expand Up @@ -116,7 +118,7 @@ public virtual IEnumerable<Type> ExportedTypes
public virtual object[] GetCustomAttributes(bool inherit) { throw NotImplemented.ByDesign; }
public virtual object[] GetCustomAttributes(Type attributeType, bool inherit) { throw NotImplemented.ByDesign; }

[RequiresAssemblyFiles("The code will throw for assemblies embedded in a single-file app")]
[RequiresAssemblyFiles(ThrowingMessageInRAF)]
public virtual string EscapedCodeBase => AssemblyName.EscapeCodeBase(CodeBase);

[RequiresUnreferencedCode("Assembly.CreateInstance is not supported with trimming. Use Type.GetType instead.")]
Expand Down Expand Up @@ -153,9 +155,11 @@ public virtual IEnumerable<Type> ExportedTypes
public virtual Assembly GetSatelliteAssembly(CultureInfo culture) { throw NotImplemented.ByDesign; }
public virtual Assembly GetSatelliteAssembly(CultureInfo culture, Version? version) { throw NotImplemented.ByDesign; }

[RequiresAssemblyFiles(ThrowingMessageInRAF)]
public virtual FileStream? GetFile(string name) { throw NotImplemented.ByDesign; }
[RequiresAssemblyFiles("The code will throw for assemblies embedded in a single-file app")]
[RequiresAssemblyFiles(ThrowingMessageInRAF)]
public virtual FileStream[] GetFiles() => GetFiles(getResourceModules: false);
[RequiresAssemblyFiles(ThrowingMessageInRAF)]
public virtual FileStream[] GetFiles(bool getResourceModules) { throw NotImplemented.ByDesign; }

public virtual void GetObjectData(SerializationInfo info, StreamingContext context) { throw NotImplemented.ByDesign; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace System.Reflection.Emit
{
public sealed partial class AssemblyBuilder : Assembly
{
[RequiresAssemblyFiles("The code will throw for assemblies embedded in a single-file app")]
[RequiresAssemblyFiles(ThrowingMessageInRAF)]
public override string? CodeBase => throw new NotSupportedException(SR.NotSupported_DynamicAssembly);
public override string Location => throw new NotSupportedException(SR.NotSupported_DynamicAssembly);
public override MethodInfo? EntryPoint => null;
Expand All @@ -18,9 +18,11 @@ public sealed partial class AssemblyBuilder : Assembly
public override Type[] GetExportedTypes() =>
throw new NotSupportedException(SR.NotSupported_DynamicAssembly);

[RequiresAssemblyFiles(ThrowingMessageInRAF)]
public override FileStream GetFile(string name) =>
throw new NotSupportedException(SR.NotSupported_DynamicAssembly);

[RequiresAssemblyFiles(ThrowingMessageInRAF)]
public override FileStream[] GetFiles(bool getResourceModules) =>
throw new NotSupportedException(SR.NotSupported_DynamicAssembly);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ public abstract partial class Module : ICustomAttributeProvider, ISerializable
protected Module() { }

public virtual Assembly Assembly => throw NotImplemented.ByDesign;

internal const string UnknownStringMessageInRAF = "Returns <Unknown> for modules with no file path";
[RequiresAssemblyFiles(UnknownStringMessageInRAF)]
public virtual string FullyQualifiedName => throw NotImplemented.ByDesign;
[RequiresAssemblyFiles(UnknownStringMessageInRAF)]
public virtual string Name => throw NotImplemented.ByDesign;

public virtual int MDStreamVersion => throw NotImplemented.ByDesign;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;

namespace System.Reflection.Context.Delegation
{
Expand All @@ -22,6 +23,11 @@ public override Assembly Assembly
get { return UnderlyingModule.Assembly; }
}

internal const string UnknownStringMessageInRAF = "Returns <Unknown> for modules with no file path";

#if NET5_0_OR_GREATER
[RequiresAssemblyFiles(UnknownStringMessageInRAF)]
#endif
public override string FullyQualifiedName
{
get { return UnderlyingModule.FullyQualifiedName; }
Expand All @@ -42,6 +48,9 @@ public override Guid ModuleVersionId
get { return UnderlyingModule.ModuleVersionId; }
}

#if NET5_0_OR_GREATER
[RequiresAssemblyFiles(UnknownStringMessageInRAF)]
#endif
public override string Name
{
get { return UnderlyingModule.Name; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ internal AssemblyBuilder() { }
public System.Reflection.Emit.ModuleBuilder? GetDynamicModule(string name) { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed")]
public override System.Type[] GetExportedTypes() { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresAssemblyFilesAttribute("This member throws an exception for assemblies embedded in a single-file app")]
buyaa-n marked this conversation as resolved.
Show resolved Hide resolved
public override System.IO.FileStream GetFile(string name) { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresAssemblyFilesAttribute("This member throws an exception for assemblies embedded in a single-file app")]
public override System.IO.FileStream[] GetFiles(bool getResourceModules) { throw null; }
public override int GetHashCode() { throw null; }
public override System.Reflection.Module[] GetLoadedModules(bool getResourceModules) { throw null; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using System.Threading;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Collections.Generic;

namespace System.Reflection.TypeLoading
Expand All @@ -16,6 +17,9 @@ internal abstract partial class RoAssembly
public sealed override Module? GetModule(string name) => GetRoModule(name);
public sealed override Module[] GetModules(bool getResourceModules) => ComputeRoModules(getResourceModules).CloneArray<Module>();

#if NET5_0_OR_GREATER
[RequiresAssemblyFiles(ThrowingMessageInRAF)]
#endif
public sealed override FileStream? GetFile(string name)
{
Module? m = GetModule(name);
Expand All @@ -24,6 +28,9 @@ internal abstract partial class RoAssembly
return new FileStream(m.FullyQualifiedName, FileMode.Open, FileAccess.Read, FileShare.Read);
}

#if NET5_0_OR_GREATER
[RequiresAssemblyFiles(ThrowingMessageInRAF)]
#endif
public sealed override FileStream[] GetFiles(bool getResourceModules)
{
Module[] m = GetModules(getResourceModules);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,18 @@ protected RoAssembly(MetadataLoadContext loader, int assemblyFileCount)
public sealed override string FullName => _lazyFullName ?? (_lazyFullName = GetName().FullName);
private volatile string? _lazyFullName;

internal const string ThrowingMessageInRAF = "This member throws an exception for assemblies embedded in a single-file app";

// Location and codebase
public abstract override string Location { get; }
#if NET5_0_OR_GREATER
[Obsolete(Obsoletions.CodeBaseMessage, DiagnosticId = Obsoletions.CodeBaseDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
[RequiresAssemblyFiles("The code will throw for assemblies embedded in a single-file app")]
[RequiresAssemblyFiles(ThrowingMessageInRAF)]
#endif
public sealed override string CodeBase => throw new NotSupportedException(SR.NotSupported_AssemblyCodeBase);
#if NET5_0_OR_GREATER
[Obsolete(Obsoletions.CodeBaseMessage, DiagnosticId = Obsoletions.CodeBaseDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
[RequiresAssemblyFiles("The code will throw for assemblies embedded in a single-file app")]
[RequiresAssemblyFiles(ThrowingMessageInRAF)]
#endif
public sealed override string EscapedCodeBase => throw new NotSupportedException(SR.NotSupported_AssemblyCodeBase);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Runtime.Serialization;

Expand Down Expand Up @@ -30,11 +31,19 @@ internal RoModule(string fullyQualifiedName)
public sealed override Assembly Assembly => GetRoAssembly();
internal abstract RoAssembly GetRoAssembly();

internal const string UnknownStringMessageInRAF = "Returns <Unknown> for modules with no file path";

#if NET5_0_OR_GREATER
[RequiresAssemblyFiles(UnknownStringMessageInRAF)]
#endif
public sealed override string FullyQualifiedName => _fullyQualifiedName;
public abstract override int MDStreamVersion { get; }
public abstract override int MetadataToken { get; }
public abstract override Guid ModuleVersionId { get; }

#if NET5_0_OR_GREATER
[RequiresAssemblyFiles(UnknownStringMessageInRAF)]
#endif
public sealed override string Name
{
get
Expand Down
10 changes: 7 additions & 3 deletions src/libraries/System.Runtime/ref/System.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11060,13 +11060,13 @@ public abstract partial class Assembly : System.Reflection.ICustomAttributeProvi
{
protected Assembly() { }
agocke marked this conversation as resolved.
Show resolved Hide resolved
[System.ObsoleteAttribute("Assembly.CodeBase and Assembly.EscapedCodeBase are only included for .NET Framework compatibility. Use Assembly.Location instead.", DiagnosticId = "SYSLIB0012", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")]
[System.Diagnostics.CodeAnalysis.RequiresAssemblyFilesAttribute("The code will throw for assemblies embedded in a single-file app")]
[System.Diagnostics.CodeAnalysis.RequiresAssemblyFilesAttribute("This member throws an exception for assemblies embedded in a single-file app")]
buyaa-n marked this conversation as resolved.
Show resolved Hide resolved
public virtual string? CodeBase { get { throw null; } }
public virtual System.Collections.Generic.IEnumerable<System.Reflection.CustomAttributeData> CustomAttributes { get { throw null; } }
public virtual System.Collections.Generic.IEnumerable<System.Reflection.TypeInfo> DefinedTypes { [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed")] get { throw null; } }
public virtual System.Reflection.MethodInfo? EntryPoint { get { throw null; } }
[System.ObsoleteAttribute("Assembly.CodeBase and Assembly.EscapedCodeBase are only included for .NET Framework compatibility. Use Assembly.Location instead.", DiagnosticId = "SYSLIB0012", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")]
[System.Diagnostics.CodeAnalysis.RequiresAssemblyFilesAttribute("The code will throw for assemblies embedded in a single-file app")]
[System.Diagnostics.CodeAnalysis.RequiresAssemblyFilesAttribute("This member throws an exception for assemblies embedded in a single-file app")]
public virtual string EscapedCodeBase { get { throw null; } }
public virtual System.Collections.Generic.IEnumerable<System.Type> ExportedTypes { [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed")] get { throw null; } }
public virtual string? FullName { get { throw null; } }
Expand Down Expand Up @@ -11100,9 +11100,11 @@ public virtual event System.Reflection.ModuleResolveEventHandler? ModuleResolve
public static System.Reflection.Assembly GetExecutingAssembly() { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed")]
public virtual System.Type[] GetExportedTypes() { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresAssemblyFilesAttribute("This member throws an exception for assemblies embedded in a single-file app")]
public virtual System.IO.FileStream? GetFile(string name) { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresAssemblyFiles("The code will throw for assemblies embedded in a single-file app")]
[System.Diagnostics.CodeAnalysis.RequiresAssemblyFilesAttribute("This member throws an exception for assemblies embedded in a single-file app")]
public virtual System.IO.FileStream[] GetFiles() { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresAssemblyFilesAttribute("This member throws an exception for assemblies embedded in a single-file app")]
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; }
Expand Down Expand Up @@ -11867,11 +11869,13 @@ public abstract partial class Module : System.Reflection.ICustomAttributeProvide
protected Module() { }
public virtual System.Reflection.Assembly Assembly { get { throw null; } }
public virtual System.Collections.Generic.IEnumerable<System.Reflection.CustomAttributeData> CustomAttributes { get { throw null; } }
[System.Diagnostics.CodeAnalysis.RequiresAssemblyFilesAttribute("Returns <Unknown> for modules with no file path")]
public virtual string FullyQualifiedName { get { throw null; } }
public virtual int MDStreamVersion { get { throw null; } }
public virtual int MetadataToken { get { throw null; } }
public System.ModuleHandle ModuleHandle { get { throw null; } }
public virtual System.Guid ModuleVersionId { get { throw null; } }
[System.Diagnostics.CodeAnalysis.RequiresAssemblyFilesAttribute("Returns <Unknown> for modules with no file path")]
public virtual string Name { get { throw null; } }
public virtual string ScopeName { get { throw null; } }
public override bool Equals(object? o) { throw null; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ internal ModuleBuilder(AssemblyBuilder assb, string name, bool emitSymbolInfo)
set_wrappers_type(this, type);
}

[RequiresAssemblyFiles(UnknownStringMessageInRAF)]
public override string FullyQualifiedName
{
get
Expand Down Expand Up @@ -823,6 +824,7 @@ public override Assembly Assembly
get { return assemblyb; }
}

[RequiresAssemblyFiles(UnknownStringMessageInRAF)]
public override string Name
{
get { return name; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public extern override MethodInfo? EntryPoint

public override bool ReflectionOnly => false;

[RequiresAssemblyFiles("The code will throw for assemblies embedded in a single-file app")]
[RequiresAssemblyFiles(ThrowingMessageInRAF)]
public override string? CodeBase
{
get
Expand Down Expand Up @@ -410,6 +410,7 @@ public override Assembly GetSatelliteAssembly(CultureInfo culture, Version? vers
return res;
}

[RequiresAssemblyFiles(ThrowingMessageInRAF)]
public override FileStream? GetFile(string name)
{
if (name == null)
Expand All @@ -430,6 +431,7 @@ public override Assembly GetSatelliteAssembly(CultureInfo culture, Version? vers
return null;
}

[RequiresAssemblyFiles(ThrowingMessageInRAF)]
public override FileStream[] GetFiles(bool getResourceModules)
{
if (Location.Length == 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Assembly Assembly
get { return assembly; }
}

[RequiresAssemblyFiles(UnknownStringMessageInRAF)]
public
override
// Note: we do not ask for PathDiscovery because no path is returned here.
Expand Down Expand Up @@ -94,6 +95,7 @@ Guid ModuleVersionId
}
}

[RequiresAssemblyFiles(UnknownStringMessageInRAF)]
public override
string FullyQualifiedName
{
Expand Down