假设要编写一个 DoWork 方法来根据传入的集合来处理"逻辑",同时希望能够通过IList, IDictionary, IEnumerator 或者ICollection。为此,可以定义四个不同的 DoWork 方法,如下例所示:
class
BusinessObject
{
private
BusinessObject()
{
}
public
static
string
DoWord(IList list)
{
return
"
list
"
;
}
public
static
string
DoWord(IDictionary dictionary)
{
return
"
dictionary
"
;
}
public
static
string
DoWord(IEnumerator enumerator)
{
return
"
enumerator
"
;
}
public
static
string
DoWord(ICollection collection)
{
return
"
collection
"
;
}
}
class
Program
{
static
void
Main(
string
[] args)
{
ArrayList list
=
new
ArrayList();
Console.WriteLine(BusinessObject.DoWord(list));
Hashtable table
=
new
Hashtable();
Console.WriteLine(BusinessObject.DoWord(table));
Queue queue
=
new
Queue();
Console.WriteLine(BusinessObject.DoWord(queue));
Stack stack
=
new
Stack();
Console.WriteLine(BusinessObject.DoWord(stack));
}
}
得出的结果是:
list
dictionary
collection
collection
为什么会得出这样的结果呢? 我们再详细的看ArrayList, Hashtable, Queue, Stack实现的接口:
[SerializableAttribute]
[ComVisibleAttribute(
true
)]
public
class
ArrayList : IList, ICollection, IEnumerable,
ICloneable
[SerializableAttribute]
[ComVisibleAttribute(
true
)]
public
class
Hashtable : IDictionary, ICollection, IEnumerable,
ISerializable, IDeserializationCallback, ICloneable
[SerializableAttribute]
[ComVisibleAttribute(
true
)]
public
class
Queue : ICollection, IEnumerable, ICloneable
[SerializableAttribute]
[ComVisibleAttribute(
true
)]
public
class
Stack : ICollection, IEnumerable, ICloneable
(以上资料来自MSDN)
我们可以看到每个类都有可能执行两种不同的方法,那我们就要思考为什么得出这样的结果?
或者我们用另一个角度去想, 我们去实现那些接口或许能知道, 先在同一个项目里实现:
Class
[SerializableAttribute]
[ComVisibleAttribute(true)]
public class TestCollection : IList, ICollection, IEnumerator, ICloneable
{
#region ICloneable 成员
object ICloneable.Clone()
{
throw new Exception("The method or operation is not implemented.");
}
#endregion
#region IEnumerator 成员
object IEnumerator.Current
{
get { throw new Exception("The method or operation is not implemented."); }
}
bool IEnumerator.MoveNext()
{
throw new Exception("The method or operation is not implemented.");
}
void IEnumerator.Reset()
{
throw new Exception("The method or operation is not implemented.");
}
#endregion
#region ICollection 成员
void ICollection.CopyTo(Array array, int index)
{
throw new Exception("The method or operation is not implemented.");
}
int ICollection.Count
{
get { throw new Exception("The method or operation is not implemented."); }
}
bool ICollection.IsSynchronized
{
get { throw new Exception("The method or operation is not implemented."); }
}
object ICollection.SyncRoot
{
get { throw new Exception("The method or operation is not implemented."); }
}
#endregion
#region IEnumerable 成员
IEnumerator IEnumerable.GetEnumerator()
{
throw new Exception("The method or operation is not implemented.");
}
#endregion
#region IList 成员
int IList.Add(object value)
{
throw new Exception("The method or operation is not implemented.");
}
void IList.Clear()
{
throw new Exception("The method or operation is not implemented.");
}
bool IList.Contains(object value)
{
throw new Exception("The method or operation is not implemented.");
}
int IList.IndexOf(object value)
{
throw new Exception("The method or operation is not implemented.");
}
void IList.Insert(int index, object value)
{
throw new Exception("The method or operation is not implemented.");
}
bool IList.IsFixedSize
{
get { throw new Exception("The method or operation is not implemented."); }
}
bool IList.IsReadOnly
{
get { throw new Exception("The method or operation is not implemented."); }
}
void IList.Remove(object value)
{
throw new Exception("The method or operation is not implemented.");
}
void IList.RemoveAt(int index)
{
throw new Exception("The method or operation is not implemented.");
}
object IList.this[int index]
{
get
{
throw new Exception("The method or operation is not implemented.");
}
set
{
throw new Exception("The method or operation is not implemented.");
}
}
#endregion
}
class Program
{
static void Main(string[] args)
{
TestCollection test1 = new TestCollection();
Console.WriteLine(BusinessObject.DoWord(test1));
}
}
结果的编译不能通过.
错误是 : 错误 1 在以下方法或属性之间的调用不明确:“TryOverride.BusinessObject.DoWord(System.Collections.IList)”和“TryOverride.BusinessObject.DoWord(System.Collections.IEnumerator)” ..\TryOverride\TryOverride\Program.cs 28 22 TryOverride
TestCollection类和ArrayList实现的接口都一样,为什么编译错误呢?
我们在不同的项目中再写一个TestTable类实现的接口和HashTable一样(详细请下载代码),编译就可以通过.
这是dotNet问题还是VS2005的问题呢? 呵呵.
http://files.cnblogs.com/wanggh/TryOverride.zip