BrandGhost

See It In Action! C# Reflection for Beginners

You've heard about reflection in C#, but in what situations are we expected to be using dotnet reflection over just... using types as we might expect to? Well, the C# typing system allows us to have a lot of information up-front at compile time, but there are circumstances where at runtime we have more information or want to find/understand our types. Check out this video for a tutorial on a simple use case for reflection in csharp!
View Transcript
if you're a beginner in C you've probably heard about reflection and maybe even seen some of it in the code you've worked in but you might be wondering why would I use reflection and what kinds of situations might it be valuable my name is Nick centino and I'm a principal software engineering manager at Microsoft in this video I'm going to give you a little bit of a contrived example but I want to be able to demonstrate to you that there's some situations where at runtime we have more information about what we want to be doing with types versus at compile time now with the typing system that we have in C a lot of what we're doing is forced into this compile time path where we need to know everything up front and that gives us a lot of type safety and makes it so that the compiler can handle a lot of potential errors for us but it would mean that we could be limited in some capacity when it comes to some runtime things that we want to do reflection can open some doors for us there I'm going to walk us through an example that allows us to examine some type information based on some user input if that sounds interesting just a quick reminder to check that pin comment for a link to my my newsletter and my courses on dome train and with that said let's jump into the code okay so I did mention that this is going to be a little bit contrived but what we are going to do is make a little console application that allows us to inspect the different types that we have available in our program so all of these classes that I have on screen right now are pretty silly they're pretty pointless but they're just here so that we have some variety to work with and that way we can go ask for different things and see the different outputs so I needed something to feed into the example here and going back to the Practical use case for this if I asked you at runtime that I wanted to be able to look up what's available for certain types of properties you can't possibly know that at compile time if I need to tell you that as a user at runtime so it's just one type of situation no pun intended where I might want to be able to look up some type of information using reflection if you stay to the end of this video I'll explain to you the situations I personally use reflection all of the time so these classes like I mentioned are really simple for the most part I just have a handful of properties declared they're between strings integers dates and times I just have some values on them as well and then I have this one that has a method on it as we go through this example we can try playing around with some things but I just wanted to show you that I have a handful of these classes that we could go inspect now I'm going to walk us through this example program and there's a lot of stuff on the screen right now and most of it is just console right line stuff so it's not that complicated I'll explain the uh reflection part there's a bunch of console right line just because that's what we're going to be showing to the end user when we go to run this and we will be running it and seeing what kind of information we get so just to kind of jump through it quickly what I'm going to do at the start is just ask for the assembly that we're currently running out of so that's assembly get executing assembly and the reason that I want to do that is because we're going to be asking the current assembly for the types from there I'm just going to start this while loop and what we're going to do is ask the user to input some type of substring that we can go match on to look up some types so all of those types that I declared at the bottom of this file that we were just looking at what we're able to do with this code is type some part of a class name that we want to look for and as long as we get a partial match and I'm making it case and sensitive just because why not if we get a partial match then we can print out all of the information for that type so the next part is that we're going to use Reflections we're going to ask that assembly for all of the types and technically I pulled out the assembly at the top here I could also take a snapshot of all of the types because between these runs so each iteration of the loop the list of types isn't going to change cool a tiny little optimization there so all of the types is now going to contain all of the types in our assembly from there I'm just using link to go find the types that match just part of that substring with case and sensitive comparison and I'm going to put them all into an array we'll print out how many we found but then this Loop afterwards is going to print out all of the information using reflection and again there's a lot of code but most of it's just for loops and console right lines but we're going to print out all all of the information that we can gather using reflection and I say all of the information there is more information that reflection can provide us so what I highly recommend is if you want to play around with something like this use the code that you see on the screen it's really quick to be able to just ask for the type information to get the constructors you don't need the rest of the code that I have here with all the loops and stuff so there's like I said a lot of code on screen but not a lot of it you need to be able to kind of Step through the debugger and play with things but there is more information that's available for the constructors I'm just going to print out the Constructor name and spoiler alert it's always going to be the same name it's just doct for Constructor and then I'm going to print out the parameters for the constructors we're going to print out method information right after so again you know method uh we're going to get the parameters out of that we're going to ask that particular type that we're looking at for all of the methods so this double for Loop is just going to be able to print out all those details we'll do a similar thing for fields and for properties as as well so the reflection pieces that are interesting right get properties get Fields get methods methods each have get parameters get Constructors Constructors have parameters right so these are all just the parts of reflection but they're pretty straightforward and pretty repeated across the different members that we have to work with like I said the rest of it is mostly console right line so let's go run this let's play around with that a little bit and see if we can tweak anything to get some extra details out that are useful to us so the first thing that our program does is asks us for a substring and I don't know if you remember all of the classes that I showed you at the beginning I didn't expect that you wrote them all down and that's okay one of them said Nick so if I put Nick in here there was Nick something I can't even remember what I wrote that's okay because that's why I picked a substring I only needed to remember part of the name but if we look for this one we can see what it finds so it found one type right one types that's fun to say out loud type zero so the first one that we find is Nick's cool class I should have remembered that that's the obvious choice right but the constructors that it has just the default Constructor so this single Constructor and I did mention that it's going to have a c name and that's because all Constructors have that name the methods that we can see are the do something method so that's the one method I defined on this type and the other ones are just kind of built in because every class inherits from an object so I didn't go write all of these right that's just built in there are no fields and there are no properties and then our program just kind of starts from the beginning what's another one that we want to look for I think I added two that had Dev leader in them and I did because there's a whole bunch of stuff that printed out I see Dev Leader Class 2 that means we should see Dev leader class here kind of the same thing right you can see that there's um properties now so this one's interesting because properties sorry it jumped a little bit but properties URL and count but look up here at the methods right there's a get URL and a get count and that's because properties are in fact methods it's just that it's like a syntactic sugar that we have in code that we treat properties as these other things that we don't have the parentheses to call them but truly they are methods with a get and a set in front of them and if we want to see what happens when there are no matches I mean look there's nothing that comes back found zero types so that's pretty simple but what would happen if we wanted to go find some stuff with private information on them right so let's go see if we can tweak this a little bit and make it such that we get some extra details all right so what I'm going to be doing is adding binding Flags onto all of these calls that we have for getting Constructors Fields properties and methods so these different members that we have access to and I've defined these binding flags up at the top so public and non-public this one's the magical one that's going to let us see hidden things that we should not be allowed to see but I'm also going to ask for instance and static so what we're going to want to do is jump back down to those types right after I update some of this code and then we're we're going to add some other things onto there which should mean we wouldn't have been able to see them before and now we'll get some extra cool details so I've just got this spot and one more right here on properties so now they all have these binding flags for us to work with now if I go down here let's say uh I'm going to play with the dev leader ones cuz we can see two classes at the same time I'm going to go ahead and make these private so that's going to be a private property I'm going to make this one a field actually so we'll do private count I'm just going to have it set to be one uh maybe we can do a public static method here why don't we do a private static method that's a double whammy okay so we have a method that's private static so technically hidden from everyone but it's static so it's on the type itself and maybe we can go add a Constructor in here and we'll chain them together so we'll have let's do it on this one on dev leader class We'll add two Constructors one will be private and have parameters and the other one will be public and have default parameters okay so I've just gone back to line 96 here I've made the field just not have anything automatically assigned here but I've added these two Constructors in this one's going to be public and have no parameters and it chains into this other private one that takes an account so now we have a mix of things we have something that's static we have a bunch of private stuff I have multiple Constructors we have a field that's private right that's kind of unique because this is usually a very hidden thing from us so let's go run this again and see what kind of details we get now okay I'm going to put in Dev leader and hopefully we still get our two types back and we get tons of stuff coming out here I'm just going to move this back on my screen a little bit so I can read it a little better and let's see so found two types type zero the first one you can see that we get two Constructors now right and from there we have the count on the second Constructor and recall that one of these was Private right this is the private Constructor we shouldn't really be able to see that but reflection lets us do it now when we look at the methods we have some extra things and I don't know if you were paying attention close enough before but member wise clone and finalize were not things that were showing up for us so that's kind of cool and what else do we have here so we have the get URL let's jump to the other one that's down below so we have a field that comes back now right we have underscore count that is a field that's really cool and we can see this method too this is hidden this is the static method that we added so this is a static and private that's showing up now so this is really cool that we're able to make a couple of tweaks and we can get static we can get private things we can get instance things we can get the public things that's the public instance stuff I think is pretty standard probably what you might expect but the fact that we can get static and private is also really cool here so that's just a quick little program that shows us that at runtime if we had some type of request to be able to go look up type information and that could be for any purpose right you might have different things that do deserialization you might be able to have other systems that are connecting together and they're not able to have that information up front you might have situations where truly based on user input maybe not as contrived as the example I gave here you need to go look up information and if we don't have that all at compile time because access to that information comes later reflection can be very powerful now I did mention to you earlier in this video that if you want to know how I use reflection all of the time you can check out this video next thanks and I'll see you next [Music] time

Frequently Asked Questions

What is reflection in C# and why would I use it?

Reflection in C# allows you to inspect and interact with types at runtime, rather than at compile time. I use reflection when I need to access type information based on user input or when the structure of the types isn't known until the program is running. This can be particularly useful for tasks like deserialization or when integrating different systems.

Can you give an example of how reflection is used in the video?

In the video, I demonstrate a console application that allows users to input a substring to search for class names in the current assembly. Using reflection, I retrieve and display information about the matching types, including their constructors, methods, fields, and properties, even if some of that information is private or static.

What are binding flags and how do they relate to reflection?

Binding flags are used in reflection to specify which members of a type you want to access, such as public, non-public, instance, or static members. In the video, I show how adding binding flags allows us to access private and static members that would normally be hidden, giving us a more complete view of the type's structure.

These FAQs were generated by AI from the video transcript.
An error has occurred. This application may no longer respond until reloaded. Reload