sometimes you may want to design some interfaces so that it can handle both generic classes and the non-generic classes. The reason for oding this is 1. for backward compatability 2. you may deal with data with determined parameter type T or data whose type is object.
A typical example of this is the IEnumerable interface, where
public interface IEnumerable
{
IEnumerator GetEnumerator();
}
public interface IEnumerable<out T> : IEnumerable
{
IEnumerator<T> GetEnumerator();
}
Below I provided the skeleton to implements such classes that inherits from the generic/non-generic interfaces.
interface IInterface
{
object Object { get; }
}
interface IInterface<T> : IInterface
{
T Object { get; }
}
class NonGeneric : IInterface
{
protected object obj;
public object Object {
get { return obj; }
}
}
class Generic<T> : NonGeneric, IInterface<T>, IInterface
{
object IInterface.Object { get { return base.obj; } }
public T Object { get { return (T)base.obj; } }
}
本文探讨了如何设计能够同时处理通用类和非通用类的接口,通过具体示例展示了如何实现 `IEnumerable` 接口的泛型和非泛型版本,并提供了实现这些接口类的骨架。
1411

被折叠的 条评论
为什么被折叠?



