How can I make implementation of an interface prod

2019-01-18 08:48发布

Consider that we have a simple interface such as ICar when I move mouse over ICar expression and click on Implement Interface Visual Studio produce below implementation.

Is there any way of providing just an auto property as seen on interface. This cause re-factoring issues and make me crazy each time!

    public interface ICar
    {
        double Power { get; set; }
    }

    public class Car:ICar
    {

        public double Power
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
    }

4条回答
相关推荐>>
2楼-- · 2019-01-18 09:34

First, that's not an auto-property. If you want an auto-property, you have to remove what the compiler produced and replace it with

public double Power { get; set; }

That is an auto-property.

The compiler does that because it's the simplest thing that the compiler can do that produces code that will compile. I suppose that it could do auto-properties for properties on interfaces but then that introduces an inconsistency between how it handles methods (it will also generate NotImplementedException method stubs) and properties. That said, you can change this. It's handled in the snippets:

1) Open the snippets directory and find this file C:\Program Files\Microsoft Visual Studio 10.0\VC#\Snippets\1033\Refactoring\PropertyStub.snippet

2) Modify the PropertyStub.snippet file, replacing the property stubs with

 <![CDATA[$signature$ { $GetterAccessibility$ get; $SetterAccessibility$ set; }]]>

You can do the same for MethodStub.snippet so that it produces an empty body.

Now, when you auto-implement an interface using Visual Studio, you'll end up with

 public double Power { get; set; }
查看更多
劫难
3楼-- · 2019-01-18 09:40

You need to change the template used by Visual Studio when you click on Implement Interface. The template is stored in the following location:

C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC#\Snippets\1033\Refactoring

The template you need to change is called PropertyStub.snippet

NOTE: I would back up the existing snippet file before making changes so that you can easily revert if things do not go well.

The lines you need to update are:

$GetterAccessibility$ get 
{ 
    $end$throw new $Exception$(); 
}
$SetterAccessibility$ set 
{ 
    throw new $Exception$(); 
}

The lines should be changed to this:

$GetterAccessibility$ get;
$SetterAccessibility$ set;
查看更多
对你真心纯属浪费
4楼-- · 2019-01-18 09:46

You can change it in options: Tools > Options > Text Editor > C# > Advanced and on the bottom you have

t

查看更多
做个烂人
5楼-- · 2019-01-18 09:48

If people come in search trying to do this is Visual Studio 2015 its not possible to modify the refactoring snippets. After the update to Roslyn it looks like Visual studio never actually references those snippet files anymore and does it directly in Roslyn.

查看更多
登录 后发表回答