BrandGhost

It's EASY - Check Type Data with Reflection in C#

If you're just starting out programming in C#, you may not have even come across reflection yet! Reflection is a powerful tool that we have access to in C# which allows us to inspect type information dynamically at runtime. This opens many possibilities for us when we have information at runtime that otherwise couldn't be used to do these checks at compile time. Check out this beginner's guide to get started with reflection in C#!
View Transcript
have you ever wondered how you can get information about classes dynamically at runtime instead of having to know about it all at compile time my name is Nick centino and I'm a principal software engineering manager at Microsoft in this video I'm going to take a beginner focus on working with reflection in C to do just that we're going to look at some code examples that allow us to inspect some types in C and see what we can do from there just a quick reminder to check that pin comment for a link to my newsletter and my courses on dome train let's head over to visual studio so in order to demonstrate create some things that we can do with reflection in C we need to have a class that we want to work with what I've done is I've created just a really simple dummy class that we can play around with there's not really any purpose to this so please don't read into what it's doing I just wanted to have a couple of variations of things so we can see how it all ties together so I've just made a new class and then I've provided two different Constructors on here and that's so that we can go look at different Constructor information using reflection we can see how they differ from each other once we start to inspect those Constructors the first example we're just providing some default values and then chaining it into this Constructor here that's defined from line 40 to 42 and then we end up setting those values right onto these properties and those properties are defined right here so we have two public properties one's an integer property one's a string property and then I've also added on this method and it doesn't do anything because that's not the point of this particular video I just want us to be able to look at what we're able to get when we inspect these things with reflection and I've said reflection a couple of times so far I'm assuming that if you clicked on this video you probably had some idea what reflection is but just a quick note that if you're not familiar with reflection it is the capability that we have in C to be able to investigate type information at runtime now C is a language that we have all of this type safety built into so it's really difficult to write code where the types don't really line up because a lot of the time the compiler can say hey you can't do that and it stops us before we can even compile and that means we don't run into some of these issues at runtime of course there are cases where we can cast things incorrectly and all that sort of thing but a lot of the time the compiler is really able to assist us but that means that we need to know that information up front at compile typ and if there's situations where you want to investigate types work with types at runtime and you don't have all of this information from ahead of time or you want to do things more dynamically reflection can help with that let's head back to the code all right there is a lot more code on my screen now but it follows a very similar pattern so please don't be totally taken back by this what we're going to be doing is looking at Constructors properties and methods on a particular type so what I wanted to show you is that I'm going to be let's just start with the constructors at the very top here so I'll collapse these other ones we're just going to be looking at this part for now because as I walk you through this the other two sort of blocks of code that we just saw those are very much the same so what I'm doing is just going to be printing out to the console so we have some you know some space that we can see what's going on and then using reflection I'm saying type of and then my class so we have this syntax that we can use here to ask for the type of my class so without that we can't just write this code like my class dot because this would be expecting a static method that's on the typ my class which doesn't exist but when we do this this is getting an instance of a type object so this is a type object and then we're asking that type object to be able to get the constructors now once we have a list of the constructors what we're able to do and this is just a little bit more verose uh link that I have here just because I want to be able to easily print out the information really all that I'm doing is asking for each Constructor um I used just used a 4 each Loop instead of a a four Loop and counted through it but that's really all that this is doing is I'm getting an instance of the Constructor along with the index so that's why it looks a little bit more of ver Bose here now this console right line is just going to print out the name of the Constructor and then from there what I wanted to be able to do is print out the parameter information of the constructors and that's because we saw on my class I defined two different Constructors so if you were interested in inspecting a type and seeing what's different between the constructors you could use this type of behavior next we're going to look at properties and like I said this is going to be almost the exact same thing but instead of getting Constructors we'll just ask for the properties so this part here is still getting the type of my class so we get a type object back this is that verbose link again that's just going to be getting an instance of the property information along with the index and then I'm just going to print that out here so we'll get the property name along with the type of the property that we have so we should see something about an integer property and something about a string property because those are the two properties that I have and finally we're going to look at methods and this is almost the exact same thing so it should look very familiar this part here is different so we're asking for the methods instead of the properties and then from there what we're doing is printing out the method names and then having the different parameters that we have available into those methods provided here as well now I didn't add any parameters onto these methods so let me go add one more and we'll provide it some parameters okay now we have some variations of these things and we have this big block of code up at the top here it's going to print this information for us again the reflection parts are really the parts that we're using after we get the instance of the type so get Constructors get properties and get methods let's go run this and see what we get okay so there's a ton of output on my screen and this is just for a simple class as well but if we start with the constructors recall that we have two Constructors right one had no parameters and that's going to be the first one cuz we see Constructor zero the name isct so this is just the built-in name cuz we don't name the constructors right the Constructor names to us look like just the type but this is the name of the method behind the scenes that is the Constructor but then there's no parameters for the first Constructor and that's one of the two Constructors we had so that checks out the second one also has the same name right it's kind of interesting they don't give the constructors unique names or anything like that and then the parameters that we have there were two there was a numeric one and a string one and internally within that Constructor we were assigning these to the properties speaking of properties that's the next part we have here so nothing really fancy going on here but we get both property names and then we can see that one is an integer and one is a string so this is what I would expect the methods part gets a little bit more interesting because you just saw me add a second method on there why the heck are there nine methods on this class there's literally only two and you saw me write the second one right before running this so many beginners don't realize this but properties are technically just syntactic sugar for methods that are Getters and Setters and we can see that very clearly here so method zero is the getter for numeric property and this is literally a method that exists it's called getor numeric property that is a method we have access to and same with set and you'll see that the set numeric property takes in a value because it's a Setter and that's of integer type then we have the same thing for the string so these four methods together are really just the properties that we declared next you'll see method four which is the one that I created in the beginning called My Method there's no parameters on this one so there's no extra information to add on to it and you can see method 5 is right below right here that's the one that I just added before running this code and you can see that it has some number and some string those are the two parameter names I made one's an integer and one's a string so that all checks out it's kind of cool that we can see all that information but then there's four more properties right and these technically come from the base class because every class is technically an object so get type two string the equals method and get hash code are things that are all going to be available on every class so even though you didn't see me go Define these on that class and technically you didn't see me Define these exactly as they're written here on that class these are all methods that technically exist in the compiled code and that's just a quick primer on how we can use reflection to go examine some type information on C types now you might be saying to yourself well that's kind of cool I can see maybe some use cases for that but wouldn't it be extra cool if we could see other things that we weren't supposed to have access to and you're totally right that would be awesome so you should check out this video next thanks and I'll see you next time

Frequently Asked Questions

What is reflection in C# and how is it useful?

Reflection in C# is the capability to investigate type information at runtime. It allows me to dynamically access information about classes, such as their constructors, properties, and methods, without needing to know this information at compile time. This can be particularly useful in scenarios where I need to work with types dynamically.

How can I inspect constructors of a class using reflection?

To inspect constructors using reflection, I use the `Type` object of the class and call the `GetConstructors` method. This gives me a list of constructors, and I can iterate through them to print out their names and parameter information. This way, I can see the differences between the constructors defined in my class.

Why do I see more methods than I defined in my class when using reflection?

When I use reflection to inspect methods, I often see more methods than I explicitly defined in my class because properties in C# are actually syntactic sugar for getter and setter methods. For each property I define, there are corresponding methods generated behind the scenes for accessing those properties. Additionally, methods from the base class, such as `GetType` and `ToString`, are also included in the list.

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