[C#] Reflection 리플렉션은 ..좀 생소하다 ㅋㅋ 아직은 와닿지 않네


using System.Reflection;   // 이넘을 추가해줘야 밑의 code 예제를 해볼수 있어여~~~



class MainApp
    {
        static void PrintInterfaces(Type type)
        {
            Console.WriteLine ("---------Interfaces ---------");

            Type[] interfaces = type.GetInterfaces();
            foreach (Type i in interfaces)
                Console.WriteLine("Name :{0}", i.Name);
        Console.WriteLine();
        }

        static void PrintFields(Type type )    // method()
        {
            Console.WriteLine("------------Fields -----------");

            FieldInfo[] fields = type.GetFields(
                BindingFlags.NonPublic |
                BindingFlags.Public |
                BindingFlags.Static |
                BindingFlags.Instance );
            foreach (FieldInfo field in fields)
            {
                String accessLevel = "protected";
                if (field.IsPublic) accessLevel = "public";
                else if (field.IsPrivate) accessLevel = "private";

                Console.WriteLine("Access: {0}, Type:{1}, Name:{2}", accessLevel, field.FieldType.Name, field.Name);

            }
            Console.WriteLine();

        }
        static void PrintMethods(Type type)        // method()
        {
            Console.WriteLine(" ------Methods-----");

            MethodInfo[] methods = type.GetMethods();
            foreach (MethodInfo method in methods)
            {
                Console.Write("Type: {0}, Name : {1}, Parameter: ", method.ReturnType.Name, method.Name);
                ParameterInfo[] args = method.GetParameters();
                for (int i=0 ; i < args.Length; i++)
                {
                    Console.Write("{0}", args[i].ParameterType.Name);
                    if (i < args.Length - 1)
                        Console.Write(",");
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }

        static void PrintProperties(Type type)         // method()
        {
            Console.WriteLine("----------- Properties --------------");

            PropertyInfo[] properties = type.GetProperties();
            foreach (PropertyInfo property in properties)
                Console.WriteLine("Type:{0}, Name : {1}", property.PropertyType.Name, property.Name);
             
            Console.WriteLine();


        }

     
        static void Main(string[] args)         // Main method()
        {
            int a = 0;
            Type type = a.GetType();

            PrintInterfaces(type);
            PrintFields(type);
            PrintProperties(type);
            PrintMethods(type);

        }
      
  }

일단 쪼개서 생각해보면 4개의 Method 별로~ 분석해보자
4개의 Method 모두 Type 인자인 type 을 parameter(매개변수) 로 받아서 일을 처리한다.

int 형식의 a 를 선언뒤 0 으로 초기화
Type 형식의 type을 선언뒤 int a 의 타입 값을 GetType() method로 type에 저장한다.

다음 4개의 Method 들이 이 type 값(value)를 가지고 일을 처리한다.

한국말이 더 어렵네... 그냥 영어로 쓸까...

어쨋든 첫번째 Method 를 실행 하는 코드만 보면

int a 의 type 이 int 니까, Type[] 인 interfaces 에 저장됫던 int의 모든 interface 들을 보여 준다.

사설 : 아... 내가 써놓고도 이말이 더 어렵다.. 코드는 간단한데.. 한국말로 옮기려니.. 더 짜증..
그래서 울나라 사람들이 코딩배우기가 힘든듯...


두번째 Field  얻는 Methods 는 더 이해가 쉽지 않다..ㅋㅋㅋ


댓글

이 블로그의 인기 게시물

[C# Winform 예제] ScreenSaver 스크린세이버 만들기

아 정말 이사람 IELTS 관한 비법 글은.. 정말 공감10000%

Vocabulary for IELTS (2000 words) advanced level.