Skip to content

Commit 6eb9a3e

Browse files
committed
Apply feedbacks
1 parent e8414ba commit 6eb9a3e

File tree

7 files changed

+512
-501
lines changed

7 files changed

+512
-501
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<Compile Include="System\Reflection\Emit\MethodBuilderImpl.cs" />
1212
<Compile Include="System\Reflection\Emit\ModuleBuilderImpl.cs" />
1313
<Compile Include="System\Reflection\Emit\ParameterBuilderImpl.cs" />
14+
<Compile Include="System\Reflection\Emit\PseudoCustomAttributesData.cs" />
1415
<Compile Include="System\Reflection\Emit\TypeBuilderImpl.cs" />
1516
<Compile Include="System\Reflection\Emit\SignatureHelper.cs" />
1617
</ItemGroup>

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

Lines changed: 0 additions & 391 deletions
Large diffs are not rendered by default.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ internal sealed class FieldBuilderImpl : FieldBuilder
1919
private readonly Type _fieldType;
2020
private FieldAttributes _attributes;
2121

22-
internal MarshallingInfo? _marshallingInfo;
22+
internal MarshallingData? _marshallingData;
2323
internal int _offset;
2424
internal List<CustomAttributeWrapper>? _customAttributes;
2525

@@ -52,7 +52,7 @@ protected override void SetCustomAttributeCore(ConstructorInfo con, ReadOnlySpan
5252
return;
5353
case "System.Runtime.InteropServices.MarshalAsAttribute":
5454
_attributes |= FieldAttributes.HasFieldMarshal;
55-
_marshallingInfo = MarshallingInfo.ParseMarshallingInfo(con, binaryAttribute, isField : true);
55+
_marshallingData = MarshallingData.CreateMarshallingData(con, binaryAttribute, isField : true);
5656
return;
5757
}
5858

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

Lines changed: 5 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ protected override ParameterBuilder DefineParameterCore(int position, ParameterA
5959
if (position > 0 && (_parameterTypes == null || position > _parameterTypes.Length))
6060
throw new ArgumentOutOfRangeException(SR.ArgumentOutOfRange_ParamSequence);
6161

62+
if (position == 0 && _parameters == null)
63+
{
64+
_parameters = new ParameterBuilderImpl[1];
65+
}
66+
6267
Debug.Assert(_parameters != null);
6368
attributes &= ~ParameterAttributes.ReservedMask;
6469
ParameterBuilderImpl parameter = new ParameterBuilderImpl(this, position, attributes, strParamName);
@@ -155,103 +160,4 @@ public override object Invoke(object? obj, BindingFlags invokeAttr, Binder? bind
155160
public override MethodInfo MakeGenericMethod(params System.Type[] typeArguments)
156161
=> throw new NotImplementedException();
157162
}
158-
159-
internal sealed class DllImportData
160-
{
161-
private readonly string _moduleName;
162-
private readonly string? _entryPoint;
163-
private readonly MethodImportAttributes _flags;
164-
internal DllImportData(string moduleName, string? entryPoint, MethodImportAttributes flags)
165-
{
166-
_moduleName = moduleName;
167-
_entryPoint = entryPoint;
168-
_flags = flags;
169-
}
170-
171-
public string ModuleName => _moduleName;
172-
173-
public string? EntryPoint => _entryPoint;
174-
175-
public MethodImportAttributes Flags => _flags;
176-
177-
internal static DllImportData CreateDllImportData(CustomAttributeInfo attr, out bool preserveSig)
178-
{
179-
string? moduleName = (string?)attr._ctorArgs[0];
180-
if (moduleName == null || moduleName.Length == 0)
181-
{
182-
throw new ArgumentException(SR.Argument_DllNameCannotBeEmpty);
183-
}
184-
185-
MethodImportAttributes importAttributes = MethodImportAttributes.None;
186-
string? entryPoint = null;
187-
preserveSig = true;
188-
for (int i = 0; i < attr._namedParamNames.Length; ++i)
189-
{
190-
string name = attr._namedParamNames[i];
191-
object value = attr._namedParamValues[i]!;
192-
switch (name)
193-
{
194-
case "PreserveSig":
195-
preserveSig = (bool)value;
196-
break;
197-
case "CallingConvention":
198-
importAttributes |= (CallingConvention)value switch
199-
{
200-
CallingConvention.Cdecl => MethodImportAttributes.CallingConventionCDecl,
201-
CallingConvention.FastCall => MethodImportAttributes.CallingConventionFastCall,
202-
CallingConvention.StdCall => MethodImportAttributes.CallingConventionStdCall,
203-
CallingConvention.ThisCall => MethodImportAttributes.CallingConventionThisCall,
204-
_=> MethodImportAttributes.CallingConventionWinApi // Roslyn defaults with this
205-
};
206-
break;
207-
case "CharSet":
208-
importAttributes |= (CharSet)value switch
209-
{
210-
CharSet.Ansi => MethodImportAttributes.CharSetAnsi,
211-
CharSet.Auto => MethodImportAttributes.CharSetAuto,
212-
CharSet.Unicode => MethodImportAttributes.CharSetUnicode,
213-
_ => MethodImportAttributes.CharSetAuto
214-
};
215-
break;
216-
case "EntryPoint":
217-
entryPoint = (string?)value;
218-
break;
219-
case "ExactSpelling":
220-
if ((bool)value)
221-
{
222-
importAttributes |= MethodImportAttributes.ExactSpelling;
223-
}
224-
break;
225-
case "SetLastError":
226-
if ((bool)value)
227-
{
228-
importAttributes |= MethodImportAttributes.SetLastError;
229-
}
230-
break;
231-
case "BestFitMapping":
232-
if ((bool)value)
233-
{
234-
importAttributes |= MethodImportAttributes.BestFitMappingEnable;
235-
}
236-
else
237-
{
238-
importAttributes |= MethodImportAttributes.BestFitMappingDisable;
239-
}
240-
break;
241-
case "ThrowOnUnmappableChar":
242-
if ((bool)value)
243-
{
244-
importAttributes |= MethodImportAttributes.ThrowOnUnmappableCharEnable;
245-
}
246-
else
247-
{
248-
importAttributes |= MethodImportAttributes.ThrowOnUnmappableCharDisable;
249-
}
250-
break;
251-
}
252-
}
253-
254-
return new DllImportData(moduleName, entryPoint, importAttributes);
255-
}
256-
}
257163
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,9 @@ internal void AppendMetadata()
148148
WriteCustomAttributes(parameter._customAttributes, parameterHandle);
149149
_nextParameterRowId++;
150150

151-
if (parameter._marshallingInfo != null)
151+
if (parameter._marshallingData != null)
152152
{
153-
AddMarshalling(parameterHandle, parameter._marshallingInfo.PopulateMarshallingBlob(_metadataBuilder));
153+
AddMarshalling(parameterHandle, parameter._marshallingData.SerializeMarshallingData());
154154
}
155155

156156
if (parameter._defaultValue != DBNull.Value)
@@ -179,9 +179,9 @@ internal void AppendMetadata()
179179
AddFieldLayout(fieldHandle, field._offset);
180180
}
181181

