By default, JetBrains Rider provides the list of file masks and regions for generated code typically produced by Visual Studio (for example, Windows Form Designer-generated code). JetBrains Rider will make fields readonly in the selected scope.Ĭode cleanup is not applied to generated code.
In the Reformat and Cleanup Code dialog that opens, select the newly created profile and choose another scope if needed.Ĭlick OK. Press Ctrl+E, C or choose Code | Reformat and Cleanup. Select one or more items in the Solution Explorer to make fields readonly in the files under these nodes and their child items. Set the caret anywhere in the file to make fields readonly to the file. Select the scope where you want to make fields readonly: For more information, see Layer-based settings. Optionally, you can enable other code cleanup tasks in this profile.Ĭlick Save in the Settings dialog to apply the modifications and let JetBrains Rider choose where to save them, or save the modifications to a specific settings layer using the Save To list. In the Selected profile settings section for the new profile, tick the Make field read-only, if possible checkbox. Go to the cleanup profiles settings page: Editor | Code Cleanup.Ĭreate a new profile as described in the Create a new custom cleanup profile section. Press Ctrl+Alt+S or choose File | Settings (Windows and Linux) or JetBrains Rider | Preferences (macOS) from the menu. Add the 'readonly' keyword to fields that only have read access
You can either run code cleanup with the Built-in: Full Cleanup profile or create and run a custom profile solely targeted at your specific task as described below. If you decide to change a readonly field later in your code (which leads to a compiler error), JetBrains Rider detects the error and suggests fixing it by removing the 'readonly' keyword from the field:Īnother option to make fields readonly in a bulk mode is code cleanup. The reverse functionality is also available. If you do not want JetBrains Rider to suggest this, you can disable the code inspection that detects it ( Field can be made readonly).
For more information, see Code Syntax Style: Modifiers. With JetBrains Rider, you can also automatically arrange the order of modifiers and add or remove explicit private and internal. It highlights such fields and suggests the corresponding quick-fix or fix in scope: By default, JetBrains Rider suggests that you make a field readonly if the above conditions are met. This answer is superior to any answers that only work if the current process has the focus, such as SendKeys and Input Simulator.It is often reasonable to use the 'readonly' modifier for fields that are assigned in the declaration or in a constructor and only have read usages. Abort on ReadLine() won't work in C#, as ReadLine() is running in unmanaged code deep within the Windows kernel. This answer also works around the fact that calling. PostMessage(hWnd, WM_KEYDOWN, VK_RETURN, 0) Ĭonsole.Write("ReadLine() successfully aborted by background thread.n") Private static extern bool PostMessage(IntPtr hWnd, uint msg, int wParam, int lParam) Ĭonsole.Write("Switch focus to another window now to verify this works in a background process.n") Use PostMessage to send into the current process: class Program