zoukankan      html  css  js  c++  java
  • AttributeTargets 枚举

    AttributeUsage AttributeTargets

    在C#的类中,有的类加上了
    [AttributeUsage(AttributeTargets.Property)]
    这个是起什么作用的呢?
    AttributeTargets 枚举  

    成员名称 说明
    All 可以对任何应用程序元素应用属性。
    Assembly 可以对程序集应用属性。
    Class 可以对类应用属性。
    Constructor 可以对构造函数应用属性。
    Delegate 可以对委托应用属性。
    Enum 可以对枚举应用属性。
    Event 可以对事件应用属性。
    Field 可以对字段应用属性。
    GenericParameter 可以对泛型参数应用属性。
    Interface 可以对接口应用属性。
    Method 可以对方法应用属性。
    Module 可以对模块应用属性。
    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)
            {
            }
        }
    }
  • 相关阅读:
    学习《Spring 3.x 企业应用开发实战》Day-1
    java面试32问
    线程的同步和异步
    TCP和UDP的区别
    深入理解Spring系列之四:BeanDefinition装载前奏曲
    深入浅出JDK动态代理(一)
    和大牛之间的差距
    深入理解Spring系列之三:BeanFactory解析
    深入理解Spring系列之二:BeanDefinition解析
    深入理解Spring系列之一:开篇
  • 原文地址:https://www.cnblogs.com/gsk99/p/4994662.html
Copyright © 2011-2022 走看看