C# and .NET Basics

Reflection and Attributes

Spencer Schneidenbach

Spencer Schneidenbach

Aviron Software, Microsoft MVP
C# and .NET Basics

Check out a free preview of the full C# and .NET Basics course

The "Reflection and Attributes" Lesson is part of the full, C# and .NET Basics course featured in this preview video. Here's what you'd learn in this lesson:

Spencer introduces reflection which allows developers to inspect and manipulate type data at runtime, such as getting the type of an object, accessing its properties, and changing their values. Spencer also briefly mentions the use of attributes, which are metadata that can be added to code to provide additional information about how it should behave.

Preview
Close

Transcript from the "Reflection and Attributes" Lesson

[00:00:00]
>> Spencer Schneidenbach: I have one last section to talk about, which is reflection, reflection is a way of inspecting type data at runtime, well, its just exactly that. You're able to do things like take an object and get the type of that object, get the properties off of that object and read those properties so you could read the names of those properties.

[00:00:24]
You could read the values of those properties and you can do things with them, you can print them. This will become more apparent in the ASP.Net core course, we'll introduce little bits of reflection to just save us keystrokes. The important thing to know is that dotnet types are strong and they are inside of the framework in a really integrated way such that the ability to actually go through and understand what's in a type, what properties it has, what methods it has, has a lot of power.

[00:00:54]
So the first time I saw reflection, well, let me show an example of it, let's take this person object, for instance, you can get the type of that person object and then get a reference to that property. So, this would actually be something where it could actually is an object that represents the property inside of the runtime and then you could use that to actually change the value of that property.

[00:01:16]
And this doesn't seem like it's super useful, why wouldn't you simply just assign the property? Until you get to the point of realizing that reflection gives you a lot of power when you don't always know the type of the thing that you're interacting with ahead of time. When you get into writing framework code, this becomes incredibly useful because you're able to create constructs that will be able to read and write from and to types.

[00:01:40]
Without knowing that type ahead of time, and that has a lot of cool implications. You can even create types at runtime and then instantiate them yourself using methods like the one activator.createinstance which again, does have have value. This will become relevant when we talk about dependency injection in the ASP.NET Core course.

[00:02:06]
It becomes really important then because oftentimes when we're interacting with a dependency injection system, the only thing we know about an object is its type, we don't necessarily know its implementation. So, we can use that to our advantage and we can use reflection to do that, the important thing to note is that when you're beginning programming, this does not seem super useful at its face.

[00:02:28]
It's one of those things that I encourage you to just put in the back of your head because you will find as you get into your career times where you're just like, man, if I only knew at runtime what this object was gonna be. C sharp is a flexible and it's a great language but it doesn't have everything in it that you would need to know everything about your type at runtime and reflection can kind of help bridge that gap.

[00:02:49]
One last note about reflection, which is attributes. Attributes are metadata that you can add to the code that you write in order to give it some data or metadata about how that thing should behave downstream. So let's take an app and a HTTP server that we've created and we've modeled our incoming request as a create employee request for the first name, last name and department.

[00:03:11]
And perhaps in the system that those things are required, we can mark those things with attributes to say that those things are required, and then downstream when we're doing validation. When we're using a validation framework, ASP Netcore and other frameworks can automatically say this thing is required, if you give me an empty string or a null string, I will send a message back to the client saying that that thing is required.

[00:03:39]
You can do all sorts of things with With this, including instructing those that those attributes to say, do we want to allow empty strings in this request? If so, okay, no, if so, then yes, an empty string is valid, otherwise, this thing is required and it should not be an empty string.

[00:03:56]
Attributes can be used on all sorts of parts of the language, including parameters, methods and properties. I use the most commonly on methods and properties and you'll see a ton of examples of that in the ASP NET core course where attributes are pretty much used everywhere to add metadata for ASP NET Core to use to do its job.

[00:04:15]
Finally, you can create your own attributes, I don't often have to do it unless I'm writing framework code, or specific code where I want that know that metadata at time. I need to know that that member or that property or that method has a specific attribute on it, and then perform some kind of action downstream in my code.

[00:04:31]
This is where reflection really becomes powerful, is when you're able to take metadata that you're able to read from objects from your types, and then perform actions downstream on them. This is everywhere in ASP.NET Core, and we'll talk about that in that course quite a bit. The main thing about attributes that you need to know is that they exist, and that they're used to add metadata to your runtime properties, and that their implications of that are vast, very many.

[00:05:00]
Last thing that I had on the that is absolutely we don't need to cover it, was just talking about the hosting model in.net you can, if you want to explore that and kind of learn about it ahead of the ASP NET core class. But a lot of that stuff is just content to kind of tee up the ASP NET Core class.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now