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

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

问题描述

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

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

推荐答案

当您点击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.0VC#Snippets1033Refactoring

您需要更改的模板名为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;

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

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