Here's How C# Attributes Make Reflection a Breeze!
Finding types in reflection can mean iterating through assemblies and looking for whatever matches your naming convention or some other heuristic. But what if we could find C# types using reflection in an easier way? What if we could use that same approach to find other members of those types as well?
In this video, I'll explain how we can use attributes in C# combined with reflection in order to find types and members easily.
View Transcript
if you're interested in being able to find classes or methods at runtime using reflection and C is's a better way to do it than looking things up strictly by their name my name is Nick centino and I'm a principal software engineering manager at Microsoft in this video I'm going to walk us through how we can use attributes to be able to look up methods classes and potentially other things that you might be interested in using reflection just a quick reminder to check that pin comment for a link to my free Weekly Newsletter and my courses on dome train and with that said let's jump over to visual studio okay so we're going to start by creating our own attributes and we're going to look at two different kinds here I'm going to make one that we can assign to classes in particular and methods as
well for a second attribute and I wanted to mention cuz I learned this through Visual Studio when I was putting this together but there is a quick option when we're trying to create these attributes which you need to inherit from attribute like you see here on Line 39 but through the quick actions and refactoring menu if I click on this we can see right at the top that apply attribute usage attribute can be one of these things in this list which is really cool and I didn't know that we had all of these available to us so easily I'm very used to forgetting how this works having to go search it online trying to find the right attribute to add to make this happen and I didn't realize that we could just use this quick refactoring menu to make it work so we can go
ahead and in this case I want to pick one for class and there we go this is going to allow us to create an attribute and when we Mark our attribute with yet another attribute this means that it will only work on classes and if we try to assign this attribute to things like methods or Properties or anything else it just won't work at compilation time when we're working with attributes we can pass in parameters as well so there's nothing really special about this I just wanted to add in some different things that we could configure on our attribute next what I'm going to do is show a couple of classes so this one here Class A is going to be flagged with our attribute and I've added Class B here which does not have the attribute and that's because I want to show that
class B shouldn't show up in our results when we go looking for things marked with this one and next I'm going to show a very similar looking attribute but really what's changing here aside from the name itself is that we're going to mark this with method instead of class so this attribute can only go onto a method and not a class and we can see that here because Class C that I've added not only has this first one that we looked at my class and I should mention too because this might be confusing and you might have noticed as I was scrolling through but we're allowed to completely remove the word attri from our attribute name when we go to use it so my class right here is the same as this and in fact if you look very closely you can see that my
class and the word attribute is technically gray out Visual Studio saying hey look you don't got to do that because it's technically a little bit redundant by having the square brackets and making it an attribute and also adding in the word attribute so I just wanted to mention that you can do it either way so you can have the full type name of your attribute or the name of it without attribute at the end so Class C is going to have our my class attribute but I've also added two methods onto here and we have one without any attribute on it and one with our My Method attribute so what we're going to do is go write a program that's going to look for this specifically and what we should see is that method B does not show up in the results because it's not
flagged with our attribute okay so now that we've seen how to declare the attributes and how to assign them onto classes and methods that we're interested in looking up and I did mention that because we have all of those other types of attributes we can assign on to things that means you could go look up other information as well using the methods I'm about to show you but you would need to go do things like assign that uh attribute to be specific to a Constructor and then you could go look up Constructors the same way I'm about to show you it's just that I'm not going to go through the exhaustive list because once you see the pattern I think you can apply it to the other scenarios what we're going to do is ask our assembly so we're going to get the current executing
assembly for all of the types and then we're going to filter out those types where we have a my class attribute on the type itself and then we're going to put that all into an array now based on your needs and your situations where you're scanning from you may want to do in a different way this is just a demo program and I know that I'm just looking up things in my current assembly but if you need to be able to load things from other dlls you may want to go approach this in a different way once we have the types from our assemblies that we're interested in what I'm going to do from there is write out to the console how many we have and then for each one of them what we're able to do is ask the type to get the attribute
off of it so we're going to ask that type get my Class attribute and we're going to have a reference to it now and that means that we can go ask for those property values that I created earlier on in the video and then we're going to extend this example just a little bit further because we're going to ask those classes that had that marker on it that attribute that we were interested in we're going to do a very similar thing we're going to ask those types which is only going to be two of them hopefully if this works and we're going to say for those types get me the methods on those those types and then we're going to filter those as well using get custom attributes once again and we're going to look specifically for our My Method attribute and then we're going
to toss that all into an array as well and a very similar console output with the for Loop that we have here all that I'm going to do is print out how many we have once again and then instead to make it a little bit more of a Bose this line here is going to make it look like we're saying the type name and then calling the method as well so we're going to have that and I think that's about all I want to show before we go run this and let's hope that it works and of course I'm kidding because I ran this before recording so I knew it was going to work but let's check the output we know that we're getting these two types that we flagged with the my class attribute and that's as expected because I had class A B
and C but Class B was the only one without that attribute and that means when we go to print out in the for Loop we get both of these types coming up and we can see that I made the description part for Class C a little bit different so it says this one has has a special method and that's going to indicate hopefully the next part works right and what we can see too is I made the names of both of these equal to the type name and you might have noticed that I used the name of call around the type and that's just so that I could have that easily transcribed right into this property the second forward Loop we had was to be able to get us this information here so we did find one method that has this attribute when we were
scanning both of these two types because these are the only two types that got pulled into that first array now that we're looking at those two types we're looking for any methods that have my method attribute and we can see that class C has method a on it and then very much like I printed above we just have the name of the method and then I put a little description saying this is a method so the takeaways for this video that are instead of looking up things specifically by their names in your code you can actually flag the different things that you're interested in using attributes and that will allow you to decorate them with different types of information as well in this example the that I showed I was able to say that I want to have a specific attribute for a class and
one for a method I added on things like a name and a description but you could do all sorts of things that you want as long as you can pass that information into the attribute when you create it that means that if you're doing a lot of reflection work in your program in my case I'm doing a lot of plug-in loading and things like that so being able to decorate this type of information on your types to be able to find the ones you're interested in means that you can enrich the results that you're working with and make things a lot easier to find instead of just saying like does it contain this part of a string and trying to match things that way if you're interested in walking through an example of an application that I built that has plug-in loading that has to
rely on some things like this you can check out this video next thanks and I'll see you next time
Frequently Asked Questions
What are C# attributes and how do they relate to reflection?
C# attributes are a way to add metadata to classes, methods, and other elements in your code. They allow you to decorate your code with additional information that can be accessed at runtime using reflection. In this video, I demonstrate how to create and use attributes to make finding classes and methods easier when using reflection.
How do I create my own attributes in C#?
To create your own attributes in C#, you need to define a class that inherits from the 'Attribute' class. You can then use the 'AttributeUsage' attribute to specify where your custom attribute can be applied, such as to classes or methods. I show this process in the video, including how to pass parameters to your attributes.
Can I use attributes to filter methods and classes at runtime?
Yes, you can use attributes to filter methods and classes at runtime using reflection. In the video, I demonstrate how to retrieve types from an assembly and filter them based on whether they have specific attributes. This allows you to easily find and work with the elements of your code that are marked with the attributes you defined.
These FAQs were generated by AI from the video transcript.