Skip to content

Commit

Permalink
Remove CustomAttributeData usage in IsByRefLike (#80841)
Browse files Browse the repository at this point in the history
Grabbing custom attribute data brings the entire reflection stack (we need properties, constructors, fields, methods).

We ask `IsByRefLike` in type loader constraint validation. Ideally we should be able to get rid of the type loader from a hello world, but that's for later. This is a more efficient implementation either way.
  • Loading branch information
MichalStrehovsky committed Jan 19, 2023
1 parent 88ab5ba commit 6619e1b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,25 @@ public sealed override Assembly Assembly
}
}

public sealed override bool IsByRefLike
{
get
{
// If we have a type handle, ask the runtime
RuntimeTypeHandle typeHandle = InternalTypeHandleIfAvailable;
if (!typeHandle.IsNull)
return Internal.Runtime.Augments.RuntimeAugments.IsByRefLike(typeHandle);

// Otherwise fall back to attributes
foreach (CustomAttributeHandle cah in _typeDefinition.CustomAttributes)
{
if (cah.IsCustomAttributeOfType(_reader, "System.Runtime.CompilerServices", "IsByRefLikeAttribute"))
return true;
}
return false;
}
}

protected sealed override Guid? ComputeGuidFromCustomAttributes()
{
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ public sealed override IEnumerable<CustomAttributeData> CustomAttributes
}
}

public sealed override bool IsByRefLike
{
get
{
return Internal.Runtime.Augments.RuntimeAugments.IsByRefLike(_typeHandle);
}
}

public sealed override string FullName
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ private RuntimeCLSIDTypeInfo(Guid clsid, string server)
public sealed override string Namespace => BaseType.Namespace;
public sealed override StructLayoutAttribute StructLayoutAttribute => BaseType.StructLayoutAttribute;
public sealed override string ToString() => BaseType.ToString();
public sealed override bool IsByRefLike => false;

public sealed override IEnumerable<CustomAttributeData> CustomAttributes
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,7 @@ internal abstract class RuntimeTypeDefinitionTypeInfo : RuntimeTypeInfo
public sealed override bool IsGenericTypeParameter => false;
public sealed override bool IsGenericMethodParameter => false;

public sealed override bool IsByRefLike
{
get
{
RuntimeTypeHandle typeHandle = InternalTypeHandleIfAvailable;
if (!typeHandle.IsNull())
return RuntimeAugments.IsByRefLike(typeHandle);

foreach (CustomAttributeData cad in CustomAttributes)
{
if (cad.AttributeType == typeof(IsByRefLikeAttribute))
return true;
}
return false;
}
}
public abstract override bool IsByRefLike { get; }

// Left unsealed as RuntimeCLSIDTypeInfo has special behavior and needs to override.
public override bool HasSameMetadataDefinitionAs(MemberInfo other)
Expand Down

0 comments on commit 6619e1b

Please sign in to comment.