C# ๊ณ ๊ธ‰ ํ”„๋กœ๊ทธ๋ž˜๋ฐ - ์• ํŠธ๋ฆฌ๋ทฐํŠธ

2020. 11. 27. 17:07ใ†์ „๊ณต ๊ณผ๋ชฉ/์œˆ๋„์šฐ ํ”„๋กœ๊ทธ๋ž˜๋ฐ

C# ๊ณ ๊ธ‰ ํ”„๋กœ๊ทธ๋ž˜๋ฐ - ์• ํŠธ๋ฆฌ๋ทฐํŠธ

 

์• ํŠธ๋ฆฌ๋ทฐํŠธ

- ์–ด์…ˆ๋ธ”๋ฆฌ๋‚˜ ํด๋ž˜์Šค, ํ•„๋“œ, ๋ฉ”์†Œ๋“œ, ํ”„๋กœํผํ‹ฐ ๋“ฑ์— ๋‹ค์–‘ํ•œ ์ •๋ณด๋ฅผ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉ

- ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ํ˜•์‹์œผ๋กœ ์ €์žฅ๋จ.

 

์• ํŠธ๋ฆฌ๋ทฐํŠธ๋Š” ํ‘œ์ค€ ์• ํŠธ๋ฆฌ๋ทฐํŠธ(.NET ํ”„๋ ˆ์ž„์›Œํฌ ์ œ๊ณต)๊ณผ ์‚ฌ์šฉ์ž ์ •์˜ ์• ํŠธ๋ฆฌ๋ทฐํŠธ๊ฐ€ ์žˆ๋‹ค.

 

๋จผ์ € ํ‘œ์ค€ ์• ํŠธ๋ฆฌ๋ทฐํŠธ๋ฅผ ์•Œ์•„๋ณด์ž.

 

ํ‘œ์ค€ ์• ํŠธ๋ฆฌ๋ทฐํŠธ

- Conditional ์• ํŠธ๋ฆฌ๋ทฐํŠธ

  • ์กฐ๊ฑด๋ถ€ ๋ฉ”์†Œ๋“œ๋ฅผ ์ž‘์„ฑํ•  ๋–„ ์‚ฌ์šฉ
  • define ์ „์ฒ˜๋ฆฌ๊ธฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์กฐ๊ฑด์„ ์ •ํ•œ๋‹ค.
  • ๋ฐ˜๋“œ์‹œ System.dianostics๋ฅผ ๋„ค์ž„์ŠคํŽ˜์ด์Šค์— ํฌํ•จ์‹œ์ผœ์•ผ ํ•œ๋‹ค.

Conditional ์• ํŠธ๋ฆฌ๋ทฐํŠธ์˜ ์˜ˆ์‹œ

#define CSHARP
using System;
using System.Diagnostics;

    class ConditionalAttrApp
    {
        [Conditional("CSHARP")]
        public static void CsharpMethod()
        {
            Console.WriteLine("In the CSharp Method...");
        }
        [Conditional("JAVA")]
        public static void JavaMethod()
        {
            Console.WriteLine("In the Java Method...");
        }
    public class BoundedGenericApp
    {
        public static void Main(string[] args)
        {
            ConditionalAttrApp.CsharpMethod();
            ConditionalAttrApp.JavaMethod();
        }
    }
}
    

๊ฒฐ๊ณผ ํ™”๋ฉด

 

์ „์ฒ˜๋ฆฌ๊ธฐ์— ์žˆ๋Š” ๊ฒƒ๋งŒ์ด Conditional์— ์ ์šฉ์ด ๋˜๊ณ , ๊ทธ๊ฒƒ๋งŒ ์‹คํ–‰์ด ๋œ๋‹ค.

 

- Obsolete ์• ํŠธ๋ฆฌ๋ทฐํŠธ

  • ์•ž์œผ๋กœ ์‚ฌ์šฉ๋˜์ง€ ์•Š์„ ๋ฉ”์†Œ๋“œ๋ฅผ ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ์• ํŠธ๋ฆฌ๋ทฐํŠธ
  • ๊ฒฝ๊ณ ๋ฅผ ํ†ตํ•ด ์‚ฌ์šฉ๋˜์ง€ ์•Š๋Š” ๋ฉ”์†Œ๋“œ์ž„์„ ๊ฐ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค.

Obsolete ์• ํŠธ๋ฆฌ๋ทฐํŠธ ์‚ฌ์šฉ ์˜ˆ์ œ

class ObsoleteAttrApp
{
    [Obsolete("๊ฒฝ๊ณ , Obsolete Method์ž…๋‹ˆ๋‹ค.")]
    public static void OldMethod()
    {
        Console.WriteLine("In the Old Method");
    }
    public static void NormalMethod()
    {
        Console.WriteLine("In the Normal Method...");
    }
}
    public class Program
    {
        public static void Main(string[] args)
        {
        ObsoleteAttrApp.OldMethod();
        ObsoleteAttrApp.NormalMethod();
        }
    }

 ์œ„ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด OldMethod์— ๊ฒฝ๊ณ ์ค„์ด ๋„์›Œ์ง„๋‹ค. ์™œ๋ƒํ•˜๋ฉด Obsolete๊ฒฝ๊ณ  ๋ฉ”์„ธ์ง€๊ฐ€ ์กด์žฌํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

์‚ฌ์šฉ์ž ์ •์˜ ์• ํŠธ๋ฆฌ๋ทฐํŠธ

- ํ”„๋กœ๊ทธ๋ž˜๋จธ์— ์˜ํ•ด ์ •์˜๋œ ์• ํŠธ๋ฆฌ๋ทฐํŠธ.

- ์ด ํด๋ž˜์Šค๋Š” System.Attribute ํด๋ž˜์Šค์—์„œ ํŒŒ์ƒํ•ด์•ผ ํ•˜๋ฉฐ, ์ด๋ฆ„์€ ํ•ญ์ƒ XxxxAttribute ํ˜•ํƒœ๋ฅผ ๊ฐ€์ ธ์•ผ ํ•œ๋‹ค.

 

์‚ฌ์šฉ์ž ์ •์˜ ์• ํŠธ๋ฆฌ๋ทฐํŠธ์˜ ์˜ˆ์ œ

public class MyAttrAttribute : Attribute
{
    public MyAttrAttribute(string message)
    {
        this.message = message;
    }
    private string message;
    public string Message
    {
        get
        {
            return message;
        }
    }
    [MyAttr("This is Attribute test.")]
    public class Program
    {
        public static void Main(string[] args)
        {
            Type type = typeof(Program);
            object[] arr = type.GetCustomAttributes(typeof(MyAttrAttribute), true);
            if (arr.Length == 0)
            {
                Console.WriteLine("This class has no custom attrs.");
            }
            else
            {
                MyAttrAttribute ma = (MyAttrAttribute)arr[0];
                Console.WriteLine(ma.Message);
            }
        }
    }
}

ํ•ต์‹ฌ์€ ์ด๋ ‡๋‹ค.

 

1) ์‚ฌ์šฉ์ž ์ •์˜ ์• ํŠธ๋ฆฌ๋ทฐํŠธ๋Š” Attribute๋ฅผ ์ƒ์†๋ฐ›์•„์•ผ ํ•œ๋‹ค.

2) type.GetCustomAttribute๋ฉ”์†Œ๋“œ๋Š” ์• ํŠธ๋ฆฌ๋ทฐํŠธ ๊ฐ์ฒด๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ์ด๋‹ค.