리플렉션은 코드에서 유형, 메서드 및 필드의 메타데이터를 설명하는 프로세스입니다. 네임스페이스 System.Reflection을 사용하면 로드된 어셈블리, 클래스, 메서드 및 값 형식과 같은 어셈블리 내의 요소에 대한 데이터를 얻을 수 있습니다. System.Reflection에는 수많은 클래스가 있지만 가장 일반적으로 사용되는 클래스는 Assembly, AssemblyName, ConstructorInfo, MethodInfo, ParameterInfo, EventInfo, PropertyInfo 및 MemberInfo입니다.
예시
static void Main(string[] args){ TypeInfo myType = typeof(TextInfo).GetTypeInfo(); IEnumerable<PropertyInfo> properties = myType.DeclaredProperties; IEnumerable<MethodInfo> methods = myType.DeclaredMethods; Console.WriteLine(myType); Console.WriteLine(properties); Console.WriteLine(methods); StringBuilder strBuilder = new StringBuilder(); Console.WriteLine(); strBuilder.Append("The properties are:"); foreach (PropertyInfo p in properties){ strBuilder.Append("\n" + p.Name); } strBuilder.Append("\n"); strBuilder.Append("\nThe methods are:"); foreach (MethodInfo m in methods){ strBuilder.Append("\n" + m.Name); } Console.WriteLine(strBuilder); }
출력
InvariantANSICodePageOEMCodePageMacCodePageEBCDICCodePageLCIDCultureNameIsReadOnlyListSeparatorIsAsciiCasingSameAsInvariantIsRightToLeftThe 방법은 :get_Invariantget_ANSICodePageget_OEMCodePageget_MacCodePageget_EBCDICCodePageget_LCIDget_CultureNameget_IsReadOnlyget_ListSeparatorset_ListSeparatorget_IsAsciiCasingSameAsInvariantget_IsRightToLeftSystem.Runtime.Serialization.IDeserializationCallback.OnDeserializationCloneReadOnlyVerifyWritableSetReadOnlyStateToLowerToLowerChangeCaseChangeCaseToLowerChangeCaseToUpperChangeCaseCommonChangeCaseCommonChangeCaseCommonToLowerAsciiInvariantToLowerAsciiInvariantToUpperAsciiInvariantToUpperAsciiInvariantToLowerAsciiInvariantToUpperToUpperToUpperAsciiInvariantIsAsciiPopulateIsAsciiCasingSameAsInvariantEqualsGetHashCodeToStringToTitleCaseAddNonLetterAddTitlecaseLetterIsWordSeparatorIsLetterCategoryFinishInitializationChangeCaseIsInva System.Globalization.TextInfoSystem.Reflection.PropertyInfo [] System.Reflection.MethodInfo [] 속성은System.Globalization.TextInfo System.Reflection.PropertyInfo[] System.Reflection.MethodInfo[] The properties are: Invariant ANSICodePage OEMCodePage MacCodePage EBCDICCodePage LCID CultureName IsReadOnly ListSeparator IsAsciiCasingSameAsInvariant IsRightToLeft The methods are: get_Invariant get_ANSICodePage get_OEMCodePage get_MacCodePage get_EBCDICCodePage get_LCID get_CultureName get_IsReadOnly get_ListSeparator set_ListSeparator get_IsAsciiCasingSameAsInvariant get_IsRightToLeft System.Runtime.Serialization.IDeserializationCallback.OnDeserialization Clone ReadOnly VerifyWritable SetReadOnlyState ToLower ToLower ChangeCase ChangeCaseToLower ChangeCaseToUpper ChangeCaseCommon ChangeCaseCommon ChangeCaseCommon ToLowerAsciiInvariant ToLowerAsciiInvariant ToUpperAsciiInvariant ToUpperAsciiInvariant ToLowerAsciiInvariant ToUpper ToUpper ToUpperAsciiInvariant IsAscii PopulateIsAsciiCasingSameAsInvariant Equals GetHashCode ToString ToTitleCase AddNonLetter AddTitlecaseLetter IsWordSeparator IsLetterCategory FinishInitialization ChangeCase IsInvariantLocale