posted by 방랑군 2009. 9. 24. 17:25



using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;


using System.Reflection;


namespace Console_AOP

{

    class Program

    {

        static void Main(string[] args)

        {   


            /// string으로  객체 타입을 얻는다

            /// - typeName :System.Type.AssemblyQualifiedName

            ///

            Type atype = Type.GetType("Console_AOP.TEST, Console_AOP");

            // == Type atype = Type.GetType("Console_AOP.TEST");


            object a = System.Activator.CreateInstance(atype);


            /// 1. CASTING

            /// 

            TEST test = (TEST)a;

            string strAddress = test.GetAddress2(2);


            Console.WriteLine(strAddress);


            /// 2. INVOKE

            /// 

            // 메소드 : A.GetAddress()를 호출한다.

            MethodInfo mi = atype.GetMethod("GetAddress");

            strAddress = (string) mi.Invoke(a, null);


            Console.WriteLine(strAddress);


            MethodInfo mi2 = atype.GetMethod("GetAddress2");

            strAddress = (string)mi2.Invoke(a, new object[] {1234});


            Console.WriteLine(strAddress);


            // 프로퍼티 : A. HP의 값을 호출한다.

            PropertyInfo pi = atype.GetProperty("HP");

            strAddress = (string)pi.GetValue(a, null);


            Console.WriteLine(strAddress);

        }

    }



    class TEST

    {

        // 필드

        public string _Name;


        // 프로퍼티

        public string HP

        {

            get { return _HP; }

            set { _HP = value; }

        }

        private string _HP;


        // 메소드

        public TEST()

        {

            _Name = "황승재";

            _HP = "016-647-0678";

        }


        public string GetAddress()

        {

            return "서울시 염창동 동아3차아파트";

        }


        public string GetAddress2(int _NO)

        {

            return "서울시 양천구 목2동 537-20";

        }

    }

}






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

string 으로 동적 참조 2 & System.Activator.CreateInstance  (0) 2009.09.24
posted by 방랑군 2009. 9. 24. 17:02



using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;


namespace Console_AOP

{

    class Program

    {

        static void Main(string[] args)

        {   


            /// string으로  객체 타입을 얻는다

            /// - typeName :System.Type.AssemblyQualifiedName

            ///

            Type atype = Type.GetType("Console_AOP.TEST, Console_AOP");

            // == Type atype = Type.GetType("Console_AOP.TEST");


            object a = System.Activator.CreateInstance(atype);


            TEST test = (TEST)a;


            Console.Write(test.GetAddress(10));

        }

    }



    class TEST

    {


        public string _Name;

        public string _HP;


        public string GetAddress(int _NO)

        {

            return "서울시 양천구 목2동 537-20";

        }

    }

}



결과 : 
서울시 양천구 목2동 537-20