Skip to content

Commit dd95ed8

Browse files
buyaa-njkotas
andauthored
Abstract out ParameterBuilder and implement it (#85446)
* Save parameters into assembly and test it * Add MarshalAs pseduo attribute handling and other refactoring * Apply suggestions from code review Co-authored-by: Jan Kotas <[email protected]> --------- Co-authored-by: Jan Kotas <[email protected]>
1 parent da75e6d commit dd95ed8

File tree

23 files changed

+804
-662
lines changed

23 files changed

+804
-662
lines changed

src/coreclr/System.Private.CoreLib/System.Private.CoreLib.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@
163163
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\DynamicMethod.CoreCLR.cs" />
164164
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\ILGenerator.cs" />
165165
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\LocalBuilder.cs" />
166-
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\ParameterBuilder.cs" />
167166
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\RuntimeAssemblyBuilder.cs" />
168167
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\RuntimeConstructorBuilder.cs" />
169168
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\RuntimeEnumBuilder.cs" />
@@ -172,6 +171,7 @@
172171
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\RuntimeGenericTypeParameterBuilder.cs" />
173172
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\RuntimeMethodBuilder.cs" />
174173
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\RuntimeModuleBuilder.cs" />
174+
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\RuntimeParameterBuilder.cs" />
175175
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\RuntimePropertyBuilder.cs" />
176176
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\RuntimeTypeBuilder.cs" />
177177
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\SignatureHelper.cs" />

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ protected override ParameterBuilder DefineParameterCore(int position, ParameterA
646646
throw new ArgumentOutOfRangeException(SR.ArgumentOutOfRange_ParamSequence);
647647

648648
attributes &= ~ParameterAttributes.ReservedMask;
649-
return new ParameterBuilder(this, position, attributes, strParamName);
649+
return new RuntimeParameterBuilder(this, position, attributes, strParamName);
650650
}
651651

652652
protected override void SetImplementationFlagsCore(MethodImplAttributes attributes)

src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/ParameterBuilder.cs src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeParameterBuilder.cs

+7-25
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
namespace System.Reflection.Emit
77
{
8-
public class ParameterBuilder
8+
internal sealed class RuntimeParameterBuilder : ParameterBuilder
99
{
1010
// Set the default value of the parameter
11-
public virtual void SetConstant(object? defaultValue)
11+
public override void SetConstant(object? defaultValue)
1212
{
1313
RuntimeTypeBuilder.SetConstantValue(
1414
_methodBuilder.GetModuleBuilder(),
@@ -17,28 +17,16 @@ public virtual void SetConstant(object? defaultValue)
1717
defaultValue);
1818
}
1919

20-
// Use this function if client decides to form the custom attribute blob themselves
21-
public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
20+
protected override void SetCustomAttributeCore(ConstructorInfo con, ReadOnlySpan<byte> binaryAttribute)
2221
{
23-
ArgumentNullException.ThrowIfNull(con);
24-
ArgumentNullException.ThrowIfNull(binaryAttribute);
25-
2622
RuntimeTypeBuilder.DefineCustomAttribute(
2723
_methodBuilder.GetModuleBuilder(),
2824
_token,
2925
((RuntimeModuleBuilder)_methodBuilder.GetModule()).GetMethodMetadataToken(con),
3026
binaryAttribute);
3127
}
3228

33-
// Use this function if client wishes to build CustomAttribute using CustomAttributeBuilder
34-
public void SetCustomAttribute(CustomAttributeBuilder customBuilder)
35-
{
36-
ArgumentNullException.ThrowIfNull(customBuilder);
37-
38-
customBuilder.CreateCustomAttribute((RuntimeModuleBuilder)(_methodBuilder.GetModule()), _token);
39-
}
40-
41-
internal ParameterBuilder(
29+
internal RuntimeParameterBuilder(
4230
RuntimeMethodBuilder methodBuilder,
4331
int sequence,
4432
ParameterAttributes attributes,
@@ -62,17 +50,11 @@ internal int GetToken()
6250
return _token;
6351
}
6452

65-
public virtual string? Name => _name;
66-
67-
public virtual int Position => _position;
68-
69-
public virtual int Attributes => (int)_attributes;
70-
71-
public bool IsIn => (_attributes & ParameterAttributes.In) != 0;
53+
public override string? Name => _name;
7254

73-
public bool IsOut => (_attributes & ParameterAttributes.Out) != 0;
55+
public override int Position => _position;
7456

75-
public bool IsOptional => (_attributes & ParameterAttributes.Optional) != 0;
57+
public override int Attributes => (int)_attributes;
7658

7759
private readonly string? _name;
7860
private readonly int _position;

src/coreclr/nativeaot/System.Private.CoreLib/src/System.Private.CoreLib.csproj

-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@
145145
<Compile Include="System\Reflection\Emit\DynamicMethod.cs" />
146146
<Compile Include="System\Reflection\Emit\ILGenerator.cs" />
147147
<Compile Include="System\Reflection\Emit\LocalBuilder.cs" />
148-
<Compile Include="System\Reflection\Emit\ParameterBuilder.cs" />
149148
<Compile Include="System\Reflection\Emit\ReflectionEmitThrower.cs" />
150149
<Compile Include="System\Reflection\Emit\SignatureHelper.cs" />
151150
<Compile Include="System\Reflection\EnumInfo.cs" />

src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Emit/ParameterBuilder.cs

-73
This file was deleted.

src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems

+1
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,7 @@
646646
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\OpCodeType.cs" />
647647
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\OperandType.cs" />
648648
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\PackingSize.cs" />
649+
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\ParameterBuilder.cs" />
649650
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\PEFileKinds.cs" />
650651
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\PropertyBuilder.cs" />
651652
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\StackBehaviour.cs" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Text;
7+
8+
namespace System.Reflection.Emit
9+
{
10+
public abstract partial class ParameterBuilder
11+
{
12+
protected ParameterBuilder() { }
13+
public virtual int Attributes => throw new NotImplementedException();
14+
public bool IsIn => ((ParameterAttributes)Attributes & ParameterAttributes.In) != 0;
15+
public bool IsOptional => ((ParameterAttributes)Attributes & ParameterAttributes.Optional) != 0;
16+
public bool IsOut => ((ParameterAttributes)Attributes & ParameterAttributes.Out) != 0;
17+
public virtual string? Name => throw new NotImplementedException();
18+
public virtual int Position => throw new NotImplementedException();
19+
public virtual void SetConstant(object? defaultValue) => throw new NotImplementedException();
20+
public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
21+
{
22+
ArgumentNullException.ThrowIfNull(con);
23+
ArgumentNullException.ThrowIfNull(binaryAttribute);
24+
25+
SetCustomAttributeCore(con, binaryAttribute);
26+
}
27+
protected abstract void SetCustomAttributeCore(ConstructorInfo con, ReadOnlySpan<byte> binaryAttribute);
28+
public void SetCustomAttribute(CustomAttributeBuilder customBuilder)
29+
{
30+
ArgumentNullException.ThrowIfNull(customBuilder);
31+
32+
SetCustomAttributeCore(customBuilder.Ctor, customBuilder.Data);
33+
}
34+
}
35+
}

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ internal LocalBuilder() { }
7272
public override int LocalIndex { get { throw null; } }
7373
public override System.Type LocalType { get { throw null; } }
7474
}
75-
public partial class ParameterBuilder
75+
public abstract partial class ParameterBuilder
7676
{
77-
internal ParameterBuilder() { }
77+
protected ParameterBuilder() { }
7878
public virtual int Attributes { get { throw null; } }
7979
public bool IsIn { get { throw null; } }
8080
public bool IsOptional { get { throw null; } }
@@ -84,6 +84,7 @@ internal ParameterBuilder() { }
8484
public virtual void SetConstant(object? defaultValue) { }
8585
public void SetCustomAttribute(System.Reflection.ConstructorInfo con, byte[] binaryAttribute) { }
8686
public void SetCustomAttribute(System.Reflection.Emit.CustomAttributeBuilder customBuilder) { }
87+
protected abstract void SetCustomAttributeCore(System.Reflection.ConstructorInfo con, System.ReadOnlySpan<byte> binaryAttribute);
8788
}
8889
public sealed partial class SignatureHelper
8990
{

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

+15-6
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,6 @@
126126
<data name="InvalidOperation_BadInterfaceNotAbstract" xml:space="preserve">
127127
<value>Interface must be declared abstract.</value>
128128
</data>
129-
<data name="NotSupported_Signature" xml:space="preserve">
130-
<value>The signature {0} is not supported.</value>
131-
</data>
132129
<data name="InvalidOperation_AModuleRequired" xml:space="preserve">
133130
<value>Assembly needs at least one module defined.</value>
134131
</data>
@@ -141,11 +138,11 @@
141138
<data name="NotSupported_DynamicModule" xml:space="preserve">
142139
<value>The invoked member is not supported in a dynamic module.</value>
143140
</data>
144-
<data name="Argument_InvalidTypeArgument" xml:space="preserve">
141+
<data name="Argument_InvalidTypeCodeForTypeArgument" xml:space="preserve">
145142
<value>The type code may not be used as a type argument of a custom attribute .</value>
146143
</data>
147-
<data name="InvalidOperation_EmptyFieldForCustomAttribute" xml:space="preserve">
148-
<value>Custom attribute '{0}' doesn't contain a field named '{1}'.</value>
144+
<data name="NotSupported_UnmanagedTypeOnlyForFields" xml:space="preserve">
145+
<value> 'UnmanagedType.{0}' named parameter is only valid for fields.</value>
149146
</data>
150147
<data name="Argument_InvalidCustomAttributeLength" xml:space="preserve">
151148
<value>Custom attribute '{0}' data length is only '{1}'.</value>
@@ -162,4 +159,16 @@
162159
<data name="Argument_DllNameCannotBeEmpty" xml:space="preserve">
163160
<value>DllName cannot be empty.</value>
164161
</data>
162+
<data name="ArgumentOutOfRange_ParamSequence" xml:space="preserve">
163+
<value>The specified parameter index is not in range.</value>
164+
</data>
165+
<data name="Argument_InvalidArgumentForAttribute" xml:space="preserve">
166+
<value>Invalid constructor argument {0} provided for MarshalAs atttribute.</value>
167+
</data>
168+
<data name="Argument_InvalidParameterForUnmanagedType" xml:space="preserve">
169+
<value>Named parameter {0} is not valid for UnmanagedType.{1} type.</value>
170+
</data>
171+
<data name="Argument_SizeConstMustBeSpecified" xml:space="preserve">
172+
<value>SizeConst parameter must be specified for UnmanagedType.ByValTStr type.</value>
173+
</data>
165174
</root>

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

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
<Compile Include="System\Reflection\Emit\FieldBuilderImpl.cs" />
1111
<Compile Include="System\Reflection\Emit\MethodBuilderImpl.cs" />
1212
<Compile Include="System\Reflection\Emit\ModuleBuilderImpl.cs" />
13+
<Compile Include="System\Reflection\Emit\ParameterBuilderImpl.cs" />
14+
<Compile Include="System\Reflection\Emit\PseudoCustomAttributesData.cs" />
1315
<Compile Include="System\Reflection\Emit\TypeBuilderImpl.cs" />
1416
<Compile Include="System\Reflection\Emit\SignatureHelper.cs" />
1517
</ItemGroup>

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Buffers.Binary;
5-
using System.Diagnostics.CodeAnalysis;
5+
using System.Diagnostics;
66
using System.Reflection.Metadata;
7+
using System.Reflection.Metadata.Ecma335;
8+
using System.Runtime.InteropServices;
79

810
namespace System.Reflection.Emit
911
{
@@ -171,7 +173,7 @@ private static Type ElementTypeToType(PrimitiveSerializationTypeCode elementType
171173
PrimitiveSerializationTypeCode.Single => typeof(float),
172174
PrimitiveSerializationTypeCode.Double => typeof(double),
173175
PrimitiveSerializationTypeCode.String => typeof(string),
174-
_ => throw new ArgumentException(SR.Argument_InvalidTypeArgument, "binaryAttribute"),
176+
_ => throw new ArgumentException(SR.Argument_InvalidTypeCodeForTypeArgument, "binaryAttribute"),
175177
};
176178
}
177179
}

0 commit comments

Comments
 (0)