Skip to content

Commit a7ed5ce

Browse files
committed
Apply feedbacks
1 parent f43f7fc commit a7ed5ce

File tree

8 files changed

+40
-46
lines changed

8 files changed

+40
-46
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@
142142
<value>The invoked member is not supported in a dynamic module.</value>
143143
</data>
144144
<data name="ArgumentException_InvalidTypeArgument" xml:space="preserve">
145-
<value>The type code '{0}' may not be used as a type argument of a custom attribute .</value>
145+
<value>The type code may not be used as a type argument of a custom attribute .</value>
146146
</data>
147147
<data name="InvalidOperation_EmptyFieldForCustomAttribute" xml:space="preserve">
148148
<value>Custom attribute '{0}' doesn't contain a field named '{1}'.</value>

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

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Buffers.Binary;
45
using System.Diagnostics.CodeAnalysis;
6+
using System.Reflection.Metadata;
57

68
namespace System.Reflection.Emit
79
{
@@ -13,7 +15,7 @@ internal readonly struct CustomAttributeWrapper
1315
public CustomAttributeWrapper(ConstructorInfo constructorInfo, ReadOnlySpan<byte> binaryAttribute)
1416
{
1517
_constructorInfo = constructorInfo;
16-
_binaryAttribute = binaryAttribute.ToArray();
18+
_binaryAttribute = binaryAttribute.ToArray(); // TODO: Update to BlobHandle when public API public APi for MetadataBuilder.GetOrAddBlob(ReadOnlySpan<byte>) added
1719
}
1820

1921
public ConstructorInfo Ctor => _constructorInfo;
@@ -143,7 +145,7 @@ private static int DecodeLen(ReadOnlySpan<byte> data, int pos, out int rpos)
143145
return StringFromBytes(data, pos, len);
144146
case TypeCode.Int32:
145147
rpos = pos + 4;
146-
return data[pos] + (data[pos + 1] << 8) + (data[pos + 2] << 16) + (data[pos + 3] << 24);
148+
return BinaryPrimitives.ReadInt32LittleEndian(data.Slice(pos));
147149
case TypeCode.Boolean:
148150
rpos = pos + 1;
149151
return (data[pos] == 0) ? false : true;
@@ -153,32 +155,31 @@ private static int DecodeLen(ReadOnlySpan<byte> data, int pos, out int rpos)
153155

154156
if (subtype >= 0x02 && subtype <= 0x0e)
155157
{
156-
return DecodeCustomAttributeValue(ElementTypeToType(subtype), data, pos, out rpos);
158+
return DecodeCustomAttributeValue(ElementTypeToType((PrimitiveSerializationTypeCode)subtype), data, pos, out rpos);
157159
}
158160
break;
159161
}
160162

161163
throw new NotImplementedException(SR.Format(SR.NotImplemented_TypeForValue, t));
162164
}
163165

