Commit 2d58a014 authored by Jeremy Meng's avatar Jeremy Meng

Add ConvertHelper.ConvertAll, which uses Array.ConvertAll() for Non-dnx, and...

Add ConvertHelper.ConvertAll, which uses Array.ConvertAll() for Non-dnx, and add implementation for dnx.
parent 9db8bbdf
......@@ -81,6 +81,7 @@
<ItemGroup>
<Compile Include="StackExchange\Redis\Aggregate.cs" />
<Compile Include="StackExchange\Redis\ClientType.cs" />
<Compile Include="StackExchange\Redis\Compat\ConvertHelper.cs" />
<Compile Include="StackExchange\Redis\Compat\VolatileWrapper.cs" />
<Compile Include="StackExchange\Redis\ConcurrentProfileStorageCollection.cs" />
<Compile Include="StackExchange\Redis\ConnectionMultiplexer.Profiling.cs">
......
......@@ -75,6 +75,7 @@
<ItemGroup>
<Compile Include="StackExchange\Redis\Aggregate.cs" />
<Compile Include="StackExchange\Redis\ClientType.cs" />
<Compile Include="StackExchange\Redis\Compat\ConvertHelper.cs" />
<Compile Include="StackExchange\Redis\Compat\VolatileWrapper.cs" />
<Compile Include="StackExchange\Redis\ConcurrentProfileStorageCollection.cs" />
<Compile Include="StackExchange\Redis\ConnectionMultiplexer.Profiling.cs">
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StackExchange.Redis
{
/// <summary>
/// Helper for Array.ConvertAll() as it's missing on .Net Core.
/// </summary>
public static class ConvertHelper
{
/// <summary>
/// Converts array of one type to an array of another type.
/// </summary>
/// <typeparam name="TInput">Input type</typeparam>
/// <typeparam name="TOutput">Output type</typeparam>
/// <param name="source">source</param>
/// <param name="selector">selector</param>
/// <returns></returns>
public static TOutput[] ConvertAll<TInput, TOutput>(TInput[] source, Func<TInput, TOutput> selector)
{
#if DNXCORE50
TOutput[] arr = new TOutput[source.Length];
for(int i = 0 ; i < arr.Length ; i++)
arr[i] = selector(source[i]);
return arr;
#else
return Array.ConvertAll(source, item => selector(item));
#endif
}
}
}
......@@ -482,7 +482,7 @@ public EndPoint[] GetEndPoints(bool configuredOnly = false)
{
if (configuredOnly) return configuration.EndPoints.ToArray();
return serverSnapshot.Select(x => x.EndPoint).ToArray();
return ConvertHelper.ConvertAll(serverSnapshot, x => x.EndPoint);
}
private readonly int timeoutMilliseconds;
......
using System;
using System.Collections.Generic;
using System.Linq;
namespace StackExchange.Redis
{
......@@ -121,7 +120,7 @@ public static string[] ToStringArray(this RedisValue[] values)
{
if (values == null) return null;
if (values.Length == 0) return nix;
return values.Select(x => (string)x).ToArray();
return ConvertHelper.ConvertAll(values, x => (string)x);
}
}
}
......@@ -195,14 +195,14 @@ internal override bool AsBoolean()
throw new InvalidCastException();
}
internal override bool[] AsBooleanArray() { return value.Select(x => x.AsBoolean()).ToArray(); }
internal override bool[] AsBooleanArray() { return ConvertHelper.ConvertAll(value, x => x.AsBoolean()); }
internal override byte[] AsByteArray()
{
if (value.Length == 1) return value[0].AsByteArray();
throw new InvalidCastException();
}
internal override byte[][] AsByteArrayArray() { return value.Select(x => x.AsByteArray()).ToArray(); }
internal override byte[][] AsByteArrayArray() { return ConvertHelper.ConvertAll(value, x => x.AsByteArray()); }
internal override double AsDouble()
{
......@@ -210,7 +210,7 @@ internal override double AsDouble()
throw new InvalidCastException();
}
internal override double[] AsDoubleArray() { return value.Select(x => x.AsDouble()).ToArray(); }
internal override double[] AsDoubleArray() { return ConvertHelper.ConvertAll(value, x => x.AsDouble()); }
internal override int AsInt32()
{
......@@ -218,7 +218,7 @@ internal override int AsInt32()
throw new InvalidCastException();
}
internal override int[] AsInt32Array() { return value.Select(x => x.AsInt32()).ToArray(); }
internal override int[] AsInt32Array() { return ConvertHelper.ConvertAll(value, x => x.AsInt32()); }
internal override long AsInt64()
{
......@@ -226,7 +226,7 @@ internal override long AsInt64()
throw new InvalidCastException();
}
internal override long[] AsInt64Array() { return value.Select(x => x.AsInt64()).ToArray(); }
internal override long[] AsInt64Array() { return ConvertHelper.ConvertAll(value, x => x.AsInt64()); }
internal override bool? AsNullableBoolean()
{
......@@ -258,7 +258,7 @@ internal override RedisKey AsRedisKey()
throw new InvalidCastException();
}
internal override RedisKey[] AsRedisKeyArray() { return value.Select(x => x.AsRedisKey()).ToArray(); }
internal override RedisKey[] AsRedisKeyArray() { return ConvertHelper.ConvertAll(value, x => x.AsRedisKey()); }
internal override RedisResult[] AsRedisResultArray() { return value; }
......@@ -268,14 +268,14 @@ internal override RedisValue AsRedisValue()
throw new InvalidCastException();
}
internal override RedisValue[] AsRedisValueArray() { return value.Select(x => x.AsRedisValue()).ToArray(); }
internal override RedisValue[] AsRedisValueArray() { return ConvertHelper.ConvertAll(value, x => x.AsRedisValue()); }
internal override string AsString()
{
if (value.Length == 1) return value[0].AsString();
throw new InvalidCastException();
}
internal override string[] AsStringArray() { return value.Select(x => x.AsString()).ToArray(); }
internal override string[] AsStringArray() { return ConvertHelper.ConvertAll(value, x => x.AsString()); }
}
private sealed class ErrorRedisResult : RedisResult
......
......@@ -85,6 +85,9 @@
<Compile Include="..\StackExchange.Redis\StackExchange\Redis\ClientType.cs">
<Link>ClientType.cs</Link>
</Compile>
<Compile Include="..\StackExchange.Redis\StackExchange\Redis\Compat\ConvertHelper.cs">
<Link>ConvertHelper.cs</Link>
</Compile>
<Compile Include="..\StackExchange.Redis\StackExchange\Redis\Compat\VolatileWrapper.cs">
<Link>VolatileWrapper.cs</Link>
</Compile>
......
......@@ -82,6 +82,9 @@
<Compile Include="..\StackExchange.Redis\StackExchange\Redis\ClientType.cs">
<Link>ClientType.cs</Link>
</Compile>
<Compile Include="..\StackExchange.Redis\StackExchange\Redis\Compat\ConvertHelper.cs">
<Link>ConvertHelper.cs</Link>
</Compile>
<Compile Include="..\StackExchange.Redis\StackExchange\Redis\Compat\VolatileWrapper.cs">
<Link>VolatileWrapper.cs</Link>
</Compile>
......
......@@ -71,6 +71,9 @@
<Compile Include="..\StackExchange.Redis\StackExchange\Redis\ClientType.cs">
<Link>ClientType.cs</Link>
</Compile>
<Compile Include="..\StackExchange.Redis\StackExchange\Redis\Compat\ConvertHelper.cs">
<Link>ConvertHelper.cs</Link>
</Compile>
<Compile Include="..\StackExchange.Redis\StackExchange\Redis\Compat\VolatileWrapper.cs">
<Link>VolatileWrapper.cs</Link>
</Compile>
......
......@@ -65,6 +65,9 @@
<Compile Include="..\StackExchange.Redis\StackExchange\Redis\ClientType.cs">
<Link>ClientType.cs</Link>
</Compile>
<Compile Include="..\StackExchange.Redis\StackExchange\Redis\Compat\ConvertHelper.cs">
<Link>ConvertHelper.cs</Link>
</Compile>
<Compile Include="..\StackExchange.Redis\StackExchange\Redis\Compat\VolatileWrapper.cs">
<Link>VolatileWrapper.cs</Link>
</Compile>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment