1,定制特性
①C#允许用一个前缀明确指定特性要应用的目标元素
[assembly: AssemblyVersion("1.0.0.0")]
②AttributeUsage. Inherited(特性应用于基类时,是否同时应用于派生类和重写的方法)
[AttributeUsage(AttributeTargets.Class|AttributeTargets.Method,Inherited = true)] internal class TastyAttribute : Attribute { } [Tasty,Serializable] internal class BaseType { [Tasty] protected virtual void DoSomething(){} } //继承父类Tasty的特性 internal class DerivedType : BaseType { //继承Tasty的特性 protected override void DoSomething(){} }
//如果特性类没有显示的引用AttributeUsage,则默认AttributeUsage如下 [AttributeUsage(AttributeTargets.All,AllowMultiple = false, Inherited = true)]
时间: 2025-01-10 03:43:28