在编译的时候添加元数据
一般用的不多
在实现O/R MAPPING的时候可以使用 特性

解决方案 »

  1.   

    看看这个把
    http://www.enet.com.cn/eschool/inforcenter/A20040407300659.html
      

  2.   

    Attribute是.net中的一个类,它的作用是使编译器在编译的时候生成指定的代码,在.net中有两种Attribute,一种是.net framework基本类库中已经定义的Attribute,另外一种是定制的Attribute,适当地使用Attribute类,可以自动化地完成一些编程任务。(不要把Attribute和Property弄混了,两者是不同的概念)
      

  3.   

    各位老大,我还是不明白,当初.NET设计者们,引入特性(Attribute)的目的是为了解决什么问题呢,那么在C#中如何利用特性(Attribute)来解决这些问题呢,头大ing........
      

  4.   

    属性(Attribute)其实是开发人员对类,方法所"添加"的额外信息,你可以通过反射查看这些类,方法的说明.属性分为
    1:已定义Attribute,如:DllImport(用于将方法标记为在外部定义),Conditonal(用于将阻止方法被编译,除非满足Conditonal条件)等等.2:定制属性,就是你自己定义的属性,继承自Attribute,如可以定义装配件的版本信息等
      

  5.   

    to 楼上:
    我明白了,Attribute是对类及方法等“添加”额外信息,但是咱们什么时候需要“添加”这些信息呢,如何添加呢,再如何通过反射查看呢,不好意思,我实在是不明白。
      

  6.   

    下面的代码实例说明了 Attribute 的用法:
    [Visual Basic] 
    Imports System
    Imports System.ReflectionPublic Module CustomAttrVB    ' An enumeration of animals. Start at 1 (0 = uninitialized).
        Public Enum Animal
            ' Pets
            Dog = 1
            Cat
            Bird
        End Enum    ' Visual Basic requires the AttributeUsage be specified.
        ' A custom attribute to allow a target to have a pet.
        <AttributeUsage(AttributeTargets.Method)> _
        Public Class AnimalTypeAttribute
            Inherits Attribute        ' The constructor is called when the attribute is set.
            Public Sub New(ByVal animal As Animal)
                Me.thePet = animal
            End Sub        ' Keep a variable internally ...
            Protected thePet As Animal        ' .. and show a copy to the outside world.
            Public Property Pet() As Animal
                Get
                    Return thePet
                End Get
                Set(ByVal Value As Animal)
                    thePet = Value
                End Set
            End Property    End Class    ' A test class where each method has its own pet.
        Class AnimalTypeTestClass        <AnimalType(Animal.Dog)> _
            Public Sub DogMethod()
            End Sub        <AnimalType(Animal.Cat)> _
            Public Sub CatMethod()
            End Sub        <AnimalType(Animal.Bird)> _
            Public Sub BirdMethod()
            End Sub
        End Class    ' The runtime test.
        Sub Main()
            Dim testClass As New AnimalTypeTestClass()
            Dim tcType As Type = testClass.GetType()
            Dim mInfo As MethodInfo
            ' Iterate through all the methods of the class.
            For Each mInfo In tcType.GetMethods()
                Dim attr As Attribute
                ' Iterate through all the attributes of the method.
                For Each attr In Attribute.GetCustomAttributes(mInfo)
                    If TypeOf attr Is AnimalTypeAttribute Then
                        Dim attrCustom As AnimalTypeAttribute = _
                            CType(attr, AnimalTypeAttribute)
                        Console.WriteLine("Method {0} has a pet {1} attribute.", _
                             mInfo.Name(), attrCustom.Pet.ToString())
                    End If
                Next
            Next
        End Sub
    End Module' Output:
    ' Method DogMethod has a pet Dog attribute.
    ' Method CatMethod has a pet Cat attribute.
    ' Method BirdMethod has a pet Bird attribute.
    [C#] 
    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.
     */
      

  7.   

    特性(Attributes)是一种崭新的声明性信息。我们不仅可以通过特性来定义设计层面的信息(例如help file, URL for documentation)以及运行时(run-time)信息(例如使XML与class相联系),而且我们还可以利用特性建立自描述(self-describing)组件。在这篇教程中,我们将会看到如何建立和添加特性到各种程序实体以及如何在运行时环境中获取特性信息。
      

  8.   

    唉。_____________________________________________________________________________该问题已经结贴 ,得分记录:  zairwolfc (300)