如何使接口的实现产生auto属性而不是NotImplementedException? [英] How can I make implementation of an interface produce an auto property instead of NotImplementedException?

查看:109
本文介绍了如何使接口的实现产生auto属性而不是NotImplementedException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一下,当我将鼠标移到ICar表达式上并单击Implement Interface时,我们有一个简单的界面,例如ICar,Visual Studio产生以下实现.

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.

有没有办法提供只在界面上看到的auto属性.这会导致重构问题,并使我每次都疯了!

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();
            }
        }
    }

推荐答案

单击Implement Interface时,您需要更改Visual Studio使用的模板.模板存储在以下位置:

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

您需要更改的模板称为PropertyStub.snippet

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.

您需要更新的行是:

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

线路应更改为此:

$GetterAccessibility$ get;
$SetterAccessibility$ set;

这篇关于如何使接口的实现产生auto属性而不是NotImplementedException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