zoukankan      html  css  js  c++  java
  • AttributeUsage AttributeTargets

    在C#的类中,有的类加上了
    [AttributeUsage(AttributeTargets.Property)]
    这个是起什么作用的呢?
    AttributeTargets 枚举
    成员名称 说明
    All 可以对任何应用程序元素应用属性。 
    Assembly 可以对程序集应用属性。 
    Class 可以对类应用属性。 
    Constructor 可以对构造函数应用属性。 
    Delegate 可以对委托应用属性。 
    Enum 可以对枚举应用属性。 
    Event 可以对事件应用属性。 
    Field 可以对字段应用属性。 
    GenericParameter 可以对泛型参数应用属性。 
    Interface 可以对接口应用属性。 
    Method 可以对方法应用属性。 
    Module 可以对模块应用属性。 注意
    Module 指的是可移植的可执行文件(.dll 或 .exe),而非 Visual Basic 标准模块。


    Parameter 可以对参数应用属性。 
    Property 可以对属性 (Property) 应用属性 (Attribute)。 
    ReturnValue 可以对返回值应用属性。 
    Struct 可以对结构应用属性,即值类型

    下面的代码示例演示如何应用 AttributeTargets 枚举:

    using System;

    namespace AttTargsCS {
    // This attribute is only valid on a class.
    [AttributeUsage(AttributeTargets.Class)]
    public class ClassTargetAttribute : Attribute {
    }

    // This attribute is only valid on a method.
    [AttributeUsage(AttributeTargets.Method)]
    public class MethodTargetAttribute : Attribute {
    }

    // This attribute is only valid on a constructor.
    [AttributeUsage(AttributeTargets.Constructor)]
    public class ConstructorTargetAttribute : Attribute {
    }

    // This attribute is only valid on a field.
    [AttributeUsage(AttributeTargets.Field)]
    public class FieldTargetAttribute : Attribute {
    }

    // This attribute is valid on a class or a method.
    [AttributeUsage(AttributeTargets.Class|AttributeTargets.Method)]
    public class ClassMethodTargetAttribute : Attribute {
    }

    // This attribute is valid on any target.
    [AttributeUsage(AttributeTargets.All)]
    public class AllTargetsAttribute : Attribute {
    }

    [ClassTarget]
    [ClassMethodTarget]
    [AllTargets]
    public class TestClassAttribute {
      [ConstructorTarget]
      [AllTargets]
      TestClassAttribute() {
      }

      [MethodTarget]
      [ClassMethodTarget]
      [AllTargets]
      public void Method1() {
      }

      [FieldTarget]
      [AllTargets]
      public int myInt;

      static void Main(string[] args) {
      }
    }
    }
  • 相关阅读:
    es 报错cannot allocate because allocation is not permitted to any of the nodes
    linux下获取软件源码包 centos/redhat, debian/ubuntu
    windows假死原因调查
    k8s-calico
    helm使用
    docker网络模式
    4、formula 法则、原则、数学公式
    powershell自动添加静态IP
    WDS部署Windows server2012初试
    2、puppet资源详解
  • 原文地址:https://www.cnblogs.com/lzjsky/p/2003410.html
Copyright © 2011-2022 走看看