我在VB中覆盖一个列表。
在C#中代码编译并看起来像这样:
class MyObjectCollection : IList
{
...
/// <summary>
/// Gets or sets the element at the specified index.
/// </summary>
public MyObject this[int index]
{
get { return (MyObject)innerArray[index]; }
set { innerArray[index] = value; }
}
...
}
在VB.NET中我转换:
Class MyObjectCollection
Implements IList
...
''' <summary> '
''' Gets or sets the element at the specified index. '
''' </summary> '
Default Public Overrides Property Item(ByVal index As Integer) As MyObject
Get
Return DirectCast(innerArray(index), MyObject)
End Get
Set(ByVal value As MyObject)
innerArray(index) = value
End Set
End Property
...
End Class
错误:
'公开覆盖默认属性 Item(index As Integer)As MyObject' 和'公共默认财产 Item(index As Integer)As Object' 不能互相超载,因为 它们的区别仅在于返回类型
C#中的整个集合类
public class MyObjectCollection : IList
{
private ArrayList innerArray;
public MyObjectCollection()
{
innerArray = new ArrayList();
}
public int Count
{
get { return innerArray.Count; }
}
public bool IsFixedSize
{
get { return false; }
}
public bool IsReadOnly
{
get { return false; }
}
public bool IsSynchronized
{
get { return false; }
}
object ICollection.SyncRoot
{
get { return null; }
}
public MyObject this[int index]
{
get { return (MyObject)innerArray[index]; }
set { innerArray[index] = value; }
}
public int Add(MyObject value)
{
int index = innerArray.Add(value);
return index;
}
public void AddRange(MyObject[] array)
{
innerArray.AddRange(array);
}
public void Clear()
{
innerArray.Clear();
}
public bool Contains(MyObject item)
{
return innerArray.Contains(item);
}
public bool Contains(string name)
{
foreach (MyObject spec in innerArray)
if (spec.Name == name)
return true;
return false;
}
public void CopyTo(MyObject[] array)
{
innerArray.CopyTo(array);
}
public void CopyTo(MyObject[] array, int index)
{
innerArray.CopyTo(array, index);
}
public IEnumerator GetEnumerator()
{
return innerArray.GetEnumerator();
}
public int IndexOf(MyObject value)
{
return innerArray.IndexOf(value);
}
public int IndexOf(string name)
{
int i = 0;
foreach (MyObject spec in innerArray)
{
if (spec.Name == name)
return i;
i++;
}
return -1;
}
public void Insert(int index, MyObject value)
{
innerArray.Insert(index, value);
}
public void Remove(MyObject obj)
{
innerArray.Remove(obj);
}
public void Remove(string name)
{
int index = IndexOf(name);
RemoveAt(index);
}
public void RemoveAt(int index)
{
innerArray.RemoveAt(index);
}
public MyObject[] ToArray()
{
return (MyObject[])innerArray.ToArray(typeof(MyObject));
}
#region Explicit interface implementations for ICollection and IList
void ICollection.CopyTo(Array array, int index)
{
CopyTo((MyObject[])array, index);
}
int IList.Add(object value)
{
return Add((MyObject)value);
}
bool IList.Contains(object obj)
{
return Contains((MyObject)obj);
}
object IList.this[int index]
{
get
{
return ((MyObjectCollection)this)[index];
}
set
{
((MyObjectCollection)this)[index] = (MyObject)value;
}
}
int IList.IndexOf(object obj)
{
return IndexOf((MyObject)obj);
}
void IList.Insert(int index, object value)
{
Insert(index, (MyObject)value);
}
void IList.Remove(object value)
{
Remove((MyObject)value);
}
#endregion
}
答案 0 :(得分:1)
我建议继承CollectionBase
,它专门用于创建强类型集合。
Public Class MyObjectCollection
Inherits CollectionBase
结帐example。
答案 1 :(得分:1)
Reflector告诉我你可能尝试:
Private Property System.Collections.IList.Item(ByVal index As Integer) As Object
Get
Return Me.Item(index)
End Get
Set(ByVal value As Object)
Me.Item(index) = DirectCast(value, MyObject)
End Set
End Property
Public Default Property Item(ByVal index As Integer) As MyObject
Get
Return DirectCast(Me.innerArray.Item(index), MyObject)
End Get
Set(ByVal value As MyObject)
Me.innerArray.Item(index) = value
End Set
End Property
但是,没有保证。
答案 2 :(得分:1)
如果我正确理解了这个问题,你现在有一个方法:
Public Overrides Property Item(ByVal index As Integer) As MyObject
Get
Return DirectCast(innerArray(index), MyObject)
End Get
Set(ByVal value As MyObject)
innerArray(index) = value
End Set
End Property
您还有一种实现IList接口的方法,如下所示:
Public Property Item(ByVal index As Integer) As Object Implements IList.Item
Get
Return DirectCast(innerArray(index), MyObject)
End Get
Set(ByVal value As Object)
innerArray(index) = value
End Set
End Property
这不起作用,因为方法名称相同,但返回类型不同。您需要做的是更改IList实现方法的名称。
尝试类似:
Public Property IList_Item(ByVal index As Integer)As Object Implements IList.Item
Get
Return DirectCast(innerArray(index), MyObject)
End Get
Set(ByVal value As Object)
innerArray(index) = value
End Set
End Property
答案 3 :(得分:0)
object IList.this[int index]
{
get
{
return ((MyObjectCollection)this)[index];
}
set
{
((MyObjectCollection)this)[index] = (MyObject)value;
}
}
这就是为什么您还可以使用仅在返回类型上有所不同的类型化索引器。
你需要在VB.net中做同样的事情,但是我的VB.net技能不能胜任工作 - 我无法看到如何在VB.net中进行显式接口实现。
或者,您需要使用IList的通用版本来获得特定类型的索引器。这是C#版本:
class MyObjectCollection : IList<MyObject>
{
private readonly MyObject[] innerArray;
public MyObject this[int index]
{
get { return (MyObject)innerArray[index]; }
set { innerArray[index] = value; }
}
}