你这个问题,类似于这个 https://stackoverflow.com/questions/72818130
如果你没有在子类自定义重写Isdefined 方法,那实际上调用的是CLR 运行时环境定义的Isdefined 方法。
从开源代码中可以找到 dotnet/runtime
#region ICustomAttributeProvider
public override object[] GetCustomAttributes(bool inherit)
{
return CustomAttribute.GetCustomAttributes(this, (typeof(object) as RuntimeType)!);
}
public override object[] GetCustomAttributes(Type attributeType, bool inherit)
{
ArgumentNullException.ThrowIfNull(attributeType);
if (attributeType.UnderlyingSystemType is not RuntimeType attributeRuntimeType)
throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));
return CustomAttribute.GetCustomAttributes(this, attributeRuntimeType);
}
public override bool IsDefined(Type attributeType, bool inherit)
{
ArgumentNullException.ThrowIfNull(attributeType);
if (attributeType.UnderlyingSystemType is not RuntimeType attributeRuntimeType)
throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));
return CustomAttribute.IsDefined(this, attributeRuntimeType);
}
结合MemberInfo继承ICustomAttributeProvider接口,应该能明白了。
namespace System.Reflection
{
//
// Summary:
// Obtains information about the attributes of a member and provides access to member
// metadata.
public abstract class MemberInfo : ICustomAttributeProvider