SPATIUM Mobile
주소복사
About Operating System Languages Tools Favorites Notice Visit TEST  
     ADK (3)
     android (3)
     ASP (5)
     ASP.Net (1)
     C# (5)
     HTML (9)
     JAVA (7)
     JavaScript (9)
     jQuery (5)
     Mobile Web (2)
     node.js (1)
     Oracle (8)
     PHP (7)
     Spring Framework (4)
     SQL (17)
     WPF (5)
   ID  
   Password  
  |  
  Location United States
  IP Address 18.117.153.38
2024. 04
123456
78910111213
14151617181920
21222324
25
2627
282930
Category  Languages, C#
Writer 김태우 Date 2014-02-14 16:56:01 Visit 10027
C# Attribute(어트리뷰트) : (JAVA에서는 @사용, Annotation(어노테이션))
 
 

C# Attribute(어트리뷰트)

 
    컴파일러에게 코드에 대한 추가 정보를 제공, 선언 형식
    어트리뷰트는 크게 시스템이 제공하는 공통 어트리뷰트와 사용자가 직접 정의하는 커스텀 어트리뷰트로 구분
 
 

1. Attribute Naming

 
[대문자+소문자+Attribute]
 
: 뒤에 Attribute를 붙이면 Attribute를 사용할때 Attribute를 빼고 사용가능
 
 
예) [Requested] = [RequestedAttribute]
 
 

2. 공통 어트리뷰트(Conditinal, Obsolete, DllImport)

 
예) COM 라이브러 함수나 Win32함수 사용
 
[DllImport("user32.dll")]
public static extern int MessageBox(int hParent, string Message, string Caption, int Type);
 
외부의 DLL 함수이므로 extern 지정자, 클래스 멤버가 아니므로 static
 
 

3. Custom Attribute(커스텀 어트리뷰트)

 
컴파일 방식이나 생성되는 기계어 코드에는 전혀 영향을 주지 않음.
실행 파일에 메타 데이타로 포함, 리플렉션으로 이 정보를 관리 가능.
대규모 프로젝트에서 작성자, 작성시점, 수정사항, 버그, 메모등 기록하여 사용, 프로젝트 관리가 대폭 자동화됨
 
예)
using System;
 
// Attribute도 class로 정의, Attribute 클래스로 부터 상속받아야 함.
// AttributeUsage 는 생략가능하나 정확한 대상 지정시 꼭 필요함
// AttributeUsage 속성 ValidOn : AttributeTargets - 열거형으로 어떤코드에 적용할지 지정, 두개이상은 | 연산자 사용
// AttributeUsage 속성 AllowMultiple : 한 대상에 여러번 적용 가능한지 지정, Default = false
// AttributeUsage 속성 Inherited : 파생 클래스나 재정의 메서드 적용가능한지 지정, Default = true
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Field, AllowMultiple = true, Inherited = false)]
class ProgrammerAttribute : Attribute
{
    public string Name;
    private string Time;
    public ProgrammerAttribute(string aName)
    {
        Name = aName;
        Time = "기록없음";
    }
 
    public string When
    {
        get { return Time; }
        set { Time = value; }
    }
}
 
class CStest
{
    // Programmer에서 Name은 이름없는 인수, When은 이름있는 인수이다. 
    // 이름없는 인수는 생성자로 전달, 반드시 지정해야 하지만, 이름있는 인수는 언제든 지정 가능
    [Programmer("Kim")]
    static public int Field1 = 0;
 
    // 이름있는 인수, 없는 인수 둘다 지정할 경우, 이름없는 인수가 반드시 먼저 와야함
    [Programmer("Kim",When= "2007년 6월 29일")]
    static public void Method1(){}
 
    [Programmer ("Lee")]
    static public void Method2(){}
  
    // Park과 Choi가 같이 만들었음을 표현
    [Programmer("Park"),Programmer("Choi")]
    static public void Method3(){}
  
    static void Test()
    {
        PrintInfo(typeof(Field1));
        PrintInfo(typeof(Method1));
        PrintInfo(typeof(Method2));
        PrintInfo(typeof(Method3));
    }
 
    static void PrintInfo(System.Type t)
    {
        System.Console.WriteLine("information for {0}", t);
 
        // Using reflection.
        System.Attribute[] attrs = System.Attribute.GetCustomAttributes(t);  // Reflection.
    
        // Displaying output.
        foreach (System.Attribute attr in attrs)
        {
            if (attr is ProgrammerAttribute)
            {
                ProgrammerAttribute a = (ProgrammerAttribute)attr;
                System.Console.WriteLine("   {0}, version {1}", a.Name, a.When);
            }
        }
    }
}
 
 
상기 코드는 임의로 작성된 코드로 실행시 오류가 발생할수 있습니다.
 
 
 
Tags  Custom Attribute, reflection, AttributeUsage, GetC
  Relation Articles
[Languages-C#] C# Attribute(어트리뷰트) : (JAVA에서는 @사용, Annotation(어노테이션)) (2014-02-14 16:56:01)
  Your Opinion
Member ID
150 letters
Copyright (C) SPATIUM. All rights reserved.
[SPATIUM]WebMaster Mail