3

AcceptChanges 的默认接口 impl 的“覆盖”在将其转换为包含默认接口实现的接口类型后不会调用默认接口 impl。为什么?

public interface IItemViewModel1
{
    void AcceptChanges() => Debug.Print("In interface");
}

public class ItemViewModelBase1<TModel> : IItemViewModel1
    where TModel : new()
{
    private TModel _data;

    public void AcceptChanges()
    {
        Debug.Print("In ViewModel");
        _data ??= new TModel();
        ((IItemViewModel1)this).AcceptChanges(); // calling itself rather than default interface implementation
    }
}

public static class TestClass
{
    public static void Test()
    {
        IItemViewModel1 x = new ItemViewModelBase1<int>();

        x.AcceptChanges();
    }
}
4

0 回答 0