2013-08-19 67 views
1

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

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中不起作用。

回答

3

可以使用LINQ表达式:

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

+1酷,不知道Compact Framework的那样的LINQ,有实际看它:) –

+0

我不不要认为linq在这种情况下是谨慎的,因为每个对象都有2个分配。 – Romoku

+0

@Romoku:你为什么这么说?那这个问题怎么样? –

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; 
} 
+0

我希望它看起来不像*完全像这样...你知道,告诉某人从版权作品中复制逐字代码... – xanatos

+1

@xanatos确保它的确如此工作。你从未见过从Resharper发布的.Net代码吗?还有:您是否知道Microsoft .Net源代码是公开的? http://referencesource.microsoft.com/ –

+0

是的,根据[Microsoft参考源许可证(MS-RSL)](http://referencesource.microsoft.com/referencesourcelicense.aspx)许可证,例如,给你*( A)版权授予 - 根据本许可协议的条款,许可人授予您一份不可转让,非独占性,全球性,免版税的版权许可,以* *复制该软件以供参考使用** – xanatos

0

当我有这些问题,我想感觉“干净”,我通常会看到Project Mono的好人做了什么。

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

// 
// System.Array.cs 
// 
// Authors: 
// Joe Shaw ([email protected]) 
// Martin Baulig ([email protected]) 
// Dietmar Maurer ([email protected]) 
// Gonzalo Paniagua Javier ([email protected]) 
// Jeffrey Stedfast ([email protected]) 
// Marek Safar ([email protected]) 
// 
// (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; 
} 

因为而寻找与反射器/ ilspy的.NET的合法性是灰色区域,逐字拷贝的代码是可靠地在错误的一侧。

(请注意,我已经添加了文件的版权,这个版本比代码要大得多:-),因为从技术上讲,如果您想使用那么一小段代码,您必须尊重它的许可证,是MIT许可证)

1

对于.NET的核心,这将是有用的:

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; 
} 
相关问题