LUKIYA'S NEVERLAND

春去秋来,花谢花开。


做DesignLibrary.需要进行大量的数组类型转换,因此得写大量自定义方法,实在累人。

Baidu一下,发现新大陆。又是泛型,真是神奇,原来2.0给我们准备了这么多泛型方法。

//
// Summary:
// Converts an array of one type to an array of another type.
//
// Parameters:
// array:
// The one-dimensional, zero-based System.Array to convert to a target type.
//
// converter:
// A System.Converter<TInput,TOutput> that converts each element from one type
// to another type.
//
// Returns:
// An array of the target type containing the converted elements from the source
// array.
//
// Exceptions:
// System.ArgumentNullException:
// array is null.-or-converter is null.
public static TOutput[] ConvertAll < TInput, TOutput > (TInput[] array, Converter < TInput, TOutput > converter);

使用例子
public string [] GetRelationsForSources( object id)
{
vw_rel_InspirationsInSources[] entities
=
Gateways.Default.Select
< vw_rel_InspirationsInSources > (_Entity.vw_rel_Base.ItemId == id);

return Array.ConvertAll < vw_rel_InspirationsInSources, string > (entities,
delegate (vw_rel_InspirationsInSources input) { return input.Title; });
}