C#Array.ConvertAll用于紧凑框架

时间:2013-08-19 11:20:03

标签: c# arrays compact-framework

据我所知,.NET Compact Framework没有Array.ConvertAll()。在CF中进行Array.ConvertAll()的最佳方法是什么?

public static string DataRowToString(DataRow dr)
{
    return dr["columns"].ToString();
}

public static string[] DataTableToArray(DataTable dt)
{
    var dr = dt.Select();
    string[] strArr = Array.ConvertAll(dr, new System.Converter<DataRow, string>(DataRowToString));
    return strArr;
}

上面的代码在CF中不起作用。

4 个答案:

答案 0 :(得分:3)

您可以使用LINQ表达式:

var result = input.Select(converter).ToArray();

答案 1 :(得分:1)

此外,您可以使用reflector来实现实现,结果如下:

public static TOutput[] ConvertAll<TInput, TOutput>(TInput[] array, Converter<TInput, TOutput> converter)
{
    if (array == null)
    {
        throw new ArgumentNullException("array");
    }

    if (converter == null)
    {
        throw new ArgumentNullException("converter");
    }

    TOutput[] localArray = new TOutput[array.Length];

    for (int i = 0; i < array.Length; i++)
    {
        localArray[i] = converter(array[i]);
    }

    return localArray;
}

答案 2 :(得分:0)

当我遇到这些问题,并且我想要感觉“干净”时,我通常会看到Project Mono的好人所做的事。

(我有我的本地副本,所以我可以直接查看Array.cs)...现在让我们see

//
// System.Array.cs
//
// Authors:
//   Joe Shaw (joe@ximian.com)
//   Martin Baulig (martin@gnome.org)
//   Dietmar Maurer (dietmar@ximian.com)
//   Gonzalo Paniagua Javier (gonzalo@ximian.com)
//   Jeffrey Stedfast (fejj@novell.com)
//   Marek Safar (marek.safar@gmail.com)
//
// (C) 2001-2003 Ximian, Inc.  http://www.ximian.com
// Copyright (C) 2004-2011 Novell, Inc (http://www.novell.com)
// Copyright (C) 2011 Xamarin Inc (http://www.xamarin.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

public static TOutput[] ConvertAll<TInput, TOutput> (TInput [] array, Converter<TInput, TOutput> converter)
{
    if (array == null)
        throw new ArgumentNullException ("array");
    if (converter == null)
        throw new ArgumentNullException ("converter");

    TOutput [] output = new TOutput [array.Length];
    for (int i = 0; i < array.Length; i ++)
        output [i] = converter (array [i]);

    return output;
}

因为使用reflector / ilspy查看.NET的合法性是一个灰色区域,所以逐字复制代码肯定是错误的。

(请注意,我已添加了该文件的版权,仅此一项比代码大得多:-),因为从技术上讲,如果您想使用这一小段代码,则必须尊重其许可,即麻省理工学院许可证)

答案 3 :(得分:0)

对于.NET Core,这将非常有用:

public static TOut[] ConvertAll<TIn, TOut>(this TIn[] thisArray, Func<TIn, TOut> converter)
{
    if (thisArray == null)
        throw new ArgumentNullException(nameof(thisArray));

    if (converter == null)
        throw new ArgumentNullException(nameof(converter));

    TOut[] revVal = new TOut[thisArray.Length];

    for (int i = 0; i < thisArray.Length; i++)
        revVal[i] = converter(thisArray[i]);

    return revVal;
}