Computer >> 컴퓨터 >  >> 프로그램 작성 >> C#

C#의 Type.GetNestedType() 메서드

<시간/>

C#의 Type.GetNestedType() 메서드는 현재 Type 내에서 특정 유형의 중첩을 가져오는 데 사용됩니다.

구문

다음은 구문입니다 -

public Type GetNestedType (string name);
public abstract Type GetNestedType (string name, System.Reflection.BindingFlags bindingAttr);

이제 Type.GetNestedType() 메서드를 구현하는 예를 살펴보겠습니다. -

using System;
public class Demo {
   public static void Main(){
      Type type1 = typeof(Subject);
      try {
         Type type2 = type1.GetNestedType("AdvSubject");
         Console.Write("NestedType = "+ type2);
      }
      catch (ArgumentNullException e){
         Console.Write("{0}", e.GetType(), e.Message);
      }
   }
}
public class Subject{
   public class BasicSubject {
      //
   }
   public class AdvSubject {
      //
   }
}

출력

이것은 다음과 같은 출력을 생성합니다 -

NestedType = Subject+AdvSubject

이제 Type.GetNestedType() 메서드를 구현하는 또 다른 예를 살펴보겠습니다. -

using System;
public class Demo {
   public static void Main(){
      Type type1 = typeof(Subject);
      try {
         Type type2 = type1.GetNestedType(null);
         Console.Write("NestedType = "+ type2);
      }
      catch (ArgumentNullException e){
         Console.Write("{0}", e.GetType(), e.Message);
      }
   }
}
public class Subject{
   public class BasicSubject {
      //
   }
   public class AdvSubject {
      //
   }
}

출력

이것은 다음과 같은 출력을 생성합니다 -

System.ArgumentNullException