Custom null checking for dependencies
In a constructor with several dependencies I typically use the context action "Introduce read only field '_dependecyName'", and then "Check parameter for null" which results in a constructor that looks like:
public Constructor(Type dependecy)
{
_dependency = dependency ?? throw ArgumentNullException(nameof(dependency));
}
However, in my team the standard way of checking arguments for null is the following:
public Constructor(Type dependecy)
{
_dependency = dependency.CheckForNull(nameof(dependency));
}
I have set a custom statement for Null Checking:
$EXPR$.CheckForNull($NAME$);
When I use the "Introduce read only field" and "Check parameter for null" context actions the resulting code looks like:
public Constructor(Type dependecy)
{
dependency.CheckForNull(nameof(dependency));
_dependecy = dependency;
}
Is there something I can do so that ReSharper does the check and the assignment on the same line?
Please sign in to leave a comment.
Hello Mccloy,
Thank you for contacting us.
I'd suggest you try Structural Search and Replace feature - https://www.jetbrains.com/help/resharper/Navigation_and_Search__Structural_Search_and_Replace.html.
E.g. you can search with a pattern for
string and replace it with
Please let me know if you have any more questions.
Thank you.
Thanks Angelina, this works great.
One addition however, my search pattern is:
$a$ = $b$;
Where a and b are both identifiers, and the regex for a is _.+ (so that a is any variable starting with an underscore).
Is it possible to apply regex so that b matches the name of a without the underscore?
Eg.
_foo = foo; Should match, but
_foo = bar; should not match
Thanks,
Alex
Hello Alex,
Unfortunately, there's no way to create such pattern.
It could be possibly done if macros are implemented in Structural Search and Replace, please refer to the following request - http://youtrack.jetbrains.com/issue/RSRP-166388.
Thank you.
Hi all!
I ended up on this page, as I was looking for an option to change/extend the "Introduce read-only field 'variable'", which I suspect it is an Context Action.
As I'm dealing with a lot of new classes at the moment, it would save me a great deal of time to extend this feature, that it continues with the null check as well " ?? throw new ArgumentNullException(nameof('varialbe'));"
How is this accomplished? I have searched and looked everywhere but I cannot find this info how one change/extend this option (context action)
Hello Johan,
Please refer to the following help article - https://www.jetbrains.com/help/resharper/Reference__Options__Languages__CSharp__Null_Checking.html#exception_checks.
You may use predefined null-check pattern or create your own.
Thank you.
Hello Angelina,
I see what you mean, and I'm aware of this feature/function.
But I'm interested to extend the "Introduce read-only field 'variable'". So instead of the existing result:
I would like to extend that one to this one:
I found very old guidelines regarding this, but it's not giving me the complete picture how I change this on my own and extend this context action.
Could you please provide me with some info regarding this?
Thank you!
Hello @...,
Unfortunately, there is no such functionality in ReSharper. I see the following options for improving your workflow:
- If you are using C#8 and your project has nullable context enabled, then you can set the caret directly after the parameter name or parameter type and press "!";
- Or you can generate a constructor using ReSharper with the "Check parameters for null" option enabled: https://www.jetbrains.com/help/resharper/Code_Generation__Type_Constructors.html#492ce
Hope this helps! Please let me know if you have any questions.