C#基础学习系列之Attribute和反射详解
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method |
AttributeTargets.Property | AttributeTargets.Field,
Inherited = true)]
public class InheritedAttribute : Attribute
{}
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method |
AttributeTargets.Property | AttributeTargets.Field,
Inherited = false)]
public class NotInheritedAttribute : Attribute
{}
using System;
using System.Reflection;
[InheritedAttribute]
public class BaseA
{
[InheritedAttribute]
public virtual void MethodA()
{}
}
public class DerivedA : BaseA
{
public override void MethodA()
{}
}
[NotInheritedAttribute]
public class BaseB
{
[NotInheritedAttribute]
public virtual void MethodB()
{}
}
public class DerivedB : BaseB
{
public override void MethodB()
{}
}
public class Example
{
public static void Main()
{
Type typeA = typeof(DerivedA);
Console.WriteLine("DerivedA has Inherited attribute: {0}",
typeA.GetCustomAttributes(typeof(InheritedAttribute), true).Length > 0);
MethodInfo memberA = typeA.GetMethod("MethodA");
Console.WriteLine("DerivedA.MemberA has Inherited attribute: {0}
",
memberA.GetCustomAttributes(typeof(InheritedAttribute), true).Length > 0);
Type typeB = typeof(DerivedB);
Console.WriteLine("DerivedB has Inherited attribute: {0}",
typeB.GetCustomAttributes(typeof(InheritedAttribute), true).Length > 0);
MethodInfo memberB = typeB.GetMethod("MethodB");
Console.WriteLine("DerivedB.MemberB has Inherited attribute: {0}",
memberB.GetCustomAttributes(typeof(InheritedAttribute), true).Length > 0);
}
}
// The example displays the following output:
// DerivedA has Inherited attribute: True
// DerivedA.MemberA has Inherited attribute: True
//
// DerivedB has Inherited attribute: False
// DerivedB.MemberB has Inherited attribute: False
- .NET Core系列之MemoryCache 初识
- 007手机一键Root(安机网一键Root) v3.0 官方最新版 一键ROOT您的Android手机
- 12306密码被盗了怎么办?12306密码外泄解决方法
- 12个字的qq网名
- 150M迷你型无线路由器怎么设置?
- 192.168.1.1打不开怎么办?路由器192.168.1.1打不开的原因以及解决办法
- 2011年电子报合订本 电子报 编辑部 中文 PDF版 [84M]
- 2015年1月15日小米新旗舰发布会现场图文直播
- 2016.3.1vivo Xplay5新品发布会现场视频直播 优酷直播
- 2016华为P9发布会视频直播地址 4月15日华为P9国行发布会直播