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) {
      }
    }
    }
  • 相关阅读:
    关于ng路由的传参问题(传递一个,多个参数)
    ng指令控制一个元素的影藏的与显示几种方法的使用
    将一个对象push到数组之中的几点问题
    关于ng的路由的几点想法(ui-view)
    angularJS自定义一个过滤器
    ng自带的表单验证
    实现标签的添加与删除(tags)
    关于ng-class,ng-style的用法
    5分钟搞懂:session与cookie
    前端浏览器的两种缓存:协商缓存和强缓存
  • 原文地址:https://www.cnblogs.com/lzjsky/p/2003410.html
Copyright © 2011-2022 走看看