Reflection – getting info about type during execution.
1 |
using System.Reflection; |
#1 Need instance and info about type, before getting type
1 2 3 4 |
SportsCar viper = new SportsCar("Viper",240,40); Type t = viper.GetType(); Console.WriteLine("Type of viper is: "+t); Console.ReadLine(); |
#2 Need info about type before getting type
1 2 |
SportsCar viper = new SportsCar("Viper",240,40); Type t = typeof(SportsCar); |
#3 in current assembly with string param
1 2 |
//in current assembly Type t = Type.GetType("CarLibrary.SportsCar",false,true); |
#4 in external assembly GAC
1 2 |
//in external assembly GAC Type t = Type.GetType("CarLibrary.SportsCar,CarLibrary"); |
#5 for generic types
1 2 3 |
Type t = Type.GetType("System.Collections.Generic.List`1", false, true); Console.WriteLine(t); Console.ReadLine(); |
1 2 3 |
Type t = Type.GetType("System.Collections.Generic.Dictionary`2", false, true); Console.WriteLine(t); Console.ReadLine(); |
Reflection of methods, fields, properties,
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Reflection; namespace ReflectionExample { class Program { static void Main(string[] args) { Console.WriteLine("***Welcome to MyTypeViewer"); string typeName = ""; do { Console.WriteLine("\nEnter a type name to evaluate"); Console.WriteLine("Or Enter Q to quit"); typeName = Console.ReadLine(); if (typeName == "Q") { break; } try { Type t = Type.GetType(typeName); Console.WriteLine(""); ListVariousStats(t); ListFieldsLinq(t); ListPropertiesLinq(t); ListMethodsLinqOnlyNames(t); ListInterfacesLinq(t); } catch { Console.WriteLine("Sorry, can't find type"); } } while (true); } //--- reflection of methods with paramInfo static void ListMethods(Type t) { Console.WriteLine("***ListMethods***"); MethodInfo[] mi = t.GetMethods(); foreach (MethodInfo m in mi) //Console.WriteLine("->{0}", m.Name); { string retVal = m.ReturnType.FullName; string paramInfo = "("; foreach (ParameterInfo pi in m.GetParameters()) { paramInfo += string.Format("{0} {1}",pi.ParameterType,pi.Name); } paramInfo += ")"; Console.WriteLine("{0} {1} {2}", retVal,m.Name, paramInfo); } Console.WriteLine(); } static void ListMethodsLinq(Type t) { Console.WriteLine("***ListMethods***"); MethodInfo[] mi = t.GetMethods(); // << for what? var subset = from n in t.GetMethods() select n; foreach (var name in subset) Console.WriteLine("->{0}", name); Console.WriteLine(); } static void ListMethodsLinqOnlyNames(Type t) { Console.WriteLine("***ListMethods***"); var subset = from n in t.GetMethods() select n.Name; foreach (var name in subset) Console.WriteLine("->{0}",name); Console.WriteLine(); } //--- reflection of fields static void ListFieldsLinq(Type t) { Console.WriteLine("***ListFields***"); var subset = from f in t.GetFields() select f.Name; foreach (var name in subset) Console.WriteLine("->{0}", name); Console.WriteLine(); } //--- reflection of properties static void ListPropertiesLinq(Type t) { Console.WriteLine("***ListProperties***"); var subset = from p in t.GetProperties() select p.Name; foreach (var name in subset) Console.WriteLine("->{0}", name); Console.WriteLine(); } //--- reflection of interfaces static void ListInterfacesLinq(Type t) { Console.WriteLine("***ListInterfaces***"); var subset = from i in t.GetInterfaces() select i; // foreach (var name in subset) Console.WriteLine("->{0}", name); foreach (var i in subset) Console.WriteLine("->", (i as Type).Name); Console.WriteLine(); } static void ListVariousStats(Type t) { Console.WriteLine("***Various stats***"); Console.WriteLine("Base class is: {0}",t.BaseType); Console.WriteLine("Is type abstract?: {0}", t.IsAbstract); Console.WriteLine("Is sealed: {0}", t.IsSealed); Console.WriteLine("Is generic: {0}", t.IsGenericTypeDefinition); Console.WriteLine("Is type a class type: {0}", t.IsClass); Console.WriteLine(); } } } |