当前位置: 首页>>代码示例>>C#>>正文


C# AttributeUsageAttribute.Inherited属性代码示例

本文整理汇总了C#中System.AttributeUsageAttribute.Inherited属性的典型用法代码示例。如果您正苦于以下问题:C# AttributeUsageAttribute.Inherited属性的具体用法?C# AttributeUsageAttribute.Inherited怎么用?C# AttributeUsageAttribute.Inherited使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。


在下文中一共展示了AttributeUsageAttribute.Inherited属性的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1:

[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
{ }
开发者ID:.NET开发者,项目名称:System,代码行数:11,代码来源:AttributeUsageAttribute.Inherited

示例2: MethodA

//引入命名空间
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: {typeA.GetCustomAttributes(typeof(InheritedAttribute), true).Length > 0}");
        MethodInfo memberA = typeA.GetMethod(nameof(DerivedA.MethodA));
        Console.WriteLine($"DerivedA.MemberA has Inherited attribute: {memberA.GetCustomAttributes(typeof(InheritedAttribute), true).Length > 0}\n");

        Type typeB = typeof(DerivedB);
        Console.WriteLine($"DerivedB has NotInherited attribute: {typeB.GetCustomAttributes(typeof(NotInheritedAttribute), true).Length > 0}");
        MethodInfo memberB = typeB.GetMethod(nameof(DerivedB.MethodB));
        Console.WriteLine($"DerivedB.MemberB has NotInherited attribute: {memberB.GetCustomAttributes(typeof(NotInheritedAttribute), true).Length > 0}");
    }
}
开发者ID:.NET开发者,项目名称:System,代码行数:47,代码来源:AttributeUsageAttribute.Inherited

输出:

DerivedA has Inherited attribute: True
DerivedA.MemberA has Inherited attribute: True

DerivedB has NotInherited attribute: False
DerivedB.MemberB has NotInherited attribute: False


注:本文中的System.AttributeUsageAttribute.Inherited属性示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。