Field refactoring properties

If I have a field, I can generate the corresponding property by right-clicking on the field -> Refactor -> Encapsulate Field.

Is there a way to accept the opposite?

I have properties like

public int Foo { get; set; }

I want to create private fields and change getter and setter to use this field. Then I can implement INotifyPropertyChanged and change the setter to fire the PropertyChanged event when the property value changes.

therefore he becomes

eg.

private int _foo;
public int Foo
{
    get { return _foo;}
    set 
    {
        if (value != _foo)
        {
            _foo = value;
            NotifyPropertyChanged("Foo");
        }
    }
}
+3
source share
3 answers

2 options:

  • . , , . , "propv.snippet" " \Visual Studio 2008\Code Snippets\Visual #\My Code Snippets" . , "propv" intellisense:

    <?xml version="1.0" encoding="utf-8" ?>
    <CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
        <CodeSnippet Format="1.0.0">
            <Header>
                <Title>propv</Title>
                <Shortcut>propv</Shortcut>
                <Description>Code snippet for property and backing field</Description>
                <Author>Dr. Wily Apprentice</Author>
                <SnippetTypes>
                    <SnippetType>Expansion</SnippetType>
                </SnippetTypes>
            </Header>
            <Snippet>
                <Declarations>
                    <Literal>
                        <ID>type</ID>
                        <ToolTip>Property type</ToolTip>
                        <Default>int</Default>
                    </Literal>
                    <Literal>
                        <ID>property</ID>
                        <ToolTip>Property name</ToolTip>
                        <Default>MyProperty</Default>
                    </Literal>
                    <Literal>
                        <ID>field</ID>
                        <ToolTip>The variable backing this property</ToolTip>
                        <Default>myVar</Default>
                    </Literal>
                </Declarations>
                <Code Language="csharp">
                    <![CDATA[private $type$ $field$;
    
    
    public $type$ $property$
    {
    get { return $field$;}
    set { $field$ = value;}
    }$end$]]>
                </Code>
            </Snippet>
        </CodeSnippet>
    </CodeSnippets>
    
  • Visual Studio (Notepad ++) , , , .

+2

Visual Studio .

, Resharper, Refactor Pro . INPC.

Resharper, , INPC. , (my is propno), - , , .

+1

Resharper, . , Visual Studio.

+1

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


All Articles