我有一个带有 ArrayList 和一个索引属性的基类“A”(一个集合),该属性在 ArrayList 中返回该索引。我还有一个带有泛型类型 T 的派生类“B”,它充当 T 的集合。最后,有类“C”-“F”派生出类 B,每个类都有不同的 T。
代码如下所示:
public class A
{
protected ArrayList list;
public object this[int index] { get { return list[index]; } }
}
public class B<T> : A
{
}
public class C : B<G>
{
}
有使用这些类的第 3 方 .dll,所以我想知道是否可以在不破坏这些程序集的情况下进行更改。我想向 B 添加一个索引属性,它将隐藏 A 中的索引属性。
public class B<T> : A
{
// Adding this property
public new <T> this[int index] { get { return (T)list[index]; } }
}
现在其他 .dll 可能具有如下所示的代码:
C c = new C();
G g = (c[0] as G);
之前使用 'as' 进行强制转换的代码仍将使用新的 index 属性进行编译,但它也允许将代码简化为:
C c = new C();
G g = c[0];
我想知道将索引属性添加到 B 是否会使用我的程序集破坏第 3 方 .dll。
我也非常感谢解释为什么或为什么不是或不是重大变化。