开发者

ReSharper: Can I make my own context action?

开发者 https://www.devze.com 2022-12-25 17:38 出处:网络
ReSharper has a \"Check parameter for null\" context action, that will automatically insert code to check an argument for null and throw ArgumentNullException if it is null.

ReSharper has a "Check parameter for null" context action, that will automatically insert code to check an argument for null and throw ArgumentNullException if it is null.

If the parameter is a string, I would like another option: "Check string for Null or empty". This should generate code something similar to this:

if (String.IsNullOrEmpty(result))
    throw new ArgumentException("Parameter cannot be null or empty", "result");

Is the开发者_开发技巧re any way to add this to ReSharper easily ?


I've created ReSharper live templates for check for null and check for empty string.

So pnn + Enter inserts a parameter not null check etc.

The code for pnn looks like this

if($ARG$ == null)
   throw new $ARGNULLEXC$("$ARG$");$END$

Where $ARG$ is "Suggest parameter of type System.Object" and $ARGNULLEXC$ is "insert reference to System.ArgumentNullException". The $END$ indicates where your cursor should be after inserting the live template.

For the string variant, you would assign $ARG$ something like "Suggest parameter of type System.String".

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号