Visual Studio Tips

Using JetBrains Code Annotations

Posted by matteskolin on

List of Attributes https://www.jetbrains.com/help/resharper/Reference__Code_Annotation_Attributes.html

How to Install
run the below in your .NET project directory

dotnet add package JetBrains.Annotations

The JetBrains.Annotations library provides ways to improve the quality of the Resharper Code Analysis. By applying the annotation attributes, the code suggestions, intellisense, and warnings can be improved as resharper is provided with information that it cannot determine without them by examining the original code. There many attributes available; see link above.

Example: [UsedImplicity]

This attribute indicates that the marked symbol is used implicity, in a way which is not detectable by code analysis. Some examples are via reflection, an external librar, or as a record type argument like in the below example..

You can use ImplicitUseKindFlags and ImplicitUseTargetFlags to configure how this attribute is applied.

Before adding attribute, resharper reports a warning that Response is not used.. even though we can see this type being passed as the type argument of the IRequest above.. Removing the Response record type, even causes a compile error, but resharper is unable to detect this.. This may be because the actual instantiation is happening inside of the MediateR library, which is not being analyzed..

After adding the attribute..

Visual Studio Tips

Powershell Tips

Posted by matteskolin on

I’ve been gathering all my powershell learnings together into a library of useful functions that make my life easier as a programmer. I will use this blog to share some of the useful scripts, and hopefully in the process help me keep them organized and at my fingertips for easier recall when needed..

Tip 1: Search for a string within a directory

Windows provides a search box at the top of the file explorer window, but I have always had issues getting it to work well, especially over corporate networks and in directories with a large number of files. This has to do with how windows indexes the file system for searching.

Luckily, powershell provides us with the Select-String cmdlet which can be used to quickly search large directories of files for the a specific string or regular expression pattern.

There are multiple basic ways to search.. using a file path, by piping in fileobjects, or my using a variable containing string content.

Here we search all the file objects in the Dir1 directory for the pattern “content” which is found on three seperate lines in Content4.txt

Select-String -Path "./StringSearch/Dir1/*" -Pattern "content"

StringSearch\Dir1\Content4.txt:1:Content4
StringSearch\Dir1\Content4.txt:2:Content4
StringSearch\Dir1\Content4.txt:3:Content4

Here we use Get-ChildItem to pipe file objects to Select-String for searching. In this example, the current directory will be searched . The recurse option allows searching of all subdirectories.

Select-String returns a stream of MatchInfo objects on which we select all properties. I find it very helpful when learning a new command to display all the variables in order to better understand what is happening. For instance, here we see that case is being ignored, the line number, full path, and a list of regex matches found on the line. In order to search for more than one match per line, us the -AllMatches switch

Get-ChildItem -recurse | Select-String -pattern "Content4" | Select-Object *

IgnoreCase : True
LineNumber : 1
Line       : Content4
Filename   : Content4.txt
Path       : C:\src\gh\PowerShellCookbook\StringSearch\Dir1\Content4.txt
Pattern    : Content4
Context    :
Matches    : {0}

IgnoreCase : True
LineNumber : 2
Line       : Content4
Filename   : Content4.txt
Path       : C:\src\gh\PowerShellCookbook\StringSearch\Dir1\Content4.txt
Pattern    : Content4
Context    :
Matches    : {0}

IgnoreCase : True
LineNumber : 3
Line       : Content4
Filename   : Content4.txt
Path       : C:\src\gh\PowerShellCookbook\StringSearch\Dir1\Content4.txt
Pattern    : Content4
Context    :
Matches    : {0}

Instead of piping the input into select-string, we can use the -InputObject parameter to specify a string to search from a previously defined variable. Here we also use format-table to format the output in fewer lines while still showing all of the properties.

$sstring = "test1 test2 test3"
Select-String -InputObject $sstring -Pattern "test"|Format-Table
IgnoreCase LineNumber Line              Filename    Path        Pattern Context Matches
---------- ---------- ----              --------    ----        ------- ------- -------
      True          1 test1 test2 test3 InputStream InputStream test            {0}


A Look at some of the other Parameters of Select-String

Select-String
[-Culture ]
[-Pattern]
[-Path]
[-SimpleMatch]
[-CaseSensitive]
[-Quiet]
[-List]
[-NoEmphasis]
[-Include ]
[-Exclude ]
[-NotMatch]
[-AllMatches]
[-Encoding ]
[-Context ]

That’s a lot of options..

Using -Raw

link to another page!



Microsoft Docs on this Command
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-string?view=powershell-7.3

Alternative way to search for strings in files with NotePad++ (this works great and allows easy navigation to the files, I prefer to use this instead of powershell when I can)

https://npp-user-manual.org/docs/searching/
Easier Searching with NotePad++ “Find in Files”

Search all files in any directory using NotePad++ Find in Files dialog. Control + Shift + F, will open the find in files window directory, skipping the extra step of clicking on “Find in Files” tab.. use shift key for extra productivity here..

The results display in the lower pane of Notepad++, you can even click the lines to jump to the file, and the matching search text is highlighted, very nice!