Skip to content

Commit

Permalink
Apply feedbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
buyaa-n committed Dec 8, 2023
1 parent 99a8c90 commit 0d23a23
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Buffers.Binary;
using System.Diagnostics;
using System.Reflection.Metadata;
using System.Reflection.Metadata.Ecma335;
using System.Runtime.InteropServices;

namespace System.Reflection.Emit
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ internal void AppendMetadata(MethodBodyStreamEncoder methodBodyEncoder)
_metadataBuilder.AddNestedType(typeHandle, (TypeDefinitionHandle)GetTypeHandle(typeBuilder.DeclaringType));
}

AddInterfaceImplementations(typeBuilder, typeHandle);
WriteInterfaceImplementations(typeBuilder, typeHandle);
WriteCustomAttributes(typeBuilder._customAttributes, typeHandle);
WriteProperties(typeBuilder);
WriteFields(typeBuilder);
Expand All @@ -183,7 +183,7 @@ internal void AppendMetadata(MethodBodyStreamEncoder methodBodyEncoder)
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2072:DynamicallyAccessedMembers", Justification = "Members are retrieved from internal cache")]
private void AddInterfaceImplementations(TypeBuilderImpl typeBuilder, TypeDefinitionHandle typeHandle)
private void WriteInterfaceImplementations(TypeBuilderImpl typeBuilder, TypeDefinitionHandle typeHandle)
{
if (typeBuilder._interfaces != null)
{
Expand Down Expand Up @@ -585,7 +585,7 @@ private MethodDefinitionHandle AddMethodDefinition(MethodBuilderImpl method, Blo
private TypeReferenceHandle AddTypeReference(Type type, EntityHandle resolutionScope) =>
_metadataBuilder.AddTypeReference(
resolutionScope: resolutionScope,
@namespace: (type.Namespace == null || type.IsNested) ? default : _metadataBuilder.GetOrAddString(type.Namespace),
@namespace: (type.Namespace == null) ? default : _metadataBuilder.GetOrAddString(type.Namespace),
name: _metadataBuilder.GetOrAddString(type.Name));

private MemberReferenceHandle AddMemberReference(string memberName, EntityHandle parent, BlobBuilder signature) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Reflection.Metadata;
using System.Runtime.InteropServices;

Expand Down Expand Up @@ -114,7 +113,7 @@ internal sealed class MarshallingData
private int _marshalArrayElementType; // safe array: VarEnum; array: UnmanagedType
private int _marshalArrayElementCount; // number of elements in an array, length of a string, or Unspecified
private int _marshalParameterIndex; // index of parameter that specifies array size (short) or IID (int), or Unspecified
private object? _marshalTypeNameOrSymbol; // custom marshaller: string or Type; safe array: element type
private string? _marshalTypeName; // custom marshaller: string or type name; safe array: element type name
private string? _marshalCookie;

internal const int Invalid = -1;
Expand All @@ -140,17 +139,14 @@ internal BlobBuilder SerializeMarshallingData()
case UnmanagedType.CustomMarshaler:
writer.WriteUInt16(0); // padding

switch (_marshalTypeNameOrSymbol)
if (_marshalTypeName != null)
{
case Type type:
writer.WriteSerializedString(type.FullName); // or AssemblyQualifiedName?
break;
case null:
writer.WriteByte(0);
break;
default:
writer.WriteSerializedString((string)_marshalTypeNameOrSymbol);
break;
writer.WriteSerializedString(_marshalTypeName);
}
else
{
writer.WriteByte(0);

}

if (_marshalCookie != null)
Expand Down Expand Up @@ -187,9 +183,9 @@ internal BlobBuilder SerializeMarshallingData()
{
writer.WriteCompressedInteger((int)safeArrayElementSubtype);

if (_marshalTypeNameOrSymbol is Type elementType)
if (_marshalTypeName is string)
{
writer.WriteSerializedString(elementType.FullName);
writer.WriteSerializedString(_marshalTypeName);
}
}
break;
Expand All @@ -210,10 +206,10 @@ internal BlobBuilder SerializeMarshallingData()
return writer;
}

internal void SetMarshalAsCustom(object typeSymbolOrName, string? cookie)
internal void SetMarshalAsCustom(string? name, string? cookie)
{
_marshalType = UnmanagedType.CustomMarshaler;
_marshalTypeNameOrSymbol = typeSymbolOrName;
_marshalTypeName = name;
_marshalCookie = cookie;
}

Expand Down Expand Up @@ -246,13 +242,13 @@ internal void SetMarshalAsFixedArray(UnmanagedType? elementType, int? elementCou
_marshalArrayElementCount = elementCount ?? Invalid;
}

internal void SetMarshalAsSafeArray(VarEnum? elementType, Type? type)
internal void SetMarshalAsSafeArray(VarEnum? elementType, string? type)
{
Debug.Assert(elementType == null || elementType >= 0 && (int)elementType <= MaxMarshalInteger);

_marshalType = UnmanagedType.SafeArray;
_marshalArrayElementType = (int)(elementType ?? InvalidVariantType);
_marshalTypeNameOrSymbol = type;
_marshalTypeName = type;
}

internal void SetMarshalAsFixedString(int elementCount)
Expand Down Expand Up @@ -367,7 +363,7 @@ private static void DecodeMarshalAsFixedString(string[] paramNames, object?[] va
private static void DecodeMarshalAsSafeArray(string[] paramNames, object?[] values, MarshallingData info)
{
VarEnum? elementTypeVariant = null;
Type? elementType = null;
string? elementType = null;
int symbolIndex = -1;

for (int i = 0; i < paramNames.Length; i++)
Expand All @@ -378,7 +374,7 @@ private static void DecodeMarshalAsSafeArray(string[] paramNames, object?[] valu
elementTypeVariant = (VarEnum)values[i]!;
break;
case "SafeArrayUserDefinedSubType":
elementType = (Type?)values[i];
elementType = (string?)values[i];
symbolIndex = i;
break;
case "ArraySubType":
Expand Down Expand Up @@ -468,21 +464,17 @@ private static void DecodeMarshalAsComInterface(string[] paramNames, object?[] v
info.SetMarshalAsComInterface(unmanagedType, parameterIndex);
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2057:RequiresUnreferencedCode", Justification = "The user should keep the type safe")]
private static void DecodeMarshalAsCustom(string[] paramNames, object?[] values, MarshallingData info)
{
string? cookie = null;
Type? type = null;
string? name = null;
for (int i = 0; i < paramNames.Length; i++)
{
switch (paramNames[i])
{
case "MarshalType":
name = (string?)values[i];
break;
case "MarshalTypeRef":
type = Type.GetType((string)values[i]!);
name = (string?)values[i];
break;
case "MarshalCookie":
cookie = (string?)values[i];
Expand All @@ -491,7 +483,7 @@ private static void DecodeMarshalAsCustom(string[] paramNames, object?[] values,
}
}

info.SetMarshalAsCustom((object?)name ?? type!, cookie);
info.SetMarshalAsCustom(name, cookie);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public void AssemblyWithDifferentTypes()
eventb.SetRemoveOnMethod(mbRemove);
tbEvents.CreateType();*/
saveMethod.Invoke(ab, [file.Path]);
Console.WriteLine("Assembly saved to " + file.Path);

Assembly assemblyFromDisk = AssemblySaveTools.LoadAssemblyFromPath(file.Path);
CheckAssembly(assemblyFromDisk);
}
Expand All @@ -222,7 +222,7 @@ private static void CheckMarshallAttribute(IList<CustomAttributeData> attributes
{
CustomAttributeNamedArgument namedArg = cattr.NamedArguments.First(a => a.MemberName == namedArgument);
Assert.Equal(namedArgument, namedArg.MemberName);
Assert.Equal(naValue, namedArg.TypedValue.Value);
Assert.Equal(naValue, namedArg.TypedValue.Value.ToString());
}
}

Expand Down Expand Up @@ -317,15 +317,14 @@ void CheckAssembly(Assembly a)
CheckMarshallAttribute(field.GetCustomAttributesData(), UnmanagedType.U4);
// ByValArray
field = type4.GetField("FieldMarshalByvalArray");
CheckMarshallAttribute(field.GetCustomAttributesData(), UnmanagedType.ByValArray, nameof(MarshalAsAttribute.SizeConst), 16);
CheckMarshallAttribute(field.GetCustomAttributesData(), UnmanagedType.ByValArray, nameof(MarshalAsAttribute.SizeConst), "16");
// ByValTStr
field = type4.GetField("FieldMarshalByvalTStr");
CheckMarshallAttribute(field.GetCustomAttributesData(), UnmanagedType.ByValTStr, nameof(MarshalAsAttribute.SizeConst), 16);
CheckMarshallAttribute(field.GetCustomAttributesData(), UnmanagedType.ByValTStr, nameof(MarshalAsAttribute.SizeConst), "16");
// Custom marshaler
field = type4.GetField("FieldMarshalCustom");
CheckMarshallAttribute(field.GetCustomAttributesData(), UnmanagedType.CustomMarshaler, nameof(MarshalAsAttribute.MarshalCookie), "Cookie");
// TODO: Check 'MarshalTypeRef' case
//CheckMarshallAttribute(field.GetCustomAttributesData(), UnmanagedType.CustomMarshaler, nameof(MarshalAsAttribute.MarshalTypeRef), typeof(object));
CheckMarshallAttribute(field.GetCustomAttributesData(), UnmanagedType.CustomMarshaler, nameof(MarshalAsAttribute.MarshalTypeRef), typeof(object).ToString());

field = type4.GetField("FieldCAttr");
CheckCattr(field.GetCustomAttributesData());
Expand Down

0 comments on commit 0d23a23

Please sign in to comment.