What is Reflection? - C# Gotcha Interview Questions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
👍︎︎ 2 👤︎︎ u/ORParga1 📅︎︎ Jan 25 2021 🗫︎ replies

Hmm. I didnt think this was a good usecase for reflection... shouldnt you just make an interface, and check if the type implements it(if character is IBackstabber)?

👍︎︎ 1 👤︎︎ u/infinetelurker 📅︎︎ Jan 26 2021 🗫︎ replies
Captions
today we're going to learn about reflection if you enjoy this video initiate a mouse click over the like button and subscribe you're going to be asked about reflection in a programming interview now reflection allows you to inspect class metadata and even run methods dynamically but what does that even mean well i was invited to a dungeons and dragons game with my co-workers and i'll show it to you in action okay welcome to my dungeons and dragons game i'm sporty ryan let's see who we have here we have posh ryan seymour regular ryan and circle back jack so what characters do you want to be i'll be the cleric i want to be the wizard can i be a heavy weapons infantryman victor how about a fighter fighter's good i want to be a project manager bro circle back jack there's no project manager character well what character is sneaky and can double cross and backstab how about a thief i ain't no thief the only thing i steal are the hearts of delta zetas make me a project manager character or i'm not playing circle back when you're done peace out nerds adding a new character could be difficult all of my characters are hard coded in enums can you help me refactor this regular ryan sure let me see the problem well i created an abstract character class because all of the characters have common abilities like attack or defend but different characters have special abilities so let's say a character attacks a monster from behind i use this switch statement and character enum to determine what kind of character is doing the attack if it's a thief he gets a special bonus called a backstab if it's a wizard or a fighter they just do a regular attack i'll have to create a new project manager enum and then i'll have to create a copy of the thief class and call it a project manager and just make sure the project manager can backstab then i'll have to go back to that switch statement and make sure that project manager is added in the switch statement with the backstab ability and i have to do that everywhere i have an enum in a switch statement that deals with characters have you thought about using reflection yeah but reflection is so complicated it's actually easier than you think now enums can be dangerous because they cause you to sprinkle switch statements all over your code you don't need an enumeration to detail your character type because your character type is already indicated right here in your derived class so you can use reflection to peek at the character type and attributes and perform actions based on the kind of character you have so let's look at those inherited classes so project manager can backstab and thief can backstab but the wizard doesn't have a backstab method the fighter doesn't have a backstab method the cleric doesn't have a backstab method we can work with this let's create a better combat class and down here if you notice rear attack we're going to change rear attack so that any character with a backstab will automatically backstab and if you don't have a backstab you'll just do a regular attack so the first thing i'm going to do is i'm going to use reflection to get the type of the current character i'm going to put that into character type now i'm going to check to see if the character type has a method called backstab remember project manager has backstab now let's check to see if backstab method info is null if it's null then this is not a character that can backstab so just do the regular attack but if it's not null we can use the power of reflection to create a backstab character instance and then invoke this method of backstab so let's see this in action i created a fighter named ryan i created a project manager named circle back jack and both characters are going to do a rear attack let's see what happens when ryan does the rear attack his character attacks when the project manager attacks he backstabs i can create a backstabbing sales person or a backstabbing test engineer as long as they have the backstab method they'll always stab you in the back and the only code change you have to make is in the derived class reflection is a core concept in c sharp if you're asked about reflection in an interview i like to use the mnemonic d simple which stands for dynamically see and invoke members and properties for loading and examination remember this mnemonic practice some reflection on your own and good luck on your next interview now let's get back to the game a flaming stakeholder appears and smashes through the door pushing you onto the bridge a yawning chasm appears below what do you do uh cleric tells the stakeholder that making the change will add risk i'll just do whatever the stakeholder wants and circle back next sprint uh the fighter tells the stakeholder that we're gonna make that change in phase two so it doesn't make sense to add it now i'm not doing it it'll break my unit tests and then they shall not pass
Info
Channel: Ryan McBeth
Views: 2,986
Rating: undefined out of 5
Keywords:
Id: nx-GGQyZ2cU
Channel Id: undefined
Length: 4min 49sec (289 seconds)
Published: Mon Jan 25 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.