您使用哪些适用于 C# 的 ReSharper 4+ 实时模板?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/186970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:10:19  来源:igfitidea点击:

What ReSharper 4+ live templates for C# do you use?

c#templatesrefactoringresharper

提问by Rinat Abdullin

What ReSharper 4.0 templates for C#do you use?

您使用哪些C# 的ReSharper 4.0 模板?

Let's share these in the following format:

让我们以以下格式分享这些:



[Title]

[标题]

Optional description

可选说明

Shortcut:shortcut
Available in:[AvailabilitySetting]

快捷方式:快捷方式 适用
于:[AvailabilitySetting]

// Resharper template code snippet
// comes here

Macros properties(if present):

宏属性(如果存在):

  • Macro1- Value - EditableOccurence
  • Macro2- Value - EditableOccurence
  • 宏 1- 值 - EditableOccurence
  • 宏 2- 值 - EditableOccurence


回答by Rinat Abdullin

Create new unit test fixture for some type

为某种类型创建新的单元测试装置

Shortcut:ntf
Available in:C# 2.0+ files where type member declaration or namespace declaration is allowed

快捷方式:ntf 适用
于:允许类型成员声明或命名空间声明的 C# 2.0+ 文件

[NUnit.Framework.TestFixtureAttribute]
public sealed class $TypeToTest$Tests
{
    [NUnit.Framework.TestAttribute]
    public void $Test$()
    {
        var t = new $TypeToTest$()
        $END$
    }
}

Macros:

宏:

  • TypeToTest- none - #2
  • Test- none - V
  • TypeToTest- 无 - #2
  • 测试- 无 - V

回答by Rinat Abdullin

Create new stand-alone unit test case

创建新的独立单元测试用例

Shortcut:ntc
Available in:C# 2.0+ files where type member declaration is allowed

快捷方式:ntc 适用
于:允许类型成员声明的 C# 2.0+ 文件

[NUnit.Framework.TestAttribute]
public void $Test$()
{
    $END$
}

Macros:

宏:

  • Test- none - V
  • 测试- 无 - V

回答by Ed Ball

Implement 'Dispose(bool)' Method

实现 'Dispose(bool)' 方法

Implement Joe Duffy's Dispose Pattern

实现Joe Duffy 的 Dispose 模式

Shortcut:dispose

快捷方式:处置

Available in:C# 2.0+ files where type member declaration is allowed

适用于:允许类型成员声明的 C# 2.0+ 文件

public void Dispose()
{
    Dispose(true);
    System.GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
    if (!disposed)
    {
        if (disposing)
        {
            if ($MEMBER$ != null)
            {
                $MEMBER$.Dispose();
                $MEMBER$ = null;
            }
        }

        disposed = true;
    }
}

~$CLASS$()
{
    Dispose(false);
}

private bool disposed;

Macros properties:

宏属性

  • MEMBER- Suggest variable of System.IDisposable - Editable Occurence #1
  • CLASS- Containing type name
  • 成员- System.IDisposable 的建议变量 - 可编辑出现次数 #1
  • CLASS- 包含类型名称

回答by Rinat Abdullin

Create test case stub for NUnit

为 NUnit 创建测试用例存根

This one could serve as a reminder (of functionality to implement or test) that shows up in the unit test runner (as any other ignored test),

这个可以作为一个提醒(实现或测试的功能),出现在单元测试运行器中(作为任何其他被忽略的测试),

Shortcut:nts
Available in:C# 2.0+ files where type member declaration is allowed

快捷方式:nts 适用
于:允许类型成员声明的 C# 2.0+ 文件

[Test, Ignore]
public void $TestName$()
{
    throw new NotImplementedException();
}
$END$

回答by Rinat Abdullin

Create sanity check to ensure that an argument is never null

创建健全性检查以确保参数永远不会为空

Shortcut:eann
Available in:C# 2.0+ files where type statement is allowed

快捷方式:eann 适用
于:允许类型语句的 C# 2.0+ 文件

Enforce.ArgumentNotNull($inner$, "$inner$");

