如何实现接口的默认方法?

时间:2014-10-11 11:48:27

标签: .net vb.net visual-studio interface

在.Net中创建新类时,如果我声明了它,那么'#34;实现IDisposable"并点击回车,我看到Visual Studio已经将填充的itselt不同方法和函数添加到我的班级。当我尝试使用我的界面时,它会创建方法和函数。

有没有办法提供我的方法和函数的默认实现?

我一直在寻找Link,但它并没有解决我的问题。

我正在寻找的实施示例:

#Region "IDisposable Support"
    Private disposedValue As Boolean ' To detect redundant calls

    ' IDisposable
    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                ' TODO: dispose managed state (managed objects).
            End If

            ' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
            ' TODO: set large fields to null.
        End If
        Me.disposedValue = True
    End Sub

    ' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
    'Protected Overrides Sub Finalize()
    '    ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
    '    Dispose(False)
    '    MyBase.Finalize()
    'End Sub

    ' This code added by Visual Basic to correctly implement the disposable pattern.
    Public Sub Dispose() Implements IDisposable.Dispose
        ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region

提前干杯。

进一步阐述我正在寻找的东西:

让我们假设以下界面

enter image description here

以下是我正在寻找的事情:

enter image description here

3 个答案:

答案 0 :(得分:0)

它知道如何设置基本的IDisposable实现,因为你应该遵循一个共同的模式。

但它怎么可能知道如何实现你的接口?所以你得到了你需要填写的空方法。

您最接近的赌注是设置snippet并插入,而不是让visual studio添加未实现的成员。

答案 1 :(得分:0)

我能想到的壁橱是创建自己的项目模板,可以从visual studio的Add New Item对话框中选择。您可以修改重构模板,但这会修改​​所有Interface实现。 这里讨论创建项模板:  http://msdn.microsoft.com/en-us/library/tsyyf0yh.aspx

答案 2 :(得分:0)

这个问题很旧,但是其他有相同问题的人可能会来到这里。

以上答案已过时

从C#8开始,您可以拥有default interface methods

文档中的Safely update interfaces using default interface methods in C#文章解释得很好。

以下是来自文档的示例。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace customer_relationship
{
    public interface ICustomer
    {
        IEnumerable<IOrder> PreviousOrders { get; }

        DateTime DateJoined { get; }
        DateTime? LastOrder { get; }
        string Name { get; }
        IDictionary<DateTime, string> Reminders { get; }

        public static void SetLoyaltyThresholds(TimeSpan ago, int minimumOrders, decimal percentageDiscount) // See how a method has a default implementation.
        {
            length = ago;
            orderCount = minimumOrders;
            discountPercent = percentageDiscount;
        }
        private static TimeSpan length = new TimeSpan(365 * 2, 0, 0, 0); // two years
        private static int orderCount = 10;
        private static decimal discountPercent = 0.10m;

        public decimal ComputeLoyaltyDiscount() => DefaultLoyaltyDiscount(this);
        protected static decimal DefaultLoyaltyDiscount(ICustomer c)
        {
            DateTime start = DateTime.Now - length;

            if ((c.DateJoined < start) && (c.PreviousOrders.Count() > orderCount))
            {
                return discountPercent;
            }
            return 0;
        }
    }
}