Skip to content

Commit 3d7ebcf

Browse files
authored
Make PersistedAssemblyBuilder public and update all references (#99660)
* Add public PersistedAssemblyBuilder and update all references * Remove unused resource strings
1 parent 4cf19ee commit 3d7ebcf

23 files changed

+307
-212
lines changed

src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/ILEmit/ILEmitResolverBuilder.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ private GeneratedMethod BuildTypeNoCache(ServiceCallSite callSite)
108108
var assembly = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName(assemblyName), AssemblyBuilderAccess.RunAndSave);
109109
var module = assembly.DefineDynamicModule(assemblyName, fileName);
110110
#else
111-
var assembly = AssemblyBuilder.DefinePersistedAssembly(new AssemblyName(assemblyName), typeof(object).Assembly);
111+
var assembly = new PersistedAssemblyBuilder(new AssemblyName(assemblyName), typeof(object).Assembly);
112112
var module = assembly.DefineDynamicModule(assemblyName);
113113
#endif
114114
var type = module.DefineType(callSite.ServiceType.Name + "Resolver");

src/libraries/System.Private.CoreLib/src/Resources/Strings.resx

+2-5
Original file line numberDiff line numberDiff line change
@@ -3346,7 +3346,7 @@
33463346
</data>
33473347
<data name="RFLCT_Targ_ITargMismatch_WithType" xml:space="preserve">
33483348
<value>Object type {0} does not match target type {1}.</value>
3349-
</data>
3349+
</data>
33503350
<data name="RFLCT_Targ_StatFldReqTarg" xml:space="preserve">
33513351
<value>Non-static field requires a target.</value>
33523352
</data>
@@ -4304,10 +4304,7 @@
43044304
<data name="RFLCT_CannotSetInitonlyStaticField" xml:space="preserve">
43054305
<value>Cannot set initonly static field '{0}' after type '{1}' is initialized.</value>
43064306
</data>
4307-
<data name="NotSupported_AssemblySave" xml:space="preserve">
4308-
<value>This AssemblyBuilder instance doesn't support saving. Use AssemblyBuilder.DefinePersistedAssembly to create an AssemblyBuilder instance that supports saving.</value>
4309-
</data>
43104307
<data name="WasmThreads_BlockingWaitNotSupportedOnJSInterop" xml:space="preserve">
43114308
<value>Blocking wait is not supported on the JS interop threads.</value>
43124309
</data>
4313-
</root>
4310+
</root>

src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/AssemblyBuilder.cs

-48
Original file line numberDiff line numberDiff line change
@@ -33,56 +33,8 @@ public ModuleBuilder DefineDynamicModule(string name)
3333
return GetDynamicModuleCore(name);
3434
}
3535

36-
/// <summary>
37-
/// Defines an <see cref="AssemblyBuilder"/> that can be saved to a file or stream.
38-
/// </summary>
39-
/// <param name="name">The name of the assembly.</param>
40-
/// <param name="coreAssembly">The assembly that denotes the "system assembly" that houses the well-known types such as <see cref="object"/></param>
41-
/// <param name="assemblyAttributes">A collection that contains the attributes of the assembly.</param>
42-
/// <returns>An <see cref="AssemblyBuilder"/> that can be persisted.</returns>
43-
/// <exception cref="ArgumentNullException">The <paramref name="name"/> or <paramref name="name.Name"/> or <paramref name="coreAssembly"/> is null.</exception>
44-
/// <remarks>Currently the persisted assembly doesn't support running, need to save it and load back to run.</remarks>
45-
public static AssemblyBuilder DefinePersistedAssembly(AssemblyName name, Assembly coreAssembly, IEnumerable<CustomAttributeBuilder>? assemblyAttributes = null)
46-
{
47-
ArgumentNullException.ThrowIfNull(name);
48-
ArgumentException.ThrowIfNullOrEmpty(name.Name, "AssemblyName.Name");
49-
ArgumentNullException.ThrowIfNull(coreAssembly);
50-
51-
Type assemblyType = Type.GetType("System.Reflection.Emit.AssemblyBuilderImpl, System.Reflection.Emit", throwOnError: true)!;
52-
ConstructorInfo con = assemblyType.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, [typeof(AssemblyName), typeof(Assembly), typeof(IEnumerable<CustomAttributeBuilder>)])!;
53-
return (AssemblyBuilder)con.Invoke([name, coreAssembly, assemblyAttributes]);
54-
}
55-
5636
protected abstract ModuleBuilder? GetDynamicModuleCore(string name);
5737

