The New Way of Calling Your Code in .NET 8 Is INSANE

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody I'm naked in this video I'm going to show you a brand new way of interacting with your code base in C shop that was just at the dotnet 8 that completely changes the game and I'm gonna be honest it is insane that Microsoft even added this and made it public for us to use because I can totally see this going very wrong if people have used it there's too much power being given here and nothing you should know about it so you can know how you can deal with it in your pull requests in case somebody tries to use it now I think this is an excellent feature and it really streamlines a lot of the pain points you might have with trying to access things you probably shouldn't have access to however there are some caveats and I'm going to present all that in this video not only that but the performance games of a feature like this are insane if you like a lot of content and you want to see more make sure you subscribe for more training check out my courses on domtrain.com now before I move on I'd like to let you know that we just launched a brand new course on Dom train called from Zero to Hero Docker for developers Docker is still the most used tool in the developer ecosystem and is a must know for everybody this call is very up to date it teaches you everything you need to know about Docker from the very Basics how to install it and how to get started with it to some pretty advanced stuff in terms of security and tips and tricks and this authored by Dan Clark a new author on Dom train he has been working with Docker for years and he's seen them Good the Bad and the Ugly and this course is going to teach you everything you need to know to use it the right way with the best practices and use it as one of the most valuable Tools in your toolbox even if you do know how to work with Docker on a basic level this course will teach you literally everything you need to know as a developer and it's made with developers in mind now to celebrate the launch I'd like to offer the first 400 of you a 15 discount code on your purchase so click the link description and use the discount code you see right now in your screen to claim your discount now back to the video all right so I'm sure you don't have here I have a simple.net 8 console application we're going to start very very simple let's say I have an example class over here and that class has let's say a private string method that return and some text so I can say return Nick over here now this is a private method but for some reason I might want to be able to call that method now if something is private you can argue that it shouldn't be called but we've been in situations throughout our lives where you might have to access something that is inaccessible I totally understand and I stand behind the fact that you should not be doing something like this if something is private it is outside of the public API and you only have to access it or deal with it through its public members because if it is inaccessible you're not supposed to be using it we can agree that this is the case however we've all been in situations where we might have to do that for Better or For Worse that is the experience that Microsoft is trying to improve and actually greatly improved in.net 8. so for the purpose of this video Let's just ignore the fact that you probably shouldn't be doing this and just focus on how we can do it way way better now in.net date and Microsoft would not add the feature like this if there wasn't there a sort of use case for it so let's just focus on the feature itself now if I want to call this method obviously I can't just go ahead and say method because well that is impossible so what can I do well traditionally you'd use reflection so you would say type of example over here and then since you have the type you would say get method you would use the method name so method now since this is an instance and a non-public method you would say binding Flags instance and also non-public and to invoke it to invoke that method you would say invoke and you would pass the instance of the example class and you would also say that this is an empty object array because we have no parameters to pass down and you might even use something like an array dot empty over here to not allocate an empty array every single time so something like this and if you are to do this then you would get the name which would be Nick let's take a look at that so console.writeline name game if I go ahead and I run this then as you can see in the console we get the name the name is Nick and in fact let's just quickly debug it to see every step of this so we get the example class it's here and then we get all that over here to get the name and as you can see the name is Nick and we can print it now it doesn't take a genius to understand that having to write something like this every single time is very very tedious and this is the same case with things like Fields so if you have a private string which is a field over here and that is also neck or something else then to access that you have to say don't get filled if it is a property you have to say get property Constructor and so on now that is a very very tedious thing to do every single time and maintaining this code is a pain in where you don't want to be painful so what was adding.netate well let me show you I'm going to create a public class over here I'm gonna call that color and I'm going to add a public static extern you probably haven't seen this keyword before but you want to have an extern here string and I'm gonna call that get method and I'm going to use the example as a parameter so that is my method now and what I'm going to do because yes I have the method now but if I go ahead and I say for example one name equals and I comment all of this because I don't need it anymore so let's go ahead and do that and I say name is color dot get method example then I'm not really going to get anything let me just quickly show you I'm going to take a breakpoint here and run this step over this nothing actually happens so how can I get this to call that method well all I have to say is use the brand new attribute and save accessor now the unsafe part in this case is that you're accessing a private member that you shouldn't be using so that basically just brings your attention to the fact that you're doing something that you have to be extra careful with but once we have that we can go here and we can use the first enum which is unsafe access what type of thing a Constructor a field a method a static field or a static method we want a method in this case because that is an instance method and then we want to specify the name of the method which in this case is just methods but you would use whatever is the name of this and once I have that I'm gonna go ahead and run this and as you can see now I can get the name using this in a compile time fashion in a very very easy to write way and easy to maintain this is it and you know if you could actually access that method or remember you could say name of and get the method and so on in this case we can but this is a fantastic step up from what we had before and this does not stop there for example if you wanted to access a field you would do the same thing you could say the extra kind now is a field and the name in this case is the name of the field so field and I can say get filled so if I do that let's just stop this over here and say get filled and the last thing I need to do before I run it is because this is a field I also need a ref keyword here because I want a reference to that field so once I have that and I have the breakpoint I'm gonna go ahead and run this and if I do that I should be able to get the name as you can see not only can I get the name but also I can say color get failed and because this is a ref I can say John so if I want to set the value of that field because this is a ref I can go ahead and do that and you can see how this can get into the danger zone because this all looks very safe for something that starts with the word unsafe but I can get the name like this so I have Nick and then I can set the name if I want and that just absolutely works so if I go to example and I see the name the name is now John because I'm using that ref keyword it is way too powerful I can't believe this was added but now we can do this with things like Fields method Constructors even with properties even though there is no property a parameter here in the enum a property is just a getter and a Setter method so if I had let's say prop string property over here and I said that this is actually a private and I set the default value again to Nick then if I wanted to call it the way this is compiled behind the scenes is that we get an asset and then the name of the property method to set the value and I get and then the property name over here to get the value so I could get this and I can say that this is just another method give me the property by saying get property here so get property and if I do that and I go ahead and I say get me the property then as you're going to see we're going to get that value from the property no problem at all the value is Nick now you're probably thinking one of two things wow this is insane I'm gonna start using it and replace my reflection calls with it which good that's basically what you should be doing but also you might be wondering other than the fact that Jesus Christ this makes reflection like Behavior way more accessible we're gonna have to deal with a lot of stupid things in our code base but also the other thing is how does this compare performance wise because this looks like it's going to be way more performant than having to do this every single time and getting the type of getting the method invoking with reflection so how does performance stack up with this well let's take a look I'm gonna add a few benchmarks over here just to show you how it works so let me talk you through the benchmarks first we get a read-only reference to that example class so we can reuse it in all the benchmarks first I'm calculating everything about the type in here and then I'm getting the method and I'm invoking it now this is an inefficient way to do reflection because you have to calculate this method info every single time so what I'm doing in the next Benchmark is I'm actually caching it over here as a static read-only field and then I'm invoking it which will give it a better chance to perform really really well then I'm calling the unsafe approach with the unsafe accessor attribute the thing we just saw and then I added an extra public method to show you how it compares to the direct direct call to a public member of the class so what I'm going to do is just go back here and say return early and then Benchmark Runner dot run my benchmarks and let's see how all of these now stack up let's make sure that this is in release mode and run it so let's take a look at what the results are all right there is also back let's see what we have here so as you can see traditional reflection 24 nanoseconds very very slow the cast version 600 seconds much faster than the non-cast version however the unsafe and the direct basically the same so this new and safe actual approach gives you compile time performance without having to use reflection at all the performance here is completely Bonkers now does this mean you should go ahead and use this everywhere in your code base probably however you should be a bit careful of a few things for example this will allow you to do things you can't do with traditional reflection and in fact you can do traditional code either here's an example let's say I have a static read only field over here so I'm going to say static read-only Sr field now obviously you can't change the value of this field in runtime in fact the jit will treat it in a way as a constant so this is just perceived as something set in stone and that's not going to change however and this is where this gets a bit tricky you could if you wanted to say something like this get static field and change this to a static field and give it the name start the grid only field over here and if you do that then nothing stops you from going over here and saying VAR field equals get static field from the color and getting its value but also being able to say hey how about I just set this value which is not supposed to be changed ever and as you can probably understand doing something like this which by the way you totally can so I can get it and I can set it and if I go in here you can see the value is now lines to John this could potentially cause an expected Behavior so very very careful this is not something reflection can do so if you try to do this with reflection the application will actually blow up so use this at your own risk and don't try to do something that's just way way too far out there because there's too much power that this thing is giving you but I do think that it is a good feature that will simplify a lot of bad code or messy to maintain code in our code bases but no one know from you what do you think this and is it something you're going to use leave a comment down below and let me know well that's all I have for you for you thank you very much for watching and as always keep coding
Info
Channel: Nick Chapsas
Views: 118,723
Rating: undefined out of 5
Keywords: Elfocrash, elfo, coding, .netcore, dot net, core, C#, how to code, tutorial, development, software engineering, microsoft, microsoft mvp, .net core, nick chapsas, chapsas, dotnet, .net, .net 8, nativeaot, reflection, pgo, nativeaot .net, nativeaot c#, unsafeaccessor, unsafeaccessorattribute, c# unsafe, reflection c#, reflection .net 8
Id: WqeSRUXJ9VM
Channel Id: undefined
Length: 12min 34sec (754 seconds)
Published: Thu Sep 21 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.