๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ–ฅ๏ธ/C#

[C#] ๋ฆฌํ”Œ๋ ‰์…˜(Reflection): ๋™์  ์ธ์Šคํ„ด์Šค ์ƒ์„ฑํ•˜๊ธฐ

by HanaV 2023. 10. 10.
728x90
 
 

Reflection

 

๋ฆฌํ”Œ๋ ‰์…˜์„ ์ด์šฉํ•ด์„œ ๋™์  ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์„ ์–ธํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

object a = Activator.CreateInstance(Type);

 

์ด๋ฒˆ์—๋„ Inventory๋ผ๋Š” ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด์„œ Inventory ์ธ์Šคํ„ด์Šค๋ฅผ ๋™์ ์œผ๋กœ ์ƒ์„ฑํ•ด๋ณด์ž.

class Inventory
{
    private string product;
    private int count;
    public Inventory()
    {
        product = "";
        count = 0;
    }
    public Inventory(string product, int count)
    {
        this.product = product;
        this.count = count;
    }
    public void Print()
    {
        Console.WriteLine($"{product}: {count}");
    }
    public string Product
    {
        get { return product; }
        set { product = value; }
    }
    public int Count
    {
        get { return count; }
        set { count = value; }
    }
}

Inventory ํด๋ž˜์Šค ์•ˆ์—๋Š” product, count ํ•„๋“œ, Print๋ผ๋Š” ๋ฉ”์„œ๋“œ, Product, Count ํ”„๋กœํผํ‹ฐ๊ฐ€ ์žˆ๋‹ค.

์ด ๋•Œ, ๋™์  ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๊ฐ’์„ setํ•ด์ฃผ๋Š” ๋ฐฉ๋ฒ•์€ ๋‘ ๊ฐ€์ง€ ๋ชจ๋‘ ๊ฐ™๋‹ค.

object inventory = Activator.CreateInstance(type, "Grape", 12); // ์ƒ์„ฑ์ž์— "Grape"์™€ 12๋ฅผ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•˜์—ฌ ์ธ์Šคํ„ด์Šค๋ฅผ ์ดˆ๊ธฐํ™”

inventory = Activator.CreateInstance(type); // ์ธ์ˆ˜ ์—†์ด ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑ
productProperty.SetValue(inventory, "Pear", null); // null์€ ๋ฐฐ์—ด๊ณผ ๊ฐ™์€ ๊ฒƒ์„ ๋‹ค๋ฃฐ ๋•Œ index๋ฅผ ๋„ฃ๋Š” ๊ณณ
countProperty.SetValue(inventory, 3, null);

 

์ด์ œ ์ธ์Šคํ„ด์Šค ํƒ€์ž…์„ ๋™์ ์œผ๋กœ ์ƒ์„ฑํ•ด๋ณด์ž.

์ •์  ์ธ์Šคํ„ด์Šค์™€ ์ฐจ๋ณ„์„ ๋‘๊ธฐ ์œ„ํ•ด ์ผ๋ถ€๋กœ instanceType์ด ๋ฐ”๋€Œ๋„๋ก ํ•ด๋†“์•˜๋‹ค.

static void Main(string[] args)
{
    for (int i = 0; i < 4; i++) {
        string instanceType = (i % 2 == 0) ? "ReflectionPractice.Inventory" : "NothingHere";
		
        Type type = Type.GetType(instanceType);
        // NothingHere์ธ ๊ฒฝ์šฐ์—๋Š” type์— null์ด ๋‹ด๊น€

		// instanceType์ด ReflectionPractice.Inventory์ธ ๊ฒฝ์šฐ์—๋งŒ ์ž‘์—… ์‹คํ–‰
        if (type == typeof(ReflectionPractice.Inventory)) {
            MethodInfo methodInfo = type.GetMethod("Print");
            PropertyInfo productProperty = type.GetProperty("Product");
            PropertyInfo countProperty = type.GetProperty("Count");
			
            // ๋™์  ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ ํ›„ ์ดˆ๊ธฐํ™” ๋ฐฉ๋ฒ• 1
            object inventory = Activator.CreateInstance(type, "Grape", 12);
            methodInfo.Invoke(inventory, null);
            
            // ๋™์  ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ ํ›„ ์ดˆ๊ธฐํ™” ๋ฐฉ๋ฒ• 2
            inventory = Activator.CreateInstance(type);
            productProperty.SetValue(inventory, "Pear", null);
            countProperty.SetValue(inventory, 3, null);

            Console.WriteLine($"{productProperty.GetValue(inventory, null)}: {countProperty.GetValue(inventory, null)}");
        }
        else {
            Console.WriteLine("^__^");
        }
    }

}
Grape: 12
Pear: 3
^__^
Grape: 12
Pear: 3
^__^

 

์ „์ฒด ์ฝ”๋“œ๋Š” Github์— ์˜ฌ๋ ค๋†จ์Šต๋‹ˆ๋‹ค

728x90

"); wcs_do();