开发者

Refactor properties into field

开发者 https://www.devze.com 2023-01-20 00:33 出处:网络
If I have a field, I can generate a corresponding property by right clicking on the field -> Refactor -> Encapsulate Field.

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

Is there a way to acheieve the opposite?

I have properties like

public int Foo { get; set; }

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

so it becomes

eg.

private int _foo;
public int Foo
{
    get { return _foo;}
    set 
    {
        if (value != _foo)
        {
            _foo = value;
            Not开发者_开发百科ifyPropertyChanged("Foo");
        }
    }
}


2 options:

  1. Create a code snippet. This doesn't automatically refactor any existing properties for you, but at least it could allow you to recreate the properties quickly if you wanted to do it by hand. For example, create a "propv.snippet" file in your "My Documents\Visual Studio 2008\Code Snippets\Visual C#\My Code Snippets" folder with the following contents. You will then be able to create properties with a backing field by selecting "propv" in the intellisense popup:

    <?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's 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>
    
  2. Use the macro recording functionality in Visual Studio or another tool (Notepad++) to record the steps necessary to refactor a property, then just run that macro where needed.


There isn't an included refactoring for this inside of Visual Studio.

Unfortunately, neither Resharper nor Refactor Pro include this as a standard feature, as well. There are just too many options for implementing INPC.

That being said, I do have a Resharper template that generates a property implementing INPC. Using this, you can type a small keyword (mine is propno), hit tab, and type the name of the property - everything else, including the backing field, is generated automatically from that.


Take a look at Resharper as it can do this for you plus a lot more refactoring options. Their live templates are much easier to use than Visual Studio's snippets.

0

精彩评论

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