리플렉션 예제
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | using System; using System.Reflection; namespace EX12_Reflection { class General { public int exp; public string Name { get; set; } public int Power { get; set; } public int HP { get; set; } public General() { exp = 0; } public General(string name, int power, int hp) : this() { Name = name; Power = power; HP = hp; } public void Print() { Console.WriteLine("장수명 : {0} POW: {1} HP : {2} Exp : {3}", Name, Power, HP, exp); } } class MainClass { public static void Main (string[] args) { Type type = Type.GetType("EX12_Reflection.General"); // Query Class Info Console.WriteLine("***** Field List *****"); FieldInfo[] fList = type.GetFields(); foreach (FieldInfo f in fList) { Console.WriteLine(f); } Console.WriteLine("***** Property List *****"); PropertyInfo[] pList = type.GetProperties (); foreach (PropertyInfo p in pList) { Console.WriteLine(p); } Console.WriteLine("***** Method List *****"); MethodInfo[] mList = type.GetMethods (); foreach (MethodInfo m in mList) { Console.WriteLine(m); } // Generate New Instance Console.WriteLine("***** Generate New Instance *****"); MethodInfo mdInfo = type.GetMethod("Print"); PropertyInfo nameProp = type.GetProperty("Name"); PropertyInfo powerProp = type.GetProperty("Power"); PropertyInfo hpProp = type.GetProperty("HP"); object general = Activator.CreateInstance(type, "관우,", 99, 100); mdInfo.Invoke(general, null); general = Activator.CreateInstance(type); nameProp.SetValue(general,"여포",null); powerProp.SetValue(general, 100, null); hpProp.SetValue(general, 100, null); mdInfo.Invoke(general, null); } } } | cs |
'Programming > C#' 카테고리의 다른 글
[C# 프로그래밍] 14장 파일입출력 (0) | 2016.01.11 |
---|---|
[C# 프로그래밍] 13장 애트리뷰트 (0) | 2016.01.11 |
[C# 프로그래밍] 11장 람다식 (0) | 2016.01.11 |
[C# 프로그래밍] 10장 LINQ (0) | 2016.01.11 |
[C# 프로그래밍] 9장 델리게이트와 이벤트 (0) | 2016.01.11 |