BrandGhost

Using Reflection Where You Didn't Expect - Debug Watches!

You've heard all of the stories about using reflection in your C# code: It's bad, it's slow, it's dangerous, don't do it! And while it's true that reflection does get a lot of misuse, there's still some cool things you can do with it... Like debugging your C# application! In this video, I'll walk you through how I was able to get a bit more insight during my debugging because I set up a debug watch with reflection. Seemingly, the watch variable had a little bit more power than I expected when it came to reflection!
View Transcript
if you've been programming in c and heard about reflection you've probably heard or said for yourself that using reflection is not the thing to do we know that it's dangerous we know that if you're using reflection a lot in your code it's not right you're going to make mistakes there's better ways to do things we've heard it all and like I said we've been guilty of saying it too hi my name is Nick centino and I'm a principal software engineering manager at Microsoft in this video I want to talk about reflection and a really cool thing that I ended up discovering when I was trying to use some debug watches for a previous video now full disclaimer I've said this before in other videos and I'll say it again I am not encouraging you to try and go break some rules using reflection and write bad code but this is truly a really helpful tip that is not going to be writing code into your project to compile we're going to use it when debugging like I said I discovered this when I was going back through my benchmarking code and fixing up some errors in one of the last YouTube videos so before I jump over to visual studio just a quick reminder to check that pin comment for my courses on dome train and to subscribe to my Channel all right on my screen I have the code in visual studio for my regular expression benchmarking video and what I want to walk through is a situation that I encountered where online2 I needed to set this Rex cache size to be zero and the reason that I wanted to do this is because I had already messed up my regular expression benchmarks and I didn't want to have to do it again I needed to prove to myself that when when I set this to zero I'm truly not going to have any headaches with cached regular Expressions now to see how this all works before I show you the reflection part which is a bit of a spoiler on the lines above but if I scroll down a little bit and we go into the static method for matches so I go into here you can see that we have this Rex cache so once we try performing a match we'll go look up the cache to see whether or not we get this pattern so if I do that we see that we have this reject cache here which is an internal sealed class because this is internal it means that it's only going to be visible to this assembly system text regular Expressions means it's not visible to our assembly that we're operating in that's going to pose a bit of a challenge because what I want to do is be able to run some reflection code which you can see up here and just calling it out it's this sore cach list field that I want to get out of that cash class right so if I go back into this cach this is a field that if I scroll down a little bit lower right here on line 55 this is the field that I want to get access to because when we try to cash things and I'm trying to prevent that we would add them into this list so if I can prove that this list is still zero then I can prove that I'm in good territory going back to our program here what I need to be able to do is get this Rex cash because if I can do that I can use some of the dark arts that we know from reflection to get this field and use use binding Flags to be able to get a private field that's something that you probably shouldn't be doing in general for most of your code if you find yourself writing code like this ask yourself why right there should be a very good reason for it it's pretty rare but you'll notice that this Rex cach I don't have access to it and if I try to resolve this error the only things that Visual Studio are suggesting to me are to make a new type or install another Nate package neither of those things are what we want to do and that comes down to the fact that we can't see this Rex cash class the other couple of things we could do are look at these types right so I could try to get the type for Rex cache or I could try the fully qualified way to get it and if we go run this which I'll do in just a moment we'll see that these come back as null so I'm going to go ahead and press play and we'll debug it but before I do I wanted to explain that after setting that cash size to zero you can see I'm making a new regular expression right so the pattern make the new instance and then I'm going to call matches on an empty string maybe I should put something in here to see if we end up matching it so that way we're not short circuiting anything behind the scenes right so if I do this if caching was enabled we would see that our pattern a node for it would get added in to that list so let's go ahead and debug what I'm going to do is add this to the watch so I right clicked on it add watch I will add this to the watch we go there's another one in there and you can see these bottom two entries right they say null and that's because when we go to evaluate these on the watch they don't get anything back so visual studio when it's running these cannot go find these types by these names so that's unfortunate but what is cool and this is a really awesome thing about debug watches that I didn't know is if you look at the top line of my watch list here right it says type of Rex and it does the get field that's the exact code that we have on line 11 line 11 didn't work for us because we can't see the Rex cache right it's marked as internal but if I go press this refresh button it did in fact go look for it and just to prove it this third entry that we have that says type of Rex cache in our watch was actually able to go resolve that type even though it's marked as internal so that's just proof that there's not some weird fancy magic going on for this first watch it truly is able to find the type and then go do reflection to get the field for sore cach list and we are proving that it's empty now like I said at the beginning of this video I know that we always say hey don't go use reflection there's better ways to go do it and I agree that for a lot of the code that we're writing in C you really don't need to be using reflection I think it's an awesome powerful tool that you can use in different situations and I wanted to show you this particular case with a debug watch because we're not actually adding any code that's using it to our source we're just using it to go debug so if you find that you're in a bit of a pinch and maybe you're doing something like I was doing where you're trying to look at some internals for some of the net types just to see what the heck is going on you might be able to leverage something like this if you thought this was useful and you want to see some other debug techniques when the video is ready you can check that out here thanks and I'll see you next time

Frequently Asked Questions

What is reflection in C# and why is it considered dangerous?

Reflection in C# allows you to inspect and interact with object types at runtime, which can be powerful but also risky. It's considered dangerous because it can lead to mistakes and make your code harder to understand and maintain. If you find yourself using reflection frequently, it's a good idea to reconsider your approach.

How can I use reflection for debugging without adding it to my project code?

In this video, I demonstrate how to use reflection in debug watches within Visual Studio. This allows you to inspect private or internal fields without actually writing reflection code into your project. It's a handy way to debug and understand what's happening under the hood without compromising your codebase.

What should I do if I encounter issues accessing internal classes or fields in my code?

If you're having trouble accessing internal classes or fields, you might consider using reflection as a debugging tool. In the video, I show how to use debug watches to access these types, even if they're marked as internal. Just remember to use this technique judiciously and only for debugging purposes.

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