Attribute quick guide

來源:互聯網
上載者:User

copy from msdn, but make it much easier to find what you are looking for.

The Attribute class associates predefined system information or user-defined custom information with a target element. A target element can be an assembly, class, constructor, delegate, enum, event, field, interface, method, portable executable file module, parameter, property, return value, struct, or another attribute.

Information provided by an attribute is also known as metadata. Metadata can be examined at run time by your application to control how your program processes data, or before run time by external tools to control how your application itself is processed or maintained. For example, the .NET Framework predefines and uses attribute types to control run-time behavior, and some programming languages use attribute types to represent language features not directly supported by the .NET Framework common type system.

using System;
using System.Reflection;

namespace CustomAttrCS {
 // An enumeration of animals. Start at 1 (0 = uninitialized).
 public enum Animal {
  // Pets.
  Dog = 1,
  Cat,
  Bird,
 }

 // A custom attribute to allow a target to have a pet.
 public class AnimalTypeAttribute : Attribute {
  // The constructor is called when the attribute is set.
  public AnimalTypeAttribute(Animal pet) {
   thePet = pet;
  }

  // Keep a variable internally ...
  protected Animal thePet;

  // .. and show a copy to the outside world.
  public Animal Pet {
   get { return thePet; }
   set { thePet = Pet; }
  }
 }

 // A test class where each method has its own pet.
 class AnimalTypeTestClass {
  [AnimalType(Animal.Dog)]
  public void DogMethod() {}

  [AnimalType(Animal.Cat)]
  public void CatMethod() {}

  [AnimalType(Animal.Bird)]
  public void BirdMethod() {}
 }

 class DemoClass {
  static void Main(string[] args) {
   AnimalTypeTestClass testClass = new AnimalTypeTestClass();
   Type type = testClass.GetType();
   // Iterate through all the methods of the class.
   foreach(MethodInfo mInfo in type.GetMethods()) {
    // Iterate through all the Attributes for each method.
    foreach (Attribute attr in
     Attribute.GetCustomAttributes(mInfo)) {
     // Check for the AnimalType attribute.
     if (attr.GetType() == typeof(AnimalTypeAttribute))
      Console.WriteLine(
       "Method {0} has a pet {1} attribute.",
       mInfo.Name, ((AnimalTypeAttribute)attr).Pet);
    }

   }
  }
 }
}

/*
 * Output:
 * Method DogMethod has a pet Dog attribute.
 * Method CatMethod has a pet Cat attribute.
 * Method BirdMethod has a pet Bird attribute.
 */

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.