58-
/// <summary>
59-
/// Serializes the assembly to <see cref="Stream"/>.
60-
/// </summary>
61-
/// <param name="stream">The <see cref="Stream"/> to which the assembly serialized.</param>
62-
/// <exception cref="ArgumentNullException"><paramref name="stream"/> is null.</exception>
63-
/// <exception cref="NotSupportedException">The AssemblyBuilder instance doesn't support saving.</exception>
64-
public void Save(Stream stream) => SaveCore(stream);
65-
66-
/// <summary>
67-
/// Saves the assembly to disk.
68-
/// </summary>
69-
/// <param name="assemblyFileName">The file name of the assembly.</param>
70-
/// <exception cref="ArgumentNullException"><paramref name="assemblyFileName"/> is null.</exception>
71-
/// <exception cref="NotSupportedException">The AssemblyBuilder instance doesn't support saving.</exception>
72-
public void Save(string assemblyFileName)
73-
{
74-
ArgumentNullException.ThrowIfNull(assemblyFileName);
75-
76-
using var peStream = new FileStream(assemblyFileName, FileMode.Create, FileAccess.Write);
77-
SaveCore(peStream);
78-
}
79-
80-
/// <summary>
81-
/// When implemented in a derived type, serializes the assembly to a stream.
82-
/// </summary>
83-
/// <param name="stream">The stream to which the assembly serialized.</param>
84-
protected virtual void SaveCore(Stream stream) => throw new NotSupportedException(SR.NotSupported_AssemblySave);
85-
8638
public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
8739
{
8840
ArgumentNullException.ThrowIfNull(con);

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

+18-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ protected AssemblyBuilder() { }
2424
public static System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly(System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access) { throw null; }
2525
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")]
2626
public static System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly(System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, System.Collections.Generic.IEnumerable<System.Reflection.Emit.CustomAttributeBuilder>? assemblyAttributes) { throw null; }
27-
public static System.Reflection.Emit.AssemblyBuilder DefinePersistedAssembly(System.Reflection.AssemblyName name, System.Reflection.Assembly coreAssembly, System.Collections.Generic.IEnumerable<System.Reflection.Emit.CustomAttributeBuilder>? assemblyAttributes = null) { throw null; }
2827
public System.Reflection.Emit.ModuleBuilder DefineDynamicModule(string name) { throw null; }
2928
protected abstract System.Reflection.Emit.ModuleBuilder DefineDynamicModuleCore(string name);
3029
public override bool Equals(object? obj) { throw null; }
@@ -55,9 +54,6 @@ protected AssemblyBuilder() { }
5554
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed by trimming. If the type name is a string literal, consider using Type.GetType instead.")]
5655
public override System.Type? GetType(string name, bool throwOnError, bool ignoreCase) { throw null; }
5756
public override bool IsDefined(System.Type attributeType, bool inherit) { throw null; }
58-
public void Save(string assemblyFileName) { throw null; }
59-
public void Save(System.IO.Stream stream) { throw null; }
60-
protected virtual void SaveCore(System.IO.Stream stream) { }
6157
public void SetCustomAttribute(System.Reflection.ConstructorInfo con, byte[] binaryAttribute) { }
6258
public void SetCustomAttribute(System.Reflection.Emit.CustomAttributeBuilder customBuilder) { }
6359
protected abstract void SetCustomAttributeCore(System.Reflection.ConstructorInfo con, System.ReadOnlySpan<byte> binaryAttribute);
@@ -474,6 +470,24 @@ public void SetCustomAttribute(System.Reflection.ConstructorInfo con, byte[] bin
474470
public void SetCustomAttribute(System.Reflection.Emit.CustomAttributeBuilder customBuilder) { }
475471
protected abstract void SetCustomAttributeCore(System.Reflection.ConstructorInfo con, System.ReadOnlySpan<byte> binaryAttribute);
476472
}
473+
#if !BUILDING_CORELIB_REFERENCE
474+
public sealed class PersistedAssemblyBuilder : System.Reflection.Emit.AssemblyBuilder
475+
{
476+
public PersistedAssemblyBuilder(System.Reflection.AssemblyName name, System.Reflection.Assembly coreAssembly, System.Collections.Generic.IEnumerable<System.Reflection.Emit.CustomAttributeBuilder>? assemblyAttributes = null) { }
477+
public override string? FullName { get { throw null; } }
478+
public override bool IsDynamic { get { throw null; } }
479+
public override System.Reflection.Module ManifestModule { get { throw null; } }
480+
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")]
481+
protected override System.Reflection.Emit.ModuleBuilder DefineDynamicModuleCore(string name) { throw null; }
482+
protected override System.Reflection.Emit.ModuleBuilder? GetDynamicModuleCore(string name) { throw null; }
483+
[System.CLSCompliantAttribute(false)]
484+
public System.Reflection.Metadata.Ecma335.MetadataBuilder GenerateMetadata(out System.Reflection.Metadata.BlobBuilder ilStream, out System.Reflection.Metadata.BlobBuilder mappedFieldData) { throw null; }
485+
public override System.Reflection.AssemblyName GetName(bool copiedName) { throw null; }
486+
public void Save(string assemblyFileName) { throw null; }
487+
public void Save(System.IO.Stream stream) { throw null; }
488+
protected override void SetCustomAttributeCore(System.Reflection.ConstructorInfo con, System.ReadOnlySpan<byte> binaryAttribute) { throw null; }
489+
}
490+
#endif
477491
public abstract partial class PropertyBuilder : System.Reflection.PropertyInfo
478492
{
479493
protected PropertyBuilder() { }

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

+1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
<ProjectReference Include="..\..\System.Runtime.InteropServices\ref\System.Runtime.InteropServices.csproj" />
1111
<ProjectReference Include="..\..\System.Reflection.Primitives\ref\System.Reflection.Primitives.csproj" />
1212
<ProjectReference Include="..\..\System.Reflection.Emit.ILGeneration\ref\System.Reflection.Emit.ILGeneration.csproj" />
13+
<ProjectReference Include="..\..\System.Reflection.Metadata\ref\System.Reflection.Metadata.csproj" />
1314
</ItemGroup>
1415
</Project>

src/libraries/System.Reflection.Emit/src/ILLink/ILLink.Descriptors.LibraryBuild.xml

-8
This file was deleted.

src/libraries/System.Reflection.Emit/src/Resources/Strings.resx

+2-5
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@
117117
<resheader name="writer">
118118
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
119119
</resheader>
120-
<data name="InvalidOperation_CannotSaveMultipleTimes" xml:space="preserve">
121-
<value>Cannot save an assembly multiple times.</value>
120+
<data name="InvalidOperation_CannotPopulateMultipleTimes" xml:space="preserve">
121+
<value>Cannot populate assembly metadata multiple times.</value>
122122
</data>
123123
<data name="Argument_CannotSetParentToInterface" xml:space="preserve">
124124
<value>Cannot set parent to an interface.</value>
@@ -249,9 +249,6 @@
249249
<data name="Argument_MustBeInterface" xml:space="preserve">
250250
<value>Type passed must be an interface.</value>
251251
</data>
252-
<data name="TypeLoad_MissingMethod" xml:space="preserve">
253-
<value>Abstract method '{0}' in type '{1}' does not have an implementation.</value>
254-
</data>
255252
<data name="InvalidOperation_BadMethodBody" xml:space="preserve">
256253
<value>Method '{0}' cannot have a method body</value>
257254
</data>

src/libraries/System.Reflection.Emit/src/System.Reflection.Emit.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
<Compile Include="System\Reflection\Emit\ArrayMethod.cs" />
1515
<Compile Include="System\Reflection\Emit\ConstructorBuilderImpl.cs" />
1616
<Compile Include="System\Reflection\Emit\CustomAttributeWrapper.cs" />
17-
<Compile Include="System\Reflection\Emit\AssemblyBuilderImpl.cs" />
1817
<Compile Include="System\Reflection\Emit\EnumBuilderImpl.cs" />
1918
<Compile Include="System\Reflection\Emit\EventBuilderImpl.cs" />
2019
<Compile Include="System\Reflection\Emit\FieldBuilderImpl.cs" />
@@ -24,6 +23,7 @@
2423
<Compile Include="System\Reflection\Emit\MethodBuilderImpl.cs" />
2524
<Compile Include="System\Reflection\Emit\ModuleBuilderImpl.cs" />
2625
<Compile Include="System\Reflection\Emit\ParameterBuilderImpl.cs" />
26+
<Compile Include="System\Reflection\Emit\PersistedAssemblyBuilder.cs" />
2727
<Compile Include="System\Reflection\Emit\PropertyBuilderImpl.cs" />
2828
<Compile Include="System\Reflection\Emit\PseudoCustomAttributesData.cs" />
2929
<Compile Include="System\Reflection\Emit\SignatureHelper.cs" />

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ internal sealed class ModuleBuilderImpl : ModuleBuilder
1616
private readonly Assembly _coreAssembly;
1717
private readonly string _name;
1818
private readonly MetadataBuilder _metadataBuilder;
19-
private readonly AssemblyBuilderImpl _assemblyBuilder;
19+
private readonly PersistedAssemblyBuilder _assemblyBuilder;
2020
private readonly TypeBuilderImpl _globalTypeBuilder;
2121
private readonly Dictionary<Assembly, AssemblyReferenceHandle> _assemblyReferences = new();
2222
private readonly Dictionary<Type, EntityHandle> _typeReferences = new();
@@ -37,7 +37,7 @@ internal sealed class ModuleBuilderImpl : ModuleBuilder
3737
private static readonly Type[] s_coreTypes = { typeof(void), typeof(object), typeof(bool), typeof(char), typeof(sbyte), typeof(byte), typeof(short), typeof(ushort), typeof(int),
3838
typeof(uint), typeof(long), typeof(ulong), typeof(float), typeof(double), typeof(string), typeof(nint), typeof(nuint), typeof(TypedReference) };
3939

40-
internal ModuleBuilderImpl(string name, Assembly coreAssembly, MetadataBuilder builder, AssemblyBuilderImpl assemblyBuilder)
40+
internal ModuleBuilderImpl(string name, Assembly coreAssembly, MetadataBuilder builder, PersistedAssemblyBuilder assemblyBuilder)
4141
{
4242
_coreAssembly = coreAssembly;
4343
_name = name;

0 commit comments

Comments
 (0)