posted by 방랑군 2009. 9. 24. 15:48
1. Obsolete 어트리뷰트
2.  사용자 어트리뷰트

====================================

Program.cs 코딩

------------------------------------

using System;

public class 특성
{
    public static void Main(string[] args)
    {
        Say1();
        Say2();
    }
   
    /// <summary>
    /// 닷넷 1.0 버전
    /// </summary>
    // Say1()은 오래된버전으로 가정하고 Say2()만 사용하고싶을때..ObsoleteAttribute사용 Attribute생략가능~
    [Obsolete("현재 메서드는 오래된 버전이므로, Say2()를 사용하세요." , false)] // false는 경고만 띄워주고,true는 컴파일오류내줌
    public static void Say1()
    {
        Console.WriteLine("안녕");
    }
   
    /// <summary>
    /// 닷넷 2.0 버전 이상
    /// </summary>
    public static void Say2()
    {
        Console.WriteLine("안녕하세열");
    }
}

=========================================

사용자정의특성.cs 코딩

-------------------------------------------

using System;

namespace 사용자정의특성
{
    [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple=true)]
    // AttributeUsate = 특정 멤버에만 특성적용해줌, AllowMultiple=true : 다중지정 가능하게..
   
    public class AuthorAttribute : Attribute
    {
        public string name;
        public AuthorAttribute(string name)
        {
            this.name = name;
        }
    }
   
    [AuthorAttribute("RedPlus")]
    class 사용자정의특성
    {
        static void Main(string[] args)
        {
            Say();
            ShowMetaData();
        }
        // 특성정보 읽어오기
        private static void ShowMetaData()
        {
            System.Attribute[] attrs = System.Attribute.GetCustomAttributes(typeof(사용자정의특성));
           
            foreach (var attr in attrs)
         {
         // if (attr is AuthorAttribute)
            // {
         //     AuthorAttribute aa = ( AuthorAttribute)attr;
               //     Console.WriteLine("{0}", aa.name);
            // }
                AuthorAttribute aa = attr as AuthorAttribute;
                if (aa != null)
                {
                    Console.WriteLine("{0}", aa.name);
                }
         }
        }
        static void Say() { Console.WriteLine("안녕하세요."); }
    }
}

==============================================



'GET > Attribute' 카테고리의 다른 글

참고 사이트.  (0) 2009.09.24
* Conditional 어트리뷰트 사용  (0) 2009.09.24
* DllImport 어트리뷰트  (0) 2009.09.24
Attrubute(어트리뷰트)  (0) 2009.09.24
Attribute Oriented Programming  (0) 2009.09.24