Part of DKNet.Fw.Extensions. Use AttributeExtensions to test for attributes without manually handling missing reflection metadata.
using System;
using System.ComponentModel.DataAnnotations;
using DKNet.Fw.Extensions.Reflection;
var item = new Item();
Console.WriteLine(item.HasAttributeOnProperty<RequiredAttribute>(nameof(Item.Name))); // True
Console.WriteLine(item.GetProperty(nameof(Item.Name)).HasAttribute<RequiredAttribute>()); // True
Console.WriteLine(typeof(Item).HasAttribute<SerializableAttribute>()); // False
public sealed class Item { [Required] public string Name { get; set; } = ""; }
HasAttribute<TAttribute> accepts a nullable Type or PropertyInfo. HasAttributeOnProperty<TAttribute> resolves the property with the default lookup from property access. All forms return false when their metadata lookup finds nothing.
| Option | Type | Default | Effect |
|---|---|---|---|
inherit |
bool |
true |
Passed to Attribute.IsDefined to control inherited attribute lookup. |
HasAttribute disables inheritance and has no inherit option; these helpers default to true.