财产

2010-04-28 119 views
-1

通用接口我有一个接口财产

/// <summary> 
/// Summary description for IBindable 
/// </summary> 
public interface IBindable<T> 
{ 
    // Property declaration: 
    T Text 
    { 
     get; 
     set; 
    } 
} 

现在我想实现这个接口在我的课

public class MyTextBox :IBindable<string> 
{ 
    //now i how can i implement Text peroperty here 
} 

我不想要实现它像

string IBindable<string>.Text 
{ 
    get { return "abc";} 
    set { //assigne value } 
} 

我想实现它像

public string Text 
{ 
    get{} set {} 
} 
+0

我不能把握的问题......你可以做你说什么。 .. – digEmAll 2010-04-28 17:53:32

+0

这里有什么问题? – 2010-04-28 17:55:01

+0

我猜OP没有意识到这是有效的...... – 2010-04-28 17:56:08

回答

5

您可以自由做到这一点。这是一个隐含的接口实现。

以下是有效的C#:

public interface IBindable<T> 
{ 
    // Property declaration: 
    T Text 
    { 
     get; 
     set; 
    } 
} 

public class MyTextBox : IBindable<string> 
{ 

    public string Text 
    { 
     get; 
     set; 
    } 
} 

当你实现一个接口,你可以自由地隐式实现它,因为上面,或者明确地说,这将是你的第二个选项:

string IBindable<string>.Text 
{ get { return "abc";} set { // assign value } } 

区别在于使用。当您使用第一个选项时,Text属性成为该类型本身的公开可见属性(MyTextBox)。这允许:

MyTextBox box = new MyTextBox(); 
box.Text = "foo"; 

但是,如果你明确地实现它,你需要直接使用你的接口:

MyTextBox box = new MyTextBox(); 
IBindable<string> bindable = box; 
box.Text = "foo"; // This will work in both cases 
2
public class MyTextBox : IBindable<string> 
{ 
    //now i how can i implement Text peroperty here 
    public string Text 
    { 
     get; 
     set; 
    } 
}