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

preview_player
Показать описание


Hello, everybody, I'm Nick, and in this video, I will introduce you to an extremely powerful feature added in .NET 8 called the UnsafeAccessor. This feature aims to replace reflection on many levels and provide compile time performance but also allow for NativeAOT support.

Subscribe to Dan: @danclarkeuk

Don't forget to comment, like and subscribe :)

Social Media:

#csharp #dotnet
Рекомендации по теме
Комментарии
Автор

This is probably a crime in +50 countries.

gustavo-santos-dev
Автор

Finally, I can change the value of a static private read only field of a class in some other person’s code so that when another person use my library they will learn to read through all code before actually believing something is read only and private.

clementdato
Автор

I'm guessing this is part of getting rid of reflection for AOT...

KieranFoot
Автор

With great power comes great responsibility.
Don't use your power to change static readonly fields :D

woocaschnowak
Автор

Who would win: ten years of SOLID principles vs one unsafe boi?

ristopaasivirta
Автор

For this feature I must say "With greater power comes greater responsibility". Excellent demonstration. Thank you Nick.

mrsajjad
Автор

"The value is Nick" - You are the value mate appreciate your work

ElmoTheAtheistPuppet
Автор

This seems really handy for unit testing, but I have concerns about using it in other contexts. As someone who writes a lot of infrastructure code, I find it crucial to restrict developers from accessing certain parts of the code.

DennisFazekas
Автор

"It's a pain ... where you don't want to be painful" is hilarious. I'm gonna steal that. Regarding the main topic ... I've used reflection to access private members before, but only when it was an abstraction designed to handle cases where you don't know the member names at design-time. This new feature doesn't help with that, so it's just ignoring the original developer's intentions. I suppose if you absolutely need to do it, having it be declarative with "Unsafe" in the keyword is better than hiding it behind reflection. Also much faster, of course, as you observed.

dhochee
Автор

Compile time performance is the best part of this ❤

PereViader
Автор

Great video, but I think one additional way of doing it should have been in the benchmark. That is, getting the MethodInfo object through reflection, then build a Func<string> (or similar) delegate of it and then cache that delegate and call that instead of using the dynamic Invoke() method each time. Many years ago I changed that in lots of places in a code base that had a lot of reflection and improved performance a lot. The question now is how that compares to this new way of doing it!

OlofLagerkvist
Автор

I had a very good use case for this recently. We use private constructors for some of our objects because we want to tightly control how new things are made. But that inadvertently effected serialization. System.Text.Json cannot serialize with private constructors. Newtonsoft can. Thats an example of when something is private. but you may still need to access it externally in some situations. I dont want to open up something thats private in the code base but i still want to be able to serialize/deserialize it properly.

Demonata
Автор

Thank you for this, Nick. Concise and helpful as always. I can see the use - as you demonstrated - clean and fast running, if potentially dangerous code.

DivingDeveloper
Автор

This is awesome! I had some cases in the past, where i had to use reflection to get a private value out - and it was very slow.
Great that there is now a tool for doing the same thing - and more, for compile time performance. I like it ;-)
Thanks for sharing.

Fnalspace
Автор

My experience with code using refection is it is either doing something really powerful and useful or something stupid. It is usually always the second one.

markhenderson
Автор

Nick's on a mission to one up each .Net 8 video with something having even more terrifying possibilities.

JackoCribbo
Автор

My mind was blown once, when my colleague showed me how to make a self-updating const in JavaScript.

Now you're showing me that private properties can be changed from outside classes 🤯

I need to sit down

PeterOeC
Автор

It's really helpful feature in some rare cases. Thanks for the sharing.

aabbccddeeff
Автор

Will we get a dedicated in-depth video about the volatile keyword at some point? It's been 2 years already since you covered it briefly the last time.

mindstyler
Автор

Regarding the code mess of the reflection solution, I had an extension method for the reflection calls in a project. Worked for any property of any class.

gliaMe