Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[mono] Allow overriding GetCustomAttributesData routines
* Merge common bits of Mono and CoreCLR CustomAttributeData implementation

* Have CustomAttributeData.GetCustomAttributes call target.GetCustomAttributesData
  to allow derived classes to override the latter (like in CoreCLR)

* Use Constructor, ConstructorArguments, and NamedArguments in internal
  routines to allow them being overridden (like with CoreCLR).
  • Loading branch information
uweigand committed Jul 16, 2021
commit eb3bf12d43d51ccc32fc46a76e736d38cbc24675
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,8 @@

namespace System.Reflection
{
public class CustomAttributeData
public partial class CustomAttributeData
{
#region Public Static Members
public static IList<CustomAttributeData> GetCustomAttributes(MemberInfo target)
{
if (target is null)
throw new ArgumentNullException(nameof(target));

return target.GetCustomAttributesData();
}

public static IList<CustomAttributeData> GetCustomAttributes(Module target)
{
if (target is null)
throw new ArgumentNullException(nameof(target));

return target.GetCustomAttributesData();
}

public static IList<CustomAttributeData> GetCustomAttributes(Assembly target)
{
if (target is null)
throw new ArgumentNullException(nameof(target));

return target.GetCustomAttributesData();
}

public static IList<CustomAttributeData> GetCustomAttributes(ParameterInfo target)
{
if (target is null)
throw new ArgumentNullException(nameof(target));

return target.GetCustomAttributesData();
}
#endregion

#region Internal Static Members
internal static IList<CustomAttributeData> GetCustomAttributesInternal(RuntimeType target)
{
Expand Down Expand Up @@ -448,44 +414,7 @@ private void Init(object pca)
}
#endregion

#region Object Override
public override string ToString()
{
var vsb = new ValueStringBuilder(stackalloc char[256]);

vsb.Append('[');
vsb.Append(Constructor.DeclaringType!.FullName);
vsb.Append('(');

bool first = true;

int count = ConstructorArguments.Count;
for (int i = 0; i < count; i++)
{
if (!first) vsb.Append(", ");
vsb.Append(ConstructorArguments[i].ToString());
first = false;
}

count = NamedArguments.Count;
for (int i = 0; i < count; i++)
{
if (!first) vsb.Append(", ");
vsb.Append(NamedArguments[i].ToString());
first = false;
}

vsb.Append(")]");

return vsb.ToString();
}
public override int GetHashCode() => base.GetHashCode();
public override bool Equals(object? obj) => obj == (object)this;
#endregion

#region Public Members
public virtual Type AttributeType => Constructor.DeclaringType!;

public virtual ConstructorInfo Constructor => m_ctor;

public virtual IList<CustomAttributeTypedArgument> ConstructorArguments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\CallingConventions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\ConstructorInfo.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\CorElementType.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\CustomAttributeData.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\CustomAttributeExtensions.cs" Condition="'$(TargetsCoreRT)' != 'true'" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\CustomAttributeFormatException.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\CustomAttributeNamedArgument.cs" Condition="'$(TargetsCoreRT)' != 'true'" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Text;

namespace System.Reflection
{
public partial class CustomAttributeData
{
#region Public Static Members
public static IList<CustomAttributeData> GetCustomAttributes(MemberInfo target)
{
if (target is null)
throw new ArgumentNullException(nameof(target));

return target.GetCustomAttributesData();
}

public static IList<CustomAttributeData> GetCustomAttributes(Module target)
{
if (target is null)
throw new ArgumentNullException(nameof(target));

return target.GetCustomAttributesData();
}

public static IList<CustomAttributeData> GetCustomAttributes(Assembly target)
{
if (target is null)
throw new ArgumentNullException(nameof(target));

return target.GetCustomAttributesData();
}

public static IList<CustomAttributeData> GetCustomAttributes(ParameterInfo target)
{
if (target is null)
throw new ArgumentNullException(nameof(target));

return target.GetCustomAttributesData();
}
#endregion

#region Object Override
public override string ToString()
{
var vsb = new ValueStringBuilder(stackalloc char[256]);

vsb.Append('[');
vsb.Append(Constructor.DeclaringType!.FullName);
vsb.Append('(');

bool first = true;

int count = ConstructorArguments.Count;
for (int i = 0; i < count; i++)
{
if (!first) vsb.Append(", ");
vsb.Append(ConstructorArguments[i].ToString());
first = false;
}

count = NamedArguments.Count;
for (int i = 0; i < count; i++)
{
if (!first) vsb.Append(", ");
vsb.Append(NamedArguments[i].ToString());
first = false;
}

vsb.Append(")]");

return vsb.ToString();
}
public override int GetHashCode() => base.GetHashCode();
public override bool Equals(object? obj) => obj == (object)this;
#endregion

#region Public Members
public virtual Type AttributeType => Constructor.DeclaringType!;
#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@
<Compile Include="$(BclSourcesRoot)\System\Reflection\Assembly.Mono.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\AssemblyName.Mono.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\CustomAttribute.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\CustomAttributeData.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\CustomAttributeData.Mono.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\CustomAttributeTypedArgument.Mono.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\FieldInfo.Mono.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\MemberInfo.Mono.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

namespace System.Reflection
{
public class CustomAttributeData
public partial class CustomAttributeData
{
private sealed class LazyCAttrData
{
Expand Down Expand Up @@ -125,62 +125,49 @@ IList<CustomAttributeNamedArgument> NamedArguments
}
}

public static IList<CustomAttributeData> GetCustomAttributes(Assembly target)
internal static IList<CustomAttributeData> GetCustomAttributesInternal(RuntimeType target)
{
return CustomAttribute.GetCustomAttributesData(target);
}

public static IList<CustomAttributeData> GetCustomAttributes(MemberInfo target)
internal static IList<CustomAttributeData> GetCustomAttributesInternal(RuntimeFieldInfo target)
{
return CustomAttribute.GetCustomAttributesData(target);
}

internal static IList<CustomAttributeData> GetCustomAttributesInternal(RuntimeType target)
internal static IList<CustomAttributeData> GetCustomAttributesInternal(RuntimeMethodInfo target)
{
return CustomAttribute.GetCustomAttributesData(target);
}

public static IList<CustomAttributeData> GetCustomAttributes(Module target)
internal static IList<CustomAttributeData> GetCustomAttributesInternal(RuntimeConstructorInfo target)
{
return CustomAttribute.GetCustomAttributesData(target);
}

public static IList<CustomAttributeData> GetCustomAttributes(ParameterInfo target)
internal static IList<CustomAttributeData> GetCustomAttributesInternal(RuntimeEventInfo target)
{
return CustomAttribute.GetCustomAttributesData(target);
}

public virtual Type AttributeType
internal static IList<CustomAttributeData> GetCustomAttributesInternal(RuntimePropertyInfo target)
{
get { return ctorInfo.DeclaringType!; }
return CustomAttribute.GetCustomAttributesData(target);
}

public override string ToString()
internal static IList<CustomAttributeData> GetCustomAttributesInternal(RuntimeModule target)
{
ResolveArguments();

StringBuilder sb = new StringBuilder();

sb.Append('[').Append(ctorInfo.DeclaringType!.FullName).Append('(');
for (int i = 0; i < ctorArgs.Count; i++)
{
sb.Append(ctorArgs[i].ToString());
if (i + 1 < ctorArgs.Count)
sb.Append(", ");
}

if (namedArgs.Count > 0)
sb.Append(", ");
return CustomAttribute.GetCustomAttributesData(target);
}

for (int j = 0; j < namedArgs.Count; j++)
{
sb.Append(namedArgs[j].ToString());
if (j + 1 < namedArgs.Count)
sb.Append(", ");
}
sb.Append(")]");
internal static IList<CustomAttributeData> GetCustomAttributesInternal(RuntimeAssembly target)
{
return CustomAttribute.GetCustomAttributesData(target);
}

return sb.ToString();
internal static IList<CustomAttributeData> GetCustomAttributesInternal(RuntimeParameterInfo target)
{
return CustomAttribute.GetCustomAttributesData(target);
}

private static T[] UnboxValues<T>(object[] values)
Expand All @@ -191,13 +178,6 @@ private static T[] UnboxValues<T>(object[] values)

return retval;
}

public override int GetHashCode() => base.GetHashCode();

public override bool Equals(object? obj)
{
return obj == (object)this;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public override bool IsDefined(Type attributeType, bool inherit)

public override IList<CustomAttributeData> GetCustomAttributesData()
{
return CustomAttributeData.GetCustomAttributes(this);
return CustomAttributeData.GetCustomAttributesInternal(this);
}

public override object[] GetCustomAttributes(bool inherit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public override object[] GetCustomAttributes(Type attributeType, bool inherit)

public override IList<CustomAttributeData> GetCustomAttributesData()
{
return CustomAttributeData.GetCustomAttributes(this);
return CustomAttributeData.GetCustomAttributesInternal(this);
}

public override int MetadataToken
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ internal RuntimeFieldInfo Clone(string newName)

public override IList<CustomAttributeData> GetCustomAttributesData()
{
return CustomAttributeData.GetCustomAttributes(this);
return CustomAttributeData.GetCustomAttributesInternal(this);
}

private void CheckGeneric()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ public override MethodBody GetMethodBody()

public override IList<CustomAttributeData> GetCustomAttributesData()
{
return CustomAttributeData.GetCustomAttributes(this);
return CustomAttributeData.GetCustomAttributesInternal(this);
}

public sealed override bool HasSameMetadataDefinitionAs(MemberInfo other) => HasSameMetadataDefinitionAsCore<RuntimeMethodInfo>(other);
Expand Down Expand Up @@ -988,7 +988,7 @@ public override string ToString()

public override IList<CustomAttributeData> GetCustomAttributesData()
{
return CustomAttributeData.GetCustomAttributes(this);
return CustomAttributeData.GetCustomAttributesInternal(this);
}

public sealed override bool HasSameMetadataDefinitionAs(MemberInfo other) => HasSameMetadataDefinitionAsCore<RuntimeConstructorInfo>(other);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ Type[] GetTypes()

public override IList<CustomAttributeData> GetCustomAttributesData()
{
return CustomAttributeData.GetCustomAttributes(this);
return CustomAttributeData.GetCustomAttributesInternal(this);
}

internal RuntimeAssembly GetRuntimeAssembly()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ bool IsDefined(Type attributeType, bool inherit)

public override IList<CustomAttributeData> GetCustomAttributesData()
{
return CustomAttributeData.GetCustomAttributes(this);
return CustomAttributeData.GetCustomAttributesInternal(this);
}

[MethodImplAttribute(MethodImplOptions.InternalCall)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ public override void SetValue(object? obj, object? value, BindingFlags invokeAtt

public override IList<CustomAttributeData> GetCustomAttributesData()
{
return CustomAttributeData.GetCustomAttributes(this);
return CustomAttributeData.GetCustomAttributesInternal(this);
}

public sealed override bool HasSameMetadataDefinitionAs(MemberInfo other) => HasSameMetadataDefinitionAsCore<RuntimePropertyInfo>(other);
Expand Down