360 likes | 368 Views
Learn about Reflection in C#, its uses, and benefits for advanced object-oriented programming. Explore the Reflection API in C# and gain the ability to access metadata, obtain and manipulate fields, constructors, methods, and attributes of objects.
E N D
Reflection C# OOP Advanced SoftUni Team Technical Trainers Software University http://softuni.bg
Table of Contents • What? Why? Where? • Reflection API • Type Class • Reflecting Fields • Reflecting Constructors • Reflecting Methods • Reflecting Attributes
Questions sli.do#7573
What is Metaprogramming? Writing programs that treat other programs as their data
What is Reflection? The ability of a programming language to be its own metalanguage
Why Reflection? Code is easier to maintain and extend
Where is Reflection used? Writing tools for programmers
Type Class Primary way to access metadata
Type Class – How to obtain it? Compile time: Type myType = typeof(ClassName); Run time: Type myType = Type.GetType("Namespace.ClassName")
Name of a Type type.Name vs type.FullName
Base Type of a Class and Interfaces Type baseType = testClass.BaseType; Type[] interfaces = testClass.GetInterfaces();
New Instance Type sbType = Type.GetType("System.Text.StringBuilder"); StringBuilder sbInstance = (StringBuilder) Activator.CreateInstance(sbType);
Obtaining Fields of an Class FieldInfo[] publicFields = type.GetFields(); FieldInfo[] nonPublicFields = type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic); FieldInfo[] staticPublicFields = type.GetFields(BindingFlags.Static | BindingFlags.Public);
Field Type and Name FieldInfo field = type.GetField("fieldName"); string fieldName = field.Name; Type fieldType = field.FieldType; Useful when creating columns of the type in databasetables
Field Altering Type testType = typeof(Test); Test testObj = (Test) Activator.CreateInstance(testType); FieldInfo field = testType.GetField("testInt"); field.SetValue(testObj, 5); int fieldValue = (int) field.GetValue(testObj); WARNING: use with extreme caution as you could alter an objects internal state!
Modifiers field.IsPrivate //private field.IsPublic //public field.IsNonPublic //everything but public field.IsFamily //protected field.IsAssembly //internal etc… These properties helps to get modifier of a field
Obtaining Constructors of an Object ConstructorInfo[] publicCtors = type.GetConstructors(); ConstructorInfo[] nonPublicCtors = type.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic); ConstructorInfo[] staticPublicCtors = type.GetConstructors(BindingFlags.Static | BindingFlags.Public);
Obtaining a Certain Constructor ConstructorInfo constructor = type.GetConstructor(Type[] parametersType);
Instantiating Objects StringBuilder builder = (StringBuilder)constructor.Invoke(object[] params); Supply an array of object parameters for each argument in the constructor you are invoking
Constructor Parameters Type[] parameterTypes = constructor.GetParameters();
Obtaining Methods of an Object MethodInfo[] publicMethods = type.GetMethods(); MethodInfo[] nonPublicMethods = type.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic); MethodInfo[] staticPublicMethods = type.GetMethods(BindingFlags.Static | BindingFlags.Public);
Obtaining а Certain Method of an Object Type sbType = typeof(StringBuilder); Type[] methodArgumentsType = new Type[] { typeof(string) }; MethodInfo appendMethod = sbTyp.GetMethod("Append", methodArgumentsType); Types of arguments for that method if it has Method name
Invoking Methods object[] parameters = new object[] { "hi!" }; appendMethod.Invoke(builder, parameters); Target object instance for which to invoke the method Parameters for the method
Method Parameters and Return Type ParameterInfo[] appendParameters = appendMethod.GetParameters(); Type returnType = appendMethod.ReturnType;
Obtaining Attributes var attributes = type.GetCustomAttributes(); foreach(Attribute attribute in attributes) { //do something with attribute }
Obtaining Method/Field Attributes var attributes = field/method.GetCustomAttributes(); foreach(Attribute attribute in attributes) { //do something with attribute }
Summary What are the: • Benefits • Drawbacks Of using reflection?
Reflection https://softuni.bg/csharp-advanced-oop
License This course (slides, examples, demos, videos, homework, etc.)is licensed under the "Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International" license • Attribution: this work may contain portions from • "Fundamentals of Computer Programming with C#" book by Svetlin Nakov & Co. under CC-BY-SA license • "OOP" course by Telerik Academy under CC-BY-NC-SA license
Free Trainings @ Software University • Software University Foundation – softuni.org • Software University – High-Quality Education, Profession and Job for Software Developers • softuni.bg • Software University @ Facebook • facebook.com/SoftwareUniversity • Software University @ YouTube • youtube.com/SoftwareUniversity • Software University Forums – forum.softuni.bg