Code snippet for automatically creating getter / setter?

I once used a piece of code that I saw / found in the past that would turn my only statement into a private / public getter / setter, I still have not been able to repeat this find since reinstalling my machine.

eg:

private string serverSMTP = string.empty; 

Then I could press Ctrl k + and include it in this:

  private string serverSMTP = string.Empty; public string ServerSMTP { get { return serverSMTP; } set { serverSMTP = value; RaisePropertyChanged("ServerSMTP"); } } 

Any ideas on how I can create something for this or an extension / snippet to take care of it for me? In large projects, this will save me a lot of time.

+6
source share
2 answers

If you already use the MVVM Light environment, you can install the code fragments that will be delivered with it, which will do something similar. In particular, the "mvvminpc" snippet will do what you are looking for, although it will not accept an existing field declaration and convert it to a property with a notification of a property change.

http://mvvmlight.codeplex.com/sourcecontrol/latest#Installer/InstallItems/Snippets/CSharp/mvvmInpc.snippet

Code snippets to speed up the addition of new properties (Visual Studio only):
mvvminpc adds a new bindable property to the ViewModel.
mvvmlocatorproperty adds a new ViewModel to the ViewModeLocator.
mvvmpropa adds a new attached property to DependencyObject (WPF only).
mvvmpropdp adds a new dependency property to DependencyObject (WPF only).
mvvmslpropa adds a new attached property to DependencyObject (Silverlight only).
mvvmslpropdp adds a new dependency property to the DependencyObject (Silverlight only).

+6
source

put this snippet:

 <?xml version="1.0" encoding="utf-8"?> <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> <CodeSnippet Format="1.0.0"> <Header> <Shortcut>propn</Shortcut> <Title> Notify Property </Title> </Header> <Snippet> <Declarations> <Literal> <ID>type</ID> <ToolTip>Type</ToolTip> <Default>int</Default> </Literal> <Literal> <ID>field</ID> <ToolTip>Field name</ToolTip> <Default>fieldName</Default> </Literal> <Literal> <ID>property</ID> <ToolTip>Propery Name</ToolTip> <Default>PropertyName</Default> </Literal> </Declarations> <Code Language="CSharp"> <![CDATA[ private $type$ $field$; public $type$ $property$ { get { return $field$; } set { $field$ = value; if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("$property$")); } } $end$]]> </Code> </Snippet> </CodeSnippet> </CodeSnippets> 

inside the propn.snippet file, in this folder: C: \ Users [YOUR_USERNAME] \ Documents \ Visual Studio 2010 \ Code Snippets \ Visual C # \ My Code Snippets

after which you can use this fragment using the shortcut (propn + tab + tab).

The xml snippet is very easy to understand on its own, so you can easily customize it for everything you need.

+3
source

Source: https://habr.com/ru/post/888353/


All Articles