1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-13 00:45:42 +00:00

(#14) Use explicit type (#42) Ensure readable stream

This commit is contained in:
Oliver Booth 2021-08-31 15:47:12 +01:00
parent 5e021b703b
commit 1ee5b8ef64
No known key found for this signature in database
GPG Key ID: A4AC17007530E9B4

View File

@ -1,5 +1,6 @@
using System; using System;
using System.IO; using System.IO;
using System.Reflection;
using System.Security.Cryptography; using System.Security.Cryptography;
#if NET5_0 #if NET5_0
using System.Buffers.Binary; using System.Buffers.Binary;
@ -38,15 +39,20 @@ namespace X10D
throw new ArgumentNullException(nameof(stream)); throw new ArgumentNullException(nameof(stream));
} }
var type = typeof(T); if (!stream.CanRead)
var create = type.GetMethod("Create", Array.Empty<Type>()); {
if (create is null) throw new IOException(ExceptionMessages.StreamDoesNotSupportReading);
}
Type type = typeof(T);
MethodInfo? createMethod = type.GetMethod("Create", BindingFlags.Public | BindingFlags.Static);
if (createMethod is null)
{ {
throw new TypeInitializationException(type.FullName, throw new TypeInitializationException(type.FullName,
new ArgumentException(ExceptionMessages.HashAlgorithmNoCreateMethod)); new ArgumentException(ExceptionMessages.HashAlgorithmNoCreateMethod));
} }
using var crypt = create.Invoke(null, null) as T; using var crypt = createMethod.Invoke(null, null) as T;
if (crypt is null) if (crypt is null)
{ {
throw new TypeInitializationException(type.FullName, throw new TypeInitializationException(type.FullName,