182-
if (field._marshallingInfo != null)
182+
if (field._marshallingData != null)
183183
{
184-
AddMarshalling(fieldHandle, field._marshallingInfo.PopulateMarshallingBlob(_metadataBuilder));
184+
AddMarshalling(fieldHandle, field._marshallingData.SerializeMarshallingData());
185185
}
186186
}
187187
}
@@ -303,8 +303,8 @@ private ModuleReferenceHandle AddModuleReference(string moduleName) =>
303303
private void AddFieldLayout(FieldDefinitionHandle fieldHandle, int offset) =>
304304
_metadataBuilder.AddFieldLayout(field: fieldHandle, offset: offset);
305305

306-
private void AddMarshalling(EntityHandle fieldHandle, BlobHandle descriptor) =>
307-
_metadataBuilder.AddMarshallingDescriptor(fieldHandle, descriptor);
306+
private void AddMarshalling(EntityHandle fieldHandle, BlobBuilder builder) =>
307+
_metadataBuilder.AddMarshallingDescriptor(fieldHandle, _metadataBuilder.GetOrAddBlob(builder));
308308

309309
private ParameterHandle AddParameter(ParameterBuilderImpl parameter) =>
310310
_metadataBuilder.AddParameter(

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ internal sealed class ParameterBuilderImpl : ParameterBuilder
1212
private readonly MethodBuilderImpl _methodBuilder;
1313
private ParameterAttributes _attributes;
1414

15-
internal List<CustomAttributeWrapper>? _customAttributes = new();
16-
internal MarshallingInfo? _marshallingInfo;
15+
internal List<CustomAttributeWrapper>? _customAttributes;
16+
internal MarshallingData? _marshallingData;
1717
internal object? _defaultValue = DBNull.Value;
1818

1919
public ParameterBuilderImpl(MethodBuilderImpl methodBuilder, int sequence, ParameterAttributes attributes, string? paramName)
@@ -47,7 +47,7 @@ protected override void SetCustomAttributeCore(ConstructorInfo con, ReadOnlySpan
4747
return;
4848
case "System.Runtime.InteropServices.MarshalAsAttribute":
4949
_attributes |= ParameterAttributes.HasFieldMarshal;
50-
_marshallingInfo = MarshallingInfo.ParseMarshallingInfo(con, binaryAttribute, isField: false);
50+
_marshallingData = MarshallingData.CreateMarshallingData(con, binaryAttribute, isField: false);
5151
return;
5252
case "System.Runtime.InteropServices.DefaultParameterValueAttribute":
5353
// MS.NET doesn't handle this attribute but we handle it for consistency TODO: not sure if we need to handle this

0 commit comments

Comments
 (0)