1 00:00:07,260 --> 00:00:11,820 So in this video, we're going to talk about topics 16. 2 00:00:12,120 --> 00:00:13,680 What is reflection? 3 00:00:14,130 --> 00:00:20,950 Reflection is a mechanism that allows us to write code that can inspect types used in the application. 4 00:00:21,300 --> 00:00:28,530 For example, using reflection, you can list all fields and their values belonging to a given object, 5 00:00:28,530 --> 00:00:30,360 even if at compile time. 6 00:00:30,480 --> 00:00:33,480 We don't know what this object type is exactly. 7 00:00:34,080 --> 00:00:40,200 This probably sounds a bit mysterious to you, so let's consider the following use case We wanted to 8 00:00:40,200 --> 00:00:45,180 write a class that can take away the objects and save them to a text file. 9 00:00:45,660 --> 00:00:50,030 Of course, they are already mechanisms that do it and still objects. 10 00:00:50,040 --> 00:00:56,970 Ask Jason or Azimo, but let's say we want some custom format, so we must implement it ourselves. 11 00:00:57,300 --> 00:01:02,130 We want this class to be completely generic so it can take any type of object. 12 00:01:02,610 --> 00:01:07,230 And here we have two simple types that this class would convert to text. 13 00:01:07,620 --> 00:01:12,930 For brevity, I define them as records, but we'll learn about later in the course. 14 00:01:13,410 --> 00:01:18,750 If an object is being converted, I would like the result to be something like this. 15 00:01:19,240 --> 00:01:22,470 Similarly, for a house, I would like to have this. 16 00:01:23,130 --> 00:01:28,590 The problem is that in the convert method, we have no idea what type we deal with. 17 00:01:29,040 --> 00:01:33,630 We can't cast the parameter to anything concrete as the type may vary. 18 00:01:34,110 --> 00:01:40,350 Also, to implement what you want, we will not only need the values of the properties which we could 19 00:01:40,350 --> 00:01:46,920 access if we only had more concrete type than system object, but we also need their names, which is 20 00:01:46,920 --> 00:01:48,390 not available at runtime. 21 00:01:48,630 --> 00:01:56,040 In other words, when using House First Property, we can get the number two, but we can't get the 22 00:01:56,040 --> 00:01:59,910 floor string and we will need it to build the string result. 23 00:02:00,510 --> 00:02:05,940 Well, actually, we can get the name of the property, but only if we use reflection. 24 00:02:06,330 --> 00:02:11,040 Reflection allows us to access information about some type at one time. 25 00:02:11,430 --> 00:02:15,990 We can not only access the values of some fields, but also their names. 26 00:02:16,410 --> 00:02:22,920 Moreover, we could access information about methods, constructors, access modifiers and so on. 27 00:02:23,490 --> 00:02:24,990 Let's see this in practice. 28 00:02:25,470 --> 00:02:30,180 First of all, we will use the grid type method on this parameter. 29 00:02:30,540 --> 00:02:35,550 It will return a type object which provides or information about the type. 30 00:02:40,870 --> 00:02:44,290 Let's run this code and see the type object in the debugger. 31 00:03:03,030 --> 00:03:06,250 As you can see, there is quite a lot of data in here. 32 00:03:06,660 --> 00:03:13,530 We have some information about constructor's methods, base type and also properties. 33 00:03:13,920 --> 00:03:20,610 We can see all properties we declared in the house type and also on extra equality contract property, 34 00:03:20,730 --> 00:03:23,040 which is auto generated for records. 35 00:03:23,280 --> 00:03:26,610 Well, ignore it when converting the object to streak. 36 00:03:27,150 --> 00:03:27,840 All right. 37 00:03:28,560 --> 00:03:31,290 Let's use this data to achieve what you want. 38 00:03:31,590 --> 00:03:37,350 First, I want to read all properties from this object, except the quality contract. 39 00:03:46,620 --> 00:03:50,070 This gives me an eye on the morale of property info. 40 00:03:50,490 --> 00:03:54,330 Now I want to build a strength for each property ethos. 41 00:03:54,570 --> 00:04:00,480 Accessing the property name as well as its value and then doing the strings together. 42 00:04:00,870 --> 00:04:02,700 I will use Link to know it. 43 00:04:03,060 --> 00:04:05,790 If you don't know Link, I have a course about that. 44 00:04:06,060 --> 00:04:09,180 See the last section in this course to find out more. 45 00:04:25,710 --> 00:04:31,110 The select committee it comes from Link and it simply maps every property to a string. 46 00:04:31,560 --> 00:04:35,400 I join those things together using this remodelling method. 47 00:04:36,000 --> 00:04:37,980 Let's make sure this method works. 48 00:04:41,100 --> 00:04:41,700 Right. 49 00:04:42,120 --> 00:04:43,660 Seems everything is working. 50 00:04:43,920 --> 00:04:50,540 We're used to perfection to access the information about some type at one time and read the values and 51 00:04:50,550 --> 00:04:52,260 names of its properties. 52 00:04:53,220 --> 00:04:55,790 Reflection gives us much more ability. 53 00:04:56,340 --> 00:04:57,660 Here are some of them. 54 00:04:58,170 --> 00:05:06,030 Loading dials at one time and using types defined in them, instantiating a new instance of some object 55 00:05:06,030 --> 00:05:08,310 of a specific type at runtime. 56 00:05:08,520 --> 00:05:16,140 For example, we can create an object of a type defined in the yellow that we hold, finding all process 57 00:05:16,140 --> 00:05:22,710 derived from a specific type or implementing a specific interface reading the attributes. 58 00:05:22,980 --> 00:05:27,090 This is, for example, what an unitard does when it runs the tests. 59 00:05:27,240 --> 00:05:31,140 It finds Olmer to which the test attributes and executes them. 60 00:05:31,450 --> 00:05:34,500 We'll learn more about attributes in the next lecture. 61 00:05:35,010 --> 00:05:41,550 Running a method by its name, for example, if the user of the application selected it from some dropdown 62 00:05:42,120 --> 00:05:42,930 debugging. 63 00:05:43,110 --> 00:05:50,280 For example, sometimes it is necessary to find out the list of all currently loaded assemblies creating 64 00:05:50,280 --> 00:05:56,640 new types at runtime system reflection image namespace is used for that, and many more. 65 00:05:57,060 --> 00:06:02,250 As you can see, a reflection is a powerful tool, but a search should be used with caution. 66 00:06:02,730 --> 00:06:08,820 The code that heavily relies on reflection is usually hard to maintain and understand. 67 00:06:09,180 --> 00:06:11,040 It's also prone to errors. 68 00:06:11,370 --> 00:06:16,980 For example, when you call a method by its name, but someone changes the name without your knowledge. 69 00:06:17,250 --> 00:06:22,710 The code will crash the next time it's wrong because no method with this name exists anymore. 70 00:06:23,220 --> 00:06:30,580 Also, using reflection has a relatively big impact on performance at one of the projects I worked on. 71 00:06:30,600 --> 00:06:33,840 I was asked to improve the performance of some process. 72 00:06:34,140 --> 00:06:39,690 This application was using réflexion, I wrote, mostly to build some types and attributes from the 73 00:06:39,690 --> 00:06:41,130 others at runtime. 74 00:06:41,580 --> 00:06:47,970 It turned out that the results of those load can be cached, and only this improvement make the process 75 00:06:47,970 --> 00:06:49,950 work twice as fast as before. 76 00:06:50,310 --> 00:06:57,150 We'll learn more about this mechanism in the whole discussion lecture, so use reflection with caution. 77 00:06:57,510 --> 00:07:02,370 If there is a convenient way of implementing the same logic without it, go for it. 78 00:07:02,610 --> 00:07:07,260 If not, reflection, maybe a lifesaver, but keep an eye on the performance. 79 00:07:07,920 --> 00:07:09,030 Let's summarize. 80 00:07:09,330 --> 00:07:15,960 Reflection is a mechanism that allows us to write code that can inspect types used in the application. 81 00:07:16,350 --> 00:07:22,830 For example, call a method with the gnome equal to given string or list or fields and their values 82 00:07:22,830 --> 00:07:24,630 belonging to some object. 83 00:07:24,930 --> 00:07:28,700 The topic of reflection is quite common during the interviews. 84 00:07:29,040 --> 00:07:34,620 If it comes up, there is a big chance you will be asked What are the downsides of using reflection? 85 00:07:35,340 --> 00:07:38,490 Well, it was a relatively big impact on performance. 86 00:07:38,940 --> 00:07:45,300 Also, it sometimes makes the cold hard to understand, and then it may also tempt some programmers 87 00:07:45,300 --> 00:07:51,660 to hack some code, for example, to access private fields at runtime, which may lead to unexpected 88 00:07:51,660 --> 00:07:53,850 results and hard to understand bugs. 89 00:07:54,570 --> 00:07:56,700 All right, that's it for this video. 90 00:07:57,100 --> 00:08:02,280 We'll continue the topic of reflection in the next lecture, where we will talk about attributes.