mirror of
https://github.com/AssetRipper/AssemblyDumper.git
synced 2026-11-13 20:05:24 -10:00
33 lines
1.3 KiB
C#
33 lines
1.3 KiB
C#
using AsmResolver.DotNet;
|
|
using System.Linq;
|
|
|
|
namespace AssetRipper.DocExtraction.Extensions;
|
|
|
|
internal static class HasCustomAttributeExtensions
|
|
{
|
|
|
|
public static CustomAttribute? GetCustomAttribute(this IHasCustomAttribute hasCustomAttributeObject, string @namespace, string name)
|
|
{
|
|
return hasCustomAttributeObject.CustomAttributes.Count > 0
|
|
? hasCustomAttributeObject.CustomAttributes
|
|
.FirstOrDefault(attr => MatchesNameAndNamespace(attr.Constructor?.DeclaringType, @namespace, name))
|
|
: null;
|
|
}
|
|
|
|
public static bool HasAttribute(this IHasCustomAttribute hasCustomAttributeObject, string @namespace, string name)
|
|
{
|
|
return hasCustomAttributeObject.CustomAttributes.Count > 0
|
|
&& hasCustomAttributeObject.CustomAttributes.Any(attr => MatchesNameAndNamespace(attr.Constructor?.DeclaringType, @namespace, name));
|
|
}
|
|
|
|
public static bool TryGetCustomAttribute(this IHasCustomAttribute hasCustomAttributeObject, string @namespace, string name, [NotNullWhen(true)] out CustomAttribute? attribute)
|
|
{
|
|
attribute = hasCustomAttributeObject.GetCustomAttribute(@namespace, name);
|
|
return attribute is not null;
|
|
}
|
|
|
|
private static bool MatchesNameAndNamespace(ITypeDefOrRef? type, string @namespace, string name)
|
|
{
|
|
return type is not null && type.Namespace == @namespace && type.Name == name;
|
|
}
|
|
} |