typeof
- 실행중에 형식 정보를 얻을수 있는 연산자.
- System.Type를 얻는다.
- C#의 Reflection 기능 구현
C# 의 typeof() 연산자를 이용해서 Type 의 인스턴스를 얻을 수도 있다.
Code:
// typeof 를 이용해서 Type 을 가져온다.
Type t = typeof(Foo);
형식에 대한 System.Type 개체를 얻는 데 사용됩니다. typeof 식의 형식은 다음과 같습니다.
System.Type type = typeof(int);
typeof가 대상 타입을 System.Type의 정보로 얻어오는 것 같은데.
그래도 잘 모르겠다. System.Type은 뭘까?
Type 클래스
.NET Framework (current version)
클래스 형식, 인터페이스 형식, 배열 형식, 값 형식, 열거형 형식, 형식 매개 변수, 제네릭 형식 정의 및 개방형 생성 제네릭 형식이나 폐쇄형 생성 제네릭 형식에 대한 형식 선언을 나타냅니다.
이 형식에 대한 .NET Framework 소스 코드를 찾아보려면 참조 원본을 참조하세요.
네임스페이스: System어셈블리: mscorlib.dll의 mscorlib
namespace ConsoleApplication1
{
class Program
{
public class test
{
public string str { get; set; }
public int age { get; set; }
}
IEnumerable<test> tests;
public void init()
{
tests = new test[] {
new test {str = "aa", age = 33},
new test {str = "aas", age = 133},
new test {str = "aad", age = 233},
};
System.Type type = typeof(int);
Console.Write(type);
}
static void Main(string[] args)
{
test theTest = new test();
System.Type st = typeof(test[]);
// typeof는 대상을 system.Type으로 변환한다.
// system.Type은 여러 정보를 담게 된다. // 받았던 대상의 타입이 클래스인지, 배열인지? 텍스트 명칭부터, 정식으로 어느 소속인지 등..
//
}
}
}
결론적으로 돌려보니, '타입'을 집어넣으면, 해당 타입의 정보를 가진 객체를 반환하는 함수였다. 그 이름이나 소속, 배열 여부 , 클래스인지 등.. 다양한 정보를 담은 객체를 던진다.
댓글을 달아 주세요