Macros:

宏:

  • inner- Suggest parameter - #1
  • 内部- 建议参数 - #1

Remarks:Although this snippet targets open source .NET Lokad.Sharedlibrary, it could be easily adapted to any other type of argument check.

备注:尽管此代码段针对开源 .NET Lokad.Shared库,但它可以轻松适应任何其他类型的参数检查。

回答by Ray Hayes

Trace - Writeline, with format

跟踪 - 写行,带格式

Very simple template to add a trace with a formatted string (like Debug.WriteLine supports already).

使用格式化字符串添加跟踪的非常简单的模板(如 Debug.WriteLine 已经支持)。

Shortcut:twlf
Available in:C# 2.0+ files where statement is allowed

快捷方式:twlf 适用
于:允许使用语句的 C# 2.0+ 文件

Trace.WriteLine(string.Format("$MASK$",$ARGUMENT$));

Macros properties:

宏属性:

  • Argument- value- EditableOccurence
  • Mask- "{0}"- EditableOccurence
  • 参数- value- EditableOccurence
  • 掩码- "{0}"- EditableOccurence

回答by Kjetil Klaussen

Assert.AreEqual

断言.相等

Simple template to add asserts to a unit test

将断言添加到单元测试的简单模板

Shortcut: ae
Available in: in C# 2.0+ files where statement is allowed

快捷方式:ae
可用于:在允许声明的 C# 2.0+ 文件中

Assert.AreEqual($expected$, $actual$);$END$

Fluent version :

流畅版:

Assert.That($expected$, Is.EqualTo($actual$));$END$

回答by Ian G

New COM Class

新的 COM 类

Shortcut: comclass

快捷方式:comclass

Available in: C# 2.0+ files where type member declaration or namespace declaration is allowed

适用于:允许类型成员声明或命名空间声明的 C# 2.0+ 文件

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[Guid("$GUID$")]
public class $NAME$ : $INTERFACE$
{
    $END$
}

Macros

  • GUID- New GUID
  • NAME- Editable
  • INTERFACE- Editable
  • GUID- 新的 GUID
  • 名称- 可编辑
  • 界面- 可编辑

回答by Drew Noakes

Assert Invoke Not Required

不需要断言调用

Useful when developing WinForms applications where you want to be sure that code is executing on the correct thread for a given item. Note that Controlimplements ISynchronizeInvoke.

在开发 WinForms 应用程序时很有用,您希望确保代码在给定项目的正确线程上执行。请注意,Control实现ISynchronizeInvoke.

Shortcut: ani

快捷方式:阿尼

Available in: C# 2.0+ files statement is allowed

适用于:允许 C# 2.0+ 文件语句

Debug.Assert(!$SYNC_INVOKE$.InvokeRequired, "InvokeRequired");

Macros

  • SYNC_INVOKE- Suggest variable of System.ComponentModel.ISynchronizeInvoke
  • SYNC_INVOKE- 建议变量System.ComponentModel.ISynchronizeInvoke

回答by Drew Noakes

Invoke if Required

需要时调用

Useful when developing WinForms applications where a method should be callable from non-UI threads, and that method should then marshall the call onto the UI thread.

在开发 WinForms 应用程序时很有用,其中方法应该可以从非 UI 线程调用,然后该方法应该将调用编组到 UI 线程上。

Shortcut: inv

快捷方式:inv

Available in: C# 3.0+ files statement is allowed

适用于:允许使用 C# 3.0+ 文件语句

if (InvokeRequired)
{
    Invoke((System.Action)delegate { $METHOD_NAME$($END$); });
    return;
}

Macros

  • METHOD_NAME- Containing type member name
  • METHOD_NAME- 包含类型成员名称


You would normally use this template as the first statement in a given method and the result resembles:

您通常会将此模板用作给定方法中的第一条语句,结果类似于:

void DoSomething(Type1 arg1)
{
    if (InvokeRequired)
    {
        Invoke((Action)delegate { DoSomething(arg1); });
        return;
    }

    // Rest of method will only execute on the correct thread
    // ...
}