2014-02-05 39 views
0

BindingList<T> implements IBindingList。一對IBindingList方法是爲什麼BindingList <T>不得不完全實現其接口?

void ApplySort(PropertyDescriptor property, ListSortDirection direction); 

BindingList<T>未實現此方法。相反,它實現了

protected virtual void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction); 

顯然ApplySortCore有不同的名稱和不公開的,所以我看不出它如何能滿足IBindingList接口的要求,然而,這是最接近什麼IBindingList的實際調用。

那麼這個類怎麼可能沒有完全實現它的接口呢?

+2

你從哪裏得到這些信息? MSDN文檔在明確的接口實現下列出它。 –

+1

http://msdn.microsoft.com/en-us/library/bb358213(v=vs.110).aspx –

+0

@LorentzVedeler我現在看到了。我只是在視覺工作室打F12。但我去了文檔,我現在看到它明確實施。你無法通過點擊F12來判斷。謝謝。 – BVernon

回答

3

我猜BindingList使用顯式接口實現來隱藏接口成員。

試試這個

interface IX 
{ 
    public string Var {get;} 
} 

public class X : IX 
{ 
    string IX.Var { get { return "x"; } } 
} 

public class Y 
{ 
    public Y() 
    { 
     X x = new X(); 
     string s = x.Var; // Var is not visible and gives a compilation error. 

     string s2 = ((IX)x).Var; // this works. Var is not hidden when using interface directly. 
    } 
} 
+1

術語是_explicit接口implementation_。 MSDN鏈接:http://msdn.microsoft.com/en-us/library/aa288461(v=vs.71).aspx –

+0

@PatrickHofman謝謝。我打F12看看課堂上有什麼,但是我沒有意識到它不會列出明確實施的方法,甚至沒有考慮到它。 – BVernon

1

綁定列表<T> 確實實際上實現了ApplySort,但它是作爲顯式接口實現的。這意味着如果你有一個變量BindingList <T>你不會看到它的ApplySort方法,但如果你轉換爲IBindingList的變量,你會看到ApplySort方法。顯式接口實現僅在您將對象視爲該接口時纔可見。

如果您查看MSDN documentation並向下滾動到標題「顯式接口實現」,您將看到列出的ApplySort(以及其他許多方法)。

相關問題