1 / 36

Reflection

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.

patb
Download Presentation

Reflection

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Reflection C# OOP Advanced SoftUni Team Technical Trainers Software University http://softuni.bg

  2. Table of Contents • What? Why? Where? • Reflection API • Type Class • Reflecting Fields • Reflecting Constructors • Reflecting Methods • Reflecting Attributes

  3. Questions sli.do#7573

  4. What? Why? Where?

  5. What is Metaprogramming? Writing programs that treat other programs as their data

  6. What is Reflection? The ability of a programming language to be its own metalanguage

  7. Why Reflection? Code is easier to maintain and extend

  8. Where is Reflection used? Writing tools for programmers

  9. Reflection API

  10. Type Class Primary way to access metadata

  11. Type Class – How to obtain it? Compile time: Type myType = typeof(ClassName); Run time: Type myType = Type.GetType("Namespace.ClassName")

  12. Name of a Type type.Name vs type.FullName

  13. Base Type of a Class and Interfaces Type baseType = testClass.BaseType; Type[] interfaces = testClass.GetInterfaces();

  14. New Instance Type sbType = Type.GetType("System.Text.StringBuilder"); StringBuilder sbInstance = (StringBuilder) Activator.CreateInstance(sbType);

  15. Demo

  16. 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);

  17. 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

  18. 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!

  19. 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

  20. Exercises in Class

  21. 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);

  22. Obtaining a Certain Constructor ConstructorInfo constructor = type.GetConstructor(Type[] parametersType);

  23. Instantiating Objects StringBuilder builder = (StringBuilder)constructor.Invoke(object[] params); Supply an array of object parameters for each argument in the constructor you are invoking

  24. Constructor Parameters Type[] parameterTypes = constructor.GetParameters();

  25. 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);

  26. 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

  27. Invoking Methods object[] parameters = new object[] { "hi!" }; appendMethod.Invoke(builder, parameters); Target object instance for which to invoke the method Parameters for the method

  28. Method Parameters and Return Type ParameterInfo[] appendParameters = appendMethod.GetParameters(); Type returnType = appendMethod.ReturnType;

  29. Exercises in Class

  30. Obtaining Attributes var attributes = type.GetCustomAttributes(); foreach(Attribute attribute in attributes) { //do something with attribute }

  31. Obtaining Method/Field Attributes var attributes = field/method.GetCustomAttributes(); foreach(Attribute attribute in attributes) { //do something with attribute }

  32. Exercises in Class

  33. Summary What are the: • Benefits • Drawbacks Of using reflection?

  34. Reflection https://softuni.bg/csharp-advanced-oop

  35. 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

  36. 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

More Related