Skip to content

Commit 00199a0

Browse files
committed
Implement conversions in GetFieldValue<T>. Fixes #716
1 parent f4c1cc0 commit 00199a0

File tree

3 files changed

+115
-6
lines changed

3 files changed

+115
-6
lines changed

src/MySqlConnector/MySql.Data.MySqlClient/MySqlDataReader.cs

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -285,14 +285,48 @@ public ReadOnlyCollection<DbColumn> GetColumnSchema()
285285

286286
public override T GetFieldValue<T>(int ordinal)
287287
{
288+
if (typeof(T) == typeof(bool))
289+
return (T) (object) GetBoolean(ordinal);
290+
if (typeof(T) == typeof(byte))
291+
return (T) (object) GetByte(ordinal);
292+
if (typeof(T) == typeof(sbyte))
293+
return (T) (object) GetSByte(ordinal);
294+
if (typeof(T) == typeof(short))
295+
return (T) (object) GetInt16(ordinal);
296+
if (typeof(T) == typeof(ushort))
297+
return (T) (object) GetUInt16(ordinal);
298+
if (typeof(T) == typeof(int))
299+
return (T) (object) GetInt32(ordinal);
300+
if (typeof(T) == typeof(uint))
301+
return (T) (object) GetUInt32(ordinal);
302+
if (typeof(T) == typeof(long))
303+
return (T) (object) GetInt64(ordinal);
304+
if (typeof(T) == typeof(ulong))
305+
return (T) (object) GetUInt64(ordinal);
306+
if (typeof(T) == typeof(char))
307+
return (T) (object) GetChar(ordinal);
308+
if (typeof(T) == typeof(decimal))
309+
return (T) (object) GetDecimal(ordinal);
310+
if (typeof(T) == typeof(double))
311+
return (T) (object) GetDouble(ordinal);
312+
if (typeof(T) == typeof(float))
313+
return (T) (object) GetFloat(ordinal);
314+
if (typeof(T) == typeof(string))
315+
return (T) (object) GetString(ordinal);
316+
if (typeof(T) == typeof(DateTime))
317+
return (T) (object) GetDateTime(ordinal);
288318
if (typeof(T) == typeof(DateTimeOffset))
289-
return (T) Convert.ChangeType(GetDateTimeOffset(ordinal), typeof(T));
290-
if (typeof(T) == typeof(TextReader) || typeof(T) == typeof(StringReader))
291-
return (T) (object) GetTextReader(ordinal);
292-
if (typeof(T) == typeof(Stream))
293-
return (T) (object) GetStream(ordinal);
319+
return (T) (object) GetDateTimeOffset(ordinal);
320+
if (typeof(T) == typeof(Guid))
321+
return (T) (object) GetGuid(ordinal);
294322
if (typeof(T) == typeof(MySqlGeometry))
295323
return (T) (object) GetMySqlGeometry(ordinal);
324+
if (typeof(T) == typeof(Stream))
325+
return (T) (object) GetStream(ordinal);
326+
if (typeof(T) == typeof(TextReader) || typeof(T) == typeof(StringReader))
327+
return (T) (object) GetTextReader(ordinal);
328+
if (typeof(T) == typeof(TimeSpan))
329+
return (T) (object) GetTimeSpan(ordinal);
296330

297331
return base.GetFieldValue<T>(ordinal);
298332
}

tests/Conformance.Tests/Conformance.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
</PropertyGroup>
1212

1313
<ItemGroup>
14-
<PackageReference Include="AdoNet.Specification.Tests" Version="2.0.0-alpha5" />
14+
<PackageReference Include="AdoNet.Specification.Tests" Version="2.0.0-alpha6" />
1515
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
1616
<PackageReference Include="xunit" Version="2.4.1" />
1717
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />

0 commit comments

Comments
 (0)