SafelyCastedListTFrom, TTo Class

表示安全转换后的强类型列表。

Definition

Namespace: QuickAdmin.Utils
Assembly: QuickAdmin.Net (in QuickAdmin.Net.dll) Version: 1.1.6
C#
public class SafelyCastedList<TFrom, TTo> : IList<TTo>, 
	ICollection<TTo>, IEnumerable<TTo>, IEnumerable
where TFrom : TTo
Inheritance
object    SafelyCastedListTFrom, TTo
Implements
ICollectionTTo, IEnumerableTTo, IListTTo, IEnumerable

Type Parameters

TFrom
转换前的元素类型。
TTo
转换后的元素类型。TFrom 必须继承自 TTo

Remarks

可通过 CastToListSafelyTFrom, TTo(IListTFrom) 扩展方法使用此类。

Example

此类用来满足以下类似需求:
C#
public interface IFoo { }
public class ClassA : IFoo { }
public class ClassB : IFoo { }

// 某公共方法输入参数为父接口(或父类)列表:
public void FooCommonMethod(IList<IFoo> list) { }

// 当需要为子类列表调用该公共方法时:
public void Test()
{
    var listA = new List<ClassA>();
    var listB = new List<ClassB>();

    // 这样是不行的:
    // FooCommonMethod(listA);
    // FooCommonMethod(listB);

    // 使用 SafelyCastedList:
    IList<IFoo> castedListA = new SafelyCastedList<ClassA, IFoo>(listA);
    IList<IFoo> castedListB = new SafelyCastedList<ClassB, IFoo>(listB);
    FooCommonMethod(listA);
    FooCommonMethod(listB);

    // 也可使用本类库提供的扩展方法 IList<T>.CastToListSafely() 获取转换后的列表,然后调用该公共方法:
    castedListA = listA.CastToListSafely<ClassA, IFoo>();
    castedListB = listB.CastToListSafely<ClassB, IFoo>();
    FooCommonMethod(listA);
    FooCommonMethod(listB);
}

Constructors

SafelyCastedListTFrom, TTo 初始化转换后的强类型列表。

Properties

Public Properties

CountGets the number of elements contained in the ICollection.
FromList 源列表。
IsReadOnlyGets a value indicating whether the ICollectionT is read-only.
ItemGets or sets the element at the specified index.

Methods

Public Methods

AddAdds an item to the ICollectionT.
ClearRemoves all items from the ICollectionT.
ContainsDetermines whether the ICollectionT contains a specific value.
CopyToCopies the elements of the ICollectionT to an Array, starting at a particular Array index.
GetEnumeratorReturns an enumerator that iterates through a collection.
IndexOfDetermines the index of a specific item in the IListT.
InsertInserts an item to the IListT at the specified index.
RemoveRemoves the first occurrence of a specific object from the ICollectionT.
RemoveAtRemoves the IListT item at the specified index.

Explicit Interface Implementations

IEnumerableGetEnumeratorReturns an enumerator that iterates through a collection.

See Also