The C# Feature I Use Instead of Reflection

  Рет қаралды 36,460

Nick Chapsas

Nick Chapsas

Жыл бұрын

Check out the ABP Framework here: bit.ly/3UmfRbh
Check out my courses: dometrain.com
Become a Patreon and get source code access: / nickchapsas
Hello everybody I'm Nick and in this video I will introduce you to a set of attributes that allow you to get compile-time metadata about your code and use them in runtime. In many cases the data you will collect are impossible to get with something like reflection during runtime.
This video is sponsored by abp.io.
Don't forget to comment, like and subscribe :)
Social Media:
Follow me on GitHub: bit.ly/ChapsasGitHub
Follow me on Twitter: bit.ly/ChapsasTwitter
Connect on LinkedIn: bit.ly/ChapsasLinkedIn
Keep coding merch: keepcoding.shop
#csharp #dotnet

Пікірлер: 48
@thinker2273
@thinker2273 Жыл бұрын
I think it was mentioned in a recent stream with Mads Torgerson that there is a proposal to add a [CallerMemberInfo] attribute which would work the same as [CallerMemberName], but would allow you to recieve a MemberInfo instead of a string. Personally I think that would make the entire "caller" family complete, and would also act as a more reasonable alternative to the proposed infoof expression.
@KieranFoot
@KieranFoot Жыл бұрын
That would be a great addition.
@Sanabalis
@Sanabalis Жыл бұрын
I've used all except CallerArgumentExpression. One of the more useful things I did was inject the Caller, File, and Line number into the logger scope at the start of methods, making debugging from logs much easier, especially if you're looking at logs that don't have stack trace.
@jptouron3080
@jptouron3080 Жыл бұрын
hi! do you have a sample on how to do that? was looking for it myself :-)
@Sanabalis
@Sanabalis Жыл бұрын
​@@jptouron3080 public static class LoggingExtensions { public static IDisposable? AddCallerScope(this ILogger logger, string memberName = "", string filePath = "", long lineNumber = 0, [CallerMemberName] string methodName = "", [CallerLineNumber] long logScopeLineNumber = 0) { return logger.BeginScope("{callerMember}, {callerPath}:{callerLine} -> {method}:{lineNumber}", memberName, filePath, lineNumber, methodName, logScopeLineNumber); } } then simply use like this: public void SomeMethod(int methodArgument, [CallerMemberName] string memberName = "", [CallerFilePath] string filePath = "", [CallerLineNumber] long lineNumber = 0) { using var logScope = _logger.AddCallerScope(memberName, filePath, lineNumber); .........
@TheMAZZTer
@TheMAZZTer Жыл бұрын
CallerMemberName is useful if you are making an INotifyPropertyChanged implementation that handles multiple properties. You can have your property setters call a private helper method that has a CallerMemberName and use this to access the property name that was modified easily, so you can add it to the EventArgs.
@marsovac
@marsovac Жыл бұрын
Why not simply use an MVVM framework, caliburn prism or whatever and the PropertyChanged nuget. Completely remove them from code and implement them via some form of precompilation on classes that implement INotifyPropertyChanged. INotifyPropertyChanged and much of the WPFish stuff is just boilerplate you don't want to deal with. Use auto properties and if you need to do something override the created methods.
@aforslow
@aforslow Жыл бұрын
Merry Christmas, Nick! You’re doing awesome work with this channel!
@F1nalspace
@F1nalspace Жыл бұрын
I love the caller attributes, i use them whenever i can. But argument expressions was new for me, so thanks for sharing.
@craigfranchuk9339
@craigfranchuk9339 Жыл бұрын
Great video. I’ve been a fan of the caller member attributes for enhancing error log details for some time. I didn’t know about that feature to capture the input arguments. Very cool and thank you for sharing.
@amrosamy8232
@amrosamy8232 Жыл бұрын
One of the important things is getting the type of the returned value of the expression
@noldor__
@noldor__ Жыл бұрын
Thank you for the video. It was very helpful. These features look really handy if you're implementing deeper diagnostic logging for your code. For example, I needed to do this for a shared library I was working on. I needed to log information about what modules and classes called it, as well as the arguments it was called with, so that I could log parameters and results and have an idea about how people were using the library. This way, we could have a statistics report at the end of the day. I had to use a lot of reflection and do a lot of string parsing of the Current StackTrace, but it still wasn't enough. I had to add extra parameters to my methods so developers could pass more information. If these features were available back then, it would have been a lot of use to me.
@davidwilliss5555
@davidwilliss5555 Жыл бұрын
I can see a good use for the last two. Sometimes you cannot attach a debugger (maybe it's running at a customer's location) and to debug where something is happening, you sprinkle a lot of logging in the code. This would allow you to write a static LogDebug message that automatically knows the filename and line of where it's called and include that in the log message.
@petrucervac8726
@petrucervac8726 Жыл бұрын
CallerMemberName is used extensively in WPF
@krccmsitp2884
@krccmsitp2884 Жыл бұрын
Fits perfectly with INotifyProperyChanged.
@splith
@splith Жыл бұрын
6:16 I was going to call you out on the use of nameof for number, but of course you had it!
@TechAndMath
@TechAndMath Жыл бұрын
Hi,great video. can you do a session on model validation performance in webapi world? Always curious if fluent validation is faster or slower than the default validation attributes?
@dmitrywho7990
@dmitrywho7990 Жыл бұрын
We usually like to enrich logging with custom fields and they way we are doing it with ILogger - I believe these attributes are not as useful because there will be a number of methods in stack trace between ilogger call and actual logging handler
@MichaelBattaglia
@MichaelBattaglia Жыл бұрын
Very fascinating stuff! Question: is there an attribute that can get all of the caller values for all of the arguments when there is more than one without having to individually add a argument for each new one?
@rasimismatulin1400
@rasimismatulin1400 Жыл бұрын
If you will call Example() methods from several another methods, each compilation result will call Example() with it name. Test1() => Example("Test1") Test2() => Example("Test2")
@JoeEnos
@JoeEnos Жыл бұрын
I’m curious what happens if you mix and match languages with the argument one - like you call from VB, passing in a lambda to a C# method. I guess that’s something to try later.
@Denmaverick39
@Denmaverick39 Жыл бұрын
Definitely gonna implement the line number caller in the exception libraries
@reikooters
@reikooters Жыл бұрын
I use CallerMemberName, CallerFilePath and CallerLineNumber in a generic logging helper class that I reuse in projects which basically just contains wrappers for the Serilog Information, Warn, Error etc functions, which prepends the file/member/line number at the beginning of the message, so I can see where in the code the log was created from. For the file path, I'm using Path.GetFileNameWithoutExtension() so as to just keep the filename so logs arent cluttered with the full path. I did not know about the new Argument one, it's interesting but not sure when I'd use it.
@SherlockHolmesCologne
@SherlockHolmesCologne Жыл бұрын
This is last example is actually similar to the C++ macro __FILE__ and __LINE__, which can be useful in UTests.
@user-kz6cj6bk8f
@user-kz6cj6bk8f Жыл бұрын
Hello. Could anybody help me - what is extention are used on 7:10 as IL viewer but with higth level c#?
@fakhrulhilal
@fakhrulhilal Жыл бұрын
Wonderful, it's more on build time rather than compile time.
@ivandrofly
@ivandrofly Жыл бұрын
Thanks
@iOLlVER
@iOLlVER Жыл бұрын
Would be interesting to get the CallerArgumentExpression as an actual Expression
@zsoltszilagyi4768
@zsoltszilagyi4768 Жыл бұрын
These attributes are a great addition but still very situational. For example, they can not work with params constructs, and this really breaks their usage in most logging methods, unless you are using string interpolation in your message.
@saylorsedell2380
@saylorsedell2380 Жыл бұрын
Exactly, which is the problem I'm now running into when setting up Serilog the correct way
@klocugh12
@klocugh12 Жыл бұрын
Those could be extremely useful for debugging dlls created at runtime.
@Looooooka
@Looooooka Жыл бұрын
Is there an c# IL viewer extension for visual studio(not code)? 😤
@krccmsitp2884
@krccmsitp2884 Жыл бұрын
Have a look at "IL Spy 2022"
@anreton
@anreton Жыл бұрын
And check ILDasm.
@tmhchacham
@tmhchacham Жыл бұрын
Can you get the class name?
@haxi52
@haxi52 Жыл бұрын
I use CallerMemberName the most, its really handy for implementing IPropertyChanged. CallerFileName and line number can be really useful for error logging.
@anonimxwz
@anonimxwz 8 ай бұрын
this works in unity?
@vm123
@vm123 Жыл бұрын
Wish the added new keyword pathof(exp) which would do the same job as nameof but returning full path eg. Foo.Bar.PropName instead of just PropName
@moatazal-ali8589
@moatazal-ali8589 Жыл бұрын
Hi nick thanks a lot for yours courses and videos ... Can you please make a course for mongoDB and dynamic DTO to validate dynamic forms or dynamic JSON
@jacobstamm
@jacobstamm Жыл бұрын
AN’T WORK?
@EdKolis
@EdKolis Жыл бұрын
I feel like this feature could be used to exploit vulnerabilities in calling code when implemented in a library. The library uses this feature to find the names of methods in code that calls it, and uses this to seek out exploits by brute force calling those methods using reflection...
@EdKolis
@EdKolis Жыл бұрын
Oh wow, the expression one is even worse. A library could extract all sorts of sensitive data that whoever wrote the calling code thought the library wouldn't have access to because it's being evaluated before being passed in! Yikes!
@EdKolis
@EdKolis Жыл бұрын
e.g. CallLibrary(HashPassword(pword, "secretsalt")); exposes the salt
@paulkoopmans4620
@paulkoopmans4620 Жыл бұрын
@@EdKolis putting your salt in... right as a string there... anyone could get that out of your source with reflector, dotPeek, ilDasm. And really... that goes for all of your code. It really is a non-issue at that point.
@MaximilienNoal
@MaximilienNoal Жыл бұрын
Unless you put secrets in your version control, This isn't an issue at all.
@LeMustache
@LeMustache Жыл бұрын
Installing a malicious nuget package can already do much worse things than getting some potentially hardcoded string. Like hell, even the dumbest thing - it could literally scan the entire executable on runtime and send all the strings it finds to the attacker. If you're worried about it the actual issue is that you're using a fishy library in the first place, not the feature. It's kind of like worrying that your bathroom lock is too weak to stop a thief. Worry about your front door first.
@mysterious_commenter3285
@mysterious_commenter3285 Жыл бұрын
First
@alimorgan1407
@alimorgan1407 Жыл бұрын
First
Redis Tutorial for Beginners | Learn Redis in 90 minutes
1:37:42
ProgrammingKnowledge
Рет қаралды 7 М.
ДЕНЬ РОЖДЕНИЯ БАБУШКИ #shorts
00:19
Паша Осадчий
Рет қаралды 6 МЛН
狼来了的故事你们听过吗?#天使 #小丑 #超人不会飞
00:42
超人不会飞
Рет қаралды 65 МЛН
.NET 9 Fixed Exceptions but Still Don’t Use Them
10:01
Nick Chapsas
Рет қаралды 39 М.
"Stop Using Async Await in .NET to Save Threads" | Code Cop #018
14:05
How to manage guarantors' details on Lendsqr
3:41
Lendsqr
Рет қаралды 3
When Readonly isn’t Readonly in C#
11:57
Nick Chapsas
Рет қаралды 22 М.
C# Source Generators Tutorial
48:58
Raw Coding
Рет қаралды 24 М.
Can Light be Black? Mind-Blowing Dark Light Experiments!
7:08
The Action Lab
Рет қаралды 12 МЛН
Forget Grafana And Prometheus! Start With This.
10:51
Nick Chapsas
Рет қаралды 42 М.
The .NET dependency injection methods you are not using
11:49
Nick Chapsas
Рет қаралды 91 М.
The fastest way to iterate a List in C# is NOT what you think
13:42
Nick Chapsas
Рет қаралды 154 М.