164-
private static Type ElementTypeToType(int elementType) =>
165-
/* Partition II, section 23.1.16 */
166-
elementType switch
167-
{
168-
0x02 => typeof(bool),
169-
0x03 => typeof(char),
170-
0x04 => typeof(sbyte),
171-
0x05 => typeof(byte),
172-
0x06 => typeof(short),
173-
0x07 => typeof(ushort),
174-
0x08 => typeof(int),
175-
0x09 => typeof(uint),
176-
0x0a => typeof(long),
177-
0x0b => typeof(ulong),
178-
0x0c => typeof(float),
179-
0x0d => typeof(double),
180-
0x0e => typeof(string),
181-
_ => throw new ArgumentException(SR.Format(SR.ArgumentException_InvalidTypeArgument, elementType)),
182-
};
166+
private static Type ElementTypeToType(PrimitiveSerializationTypeCode elementType) =>
167+
elementType switch
168+
{
169+
PrimitiveSerializationTypeCode.Boolean => typeof(bool),
170+
PrimitiveSerializationTypeCode.Char => typeof(char),
171+
PrimitiveSerializationTypeCode.SByte => typeof(sbyte),
172+
PrimitiveSerializationTypeCode.Byte => typeof(byte),
173+
PrimitiveSerializationTypeCode.Int16 => typeof(short),
174+
PrimitiveSerializationTypeCode.UInt16 => typeof(ushort),
175+
PrimitiveSerializationTypeCode.Int32 => typeof(int),
176+
PrimitiveSerializationTypeCode.UInt32 => typeof(uint),
177+
PrimitiveSerializationTypeCode.Int64 => typeof(long),
178+
PrimitiveSerializationTypeCode.UInt64 => typeof(ulong),
179+
PrimitiveSerializationTypeCode.Single => typeof(float),
180+
PrimitiveSerializationTypeCode.Double => typeof(double),
181+
PrimitiveSerializationTypeCode.String => typeof(string),
182+
_ => throw new ArgumentException(SR.ArgumentException_InvalidTypeArgument),
183+
};
183184
}
184185
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Buffers.Binary;
45
using System.Collections.Generic;
56
using System.Diagnostics;
67
using System.Globalization;
@@ -42,10 +43,7 @@ private bool IsPseudoAttribute(string attributeName, ReadOnlySpan<byte> binaryAt
4243
{
4344
case "System.Runtime.InteropServices.FieldOffsetAttribute":
4445
Debug.Assert(binaryAttribute.Length >= 6);
45-
_offset = (int)binaryAttribute[2];
46-
_offset |= ((int)binaryAttribute[3]) << 8;
47-
_offset |= ((int)binaryAttribute[4]) << 16;
48-
_offset |= ((int)binaryAttribute[5]) << 24;
46+
_offset = BinaryPrimitives.ReadInt32LittleEndian(binaryAttribute.Slice(2));
4947
break;
5048
case "System.NonSerializedAttribute":
5149
#pragma warning disable SYSLIB0050 // 'FieldAttributes.NotSerialized' is obsolete: 'Formatter-based serialization is obsolete and should not be used'.

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Buffers.Binary;
45
using System.Collections.Generic;
56
using System.Diagnostics.CodeAnalysis;
67
using System.Globalization;
@@ -66,9 +67,8 @@ private bool IsPseudoCustomAttribute(string attributeName, ConstructorInfo con,
6667
switch (attributeName)
6768
{
6869
case "System.Runtime.CompilerServices.MethodImplAttribute":
69-
int impla = data[2];
70-
impla |= data[3] << 8;
71-
_methodImplFlags |= (MethodImplAttributes)impla;
70+
int implValue = BinaryPrimitives.ReadUInt16LittleEndian(data.Slice(2));
71+
_methodImplFlags |= (MethodImplAttributes)implValue;
7272
break;
7373
case "System.Runtime.InteropServices.DllImportAttribute":
7474
{

src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeConstructorBuilder.Mono.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
using System.Globalization;
3838
using System.Diagnostics.CodeAnalysis;
3939
using System.Runtime.InteropServices;
40+
using System.Buffers.Binary;
4041

4142
namespace System.Reflection.Emit
4243
{
@@ -259,9 +260,7 @@ protected override void SetCustomAttributeCore(ConstructorInfo con, ReadOnlySpan
259260
string? attrname = con.ReflectedType!.FullName;
260261
if (attrname == "System.Runtime.CompilerServices.MethodImplAttribute")
261262
{
262-
int impla; // the (stupid) ctor takes a short or an int ...
263-
impla = (int)binaryAttribute[2];
264-
impla |= ((int)binaryAttribute[3]) << 8;
263+
int impla = BinaryPrimitives.ReadUInt16LittleEndian(binaryAttribute.Slice(2));
265264
SetImplementationFlags((MethodImplAttributes)impla);
266265
return;
267266
}

src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeFieldBuilder.Mono.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
using System.Globalization;
3838
using System.Runtime.InteropServices;
3939
using System.Diagnostics.CodeAnalysis;
40+
using System.Buffers.Binary;
4041

4142
namespace System.Reflection.Emit
4243
{
@@ -181,10 +182,7 @@ protected override void SetCustomAttributeCore(ConstructorInfo con, ReadOnlySpan
181182
string? attrname = con.ReflectedType!.FullName;
182183
if (attrname == "System.Runtime.InteropServices.FieldOffsetAttribute")
183184
{
184-
offset = (int)binaryAttribute[2];
185-
offset |= ((int)binaryAttribute[3]) << 8;
186-
offset |= ((int)binaryAttribute[4]) << 16;
187-
offset |= ((int)binaryAttribute[5]) << 24;
185+
offset = BinaryPrimitives.ReadInt32LittleEndian(binaryAttribute.Slice(2));
188186
return;
189187
}
190188
#pragma warning disable SYSLIB0050 // FieldAttributes.NotSerialized is obsolete

src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeMethodBuilder.Mono.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
//
3535

3636
#if MONO_FEATURE_SRE
37+
using System.Buffers.Binary;
3738
using System.Diagnostics.CodeAnalysis;
3839
using System.Globalization;
3940
using System.Runtime.InteropServices;
@@ -386,9 +387,7 @@ protected override void SetCustomAttributeCore(ConstructorInfo con, ReadOnlySpan
386387
switch (con.ReflectedType!.FullName)
387388
{
388389
case "System.Runtime.CompilerServices.MethodImplAttribute":
389-
int impla; // the (stupid) ctor takes a short or an int ...
390-
impla = (int)binaryAttribute[2];
391-
impla |= ((int)binaryAttribute[3]) << 8;
390+
int impla = BinaryPrimitives.ReadUInt16LittleEndian(binaryAttribute.Slice(2));
392391
iattrs |= (MethodImplAttributes)impla;
393392
return;
394393

src/mono/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeTypeBuilder.Mono.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3535
//
3636

37+
using System.Buffers.Binary;
3738
using System.Collections.Generic;
3839
using System.Diagnostics;
3940
using System.Diagnostics.CodeAnalysis;
@@ -1437,8 +1438,8 @@ protected override void SetCustomAttributeCore(ConstructorInfo con, ReadOnlySpan
14371438
int pos = 6;
14381439
if (ctor_type.FullName == "System.Int16")
14391440
pos = 4;
1440-
int nnamed = (int)binaryAttribute[pos++];
1441-
nnamed |= ((int)binaryAttribute[pos++]) << 8;
1441+
int nnamed = BinaryPrimitives.ReadUInt16LittleEndian(binaryAttribute.Slice(pos++));
1442+
pos++;
14421443
for (int i = 0; i < nnamed; ++i)
14431444
{
14441445
//byte named_type = data [pos++];
@@ -1461,10 +1462,8 @@ protected override void SetCustomAttributeCore(ConstructorInfo con, ReadOnlySpan
14611462
named_name = CustomAttributeBuilder.string_from_bytes(binaryAttribute, pos, len);
14621463
pos += len;
14631464
/* all the fields are integers in StructLayout */
1464-
int value = (int)binaryAttribute[pos++];
1465-
value |= ((int)binaryAttribute[pos++]) << 8;
1466-
value |= ((int)binaryAttribute[pos++]) << 16;
1467-
value |= ((int)binaryAttribute[pos++]) << 24;
1465+
int value = BinaryPrimitives.ReadInt32LittleEndian(binaryAttribute.Slice(pos++));
1466+
pos += 3;
14681467
switch (named_name)
14691468
{
14701469
case "CharSet":

0 commit comments

Comments
 (0)