另外还想问一下:实现一个接口只能 手动 添加该接口的属性和方法吗?有没有什么工具可以 自动 生成接口所有属性和方法的定义,然后添加代码(类似于 Global.asax 里面默认的代码)?谢谢,分不够再加。

解决方案 »

  1.   

    注意看下面的代码
    public class MyItem
    {
        ...
        public void Insert( int index, MyItem item )
        ...    void Insert( int index, obejct item )
        {
            Insert(index, (MyItem)item);
        }
      

  2.   

    另外还想问一下:实现一个接口只能 手动 添加该接口的属性和方法吗?有没有什么工具可以 自动 生成接口所有属性和方法的定义,然后添加代码(类似于 Global.asax 里面默认的代码)?
    在IDE里写完public class MyItem : IList以后停一下,应该会提示你按TAB键自动完成接口代码。
      

  3.   

    另外还想问一下:实现一个接口只能 手动 添加该接口的属性和方法吗?有没有什么工具可以 自动 生成接口所有属性和方法的定义,然后添加代码(类似于 Global.asax 里面默认的代码)?
    ---------
    1。在类视图中,点击类的接口右键,然后点击添加-实现接口。
    2。在编写类时,承继接口后停一下,按TAB键,类就自动添加其接口所有成员代码。
      

  4.   

    To henryfan1(每天好心情(*_*)) :
    > 2。在编写类时,承继接口后停一下,按TAB键,类就自动添加其接口所有成员代码。停一下然后按 Tab 键没有任何反应呀?我用的是 Visual Studio.net 2002 English
      

  5.   

    ★ To ccat(智拙):
    public class MyItem
    {
        ...
        public void Insert( int index, MyItem item )
        ...    void Insert( int index, obejct item )
        {
            Insert(index, (MyItem)item);
        }这样 Insert 被调用时的参数类型还是 object 呀,看看这个类 ListItemCollection:
    public sealed class ListItemCollection : IList, ICollection, IEnumerable, IStateManager
    它的方法 public void Insert(int index, ListItem item) 是怎么实现的呢?
      

  6.   

    就是像我那样实现的,通过重载,这在C#中称为显式接口实现,注意我的那个匹配IList.Insert(int index, object item)方法的实现没有任何访问限定符。
      

  7.   

    public class MyItem
    {
        ...
        public void Insert( int index, MyItem item )
        ...   // 实现 Ilist 的 Insert 方法
       void Insert( int index, obejct item )
        {
            Insert(index, (MyItem)item);
        }
    //你自己添加一个Insert方法
         public void Insert( int index, MyItem item )
        {
            ...
        }
      

  8.   

    ★ To ccat(智拙):
    void Insert( int index, obejct item )
    不加任何访问限定符编译出错:没有实现 Insert。你的意思是通过重载和访问限制,只公开方法重载版本,但是实现接口方法不用 public 编译就通不过。能不能把你的代码贴上来呢?谢谢。
      

  9.   

    ★ To henryfan1(每天好心情(*_*)) :
    tab 键实现接口定义的确在 2002 中没有。★ To ccat(智拙):
    如果不实现 IList 接口,改为继承 CollectionBase 也能实现。但是对:
    public sealed class ListItemCollection : IList, ICollection, IEnumerable, IStateManager
    的 Insert 方法 public void Insert(int index, ListItem item) 的实现不解,希望能够指点迷津。
      

  10.   

    public class Int16Collection : CollectionBase  {   public Int16 this[ int index ]  {
          get  {
             return( (Int16) List[index] );
          }
          set  {
             List[index] = value;
          }
       }   public int Add( Int16 value )  {
          return( List.Add( value ) );
       }   public int IndexOf( Int16 value )  {
          return( List.IndexOf( value ) );
       }   public void Insert( int index, Int16 value )  {
          List.Insert( index, value );
       }   public void Remove( Int16 value )  {
          List.Remove( value );
       }   public bool Contains( Int16 value )  {
          // If value is not of type Int16, this will return false.
          return( List.Contains( value ) );
       }
    }
    这是MSDN的例子,你可以把Int16改成你的类型
      

  11.   

    public class MyItem
    {
        ...
        public void Insert( int index, MyItem item )
        ...    void Insert( int index, obejct item )
        {
            Insert(index, (MyItem)item);
        }学了两手
      

  12.   

    Lydia_dotnet:
    确实是我写错了,不加限定符的那个应该是IList.Insert,也就是说,要加上完全引用名。详细的讲解请参见《Microsoft.NET 框架程序设计(修订版)》15.5,338页至343页的内容。