Skip to content

Commit e08a898

Browse files
committed
Refactor generic argument code + better Blazor support
1 parent 96b4bc2 commit e08a898

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace Genbox.SimpleS3.Core.Internals.Extensions;
2+
3+
internal static class TypeExtensions
4+
{
5+
internal static Type[] GetTypeArguments(this Type type)
6+
{
7+
Type[]? args = null;
8+
9+
foreach (Type intType in type.GetInterfaces())
10+
{
11+
args = intType.GetGenericArguments();
12+
13+
if (args.Length != 0)
14+
break;
15+
}
16+
17+
if (args == null)
18+
throw new InvalidOperationException("Unable to find generic arguments");
19+
20+
return args;
21+
}
22+
}

Src/SimpleS3.Core/Internals/Network/MarshalFactory.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,14 @@ public MarshalFactory(IEnumerable<IRequestMarshal> requestMarshals, IEnumerable<
2222
_requestMarshals = requestMarshals.ToDictionary(x =>
2323
{
2424
Type type = x.GetType();
25-
Type iType = type.GetInterfaces()[0];
26-
Type[] args = iType.GetGenericArguments();
27-
25+
Type[] args = type.GetTypeArguments();
2826
return args[0];
2927
}, x => x);
3028

3129
_responseMarshals = responseMarshals.ToDictionary(x =>
3230
{
3331
Type type = x.GetType();
34-
Type iType = type.GetInterfaces()[0];
35-
Type[] args = iType.GetGenericArguments();
36-
32+
Type[] args = type.GetTypeArguments();
3733
return args[0];
3834
}, x => x);
3935
}

Src/SimpleS3.Core/Internals/Network/PostMapperFactory.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using Genbox.SimpleS3.Core.Abstracts.Factories;
33
using Genbox.SimpleS3.Core.Abstracts.Request;
44
using Genbox.SimpleS3.Core.Abstracts.Response;
5+
using Genbox.SimpleS3.Core.Internals.Extensions;
6+
using Genbox.SimpleS3.Core.Internals.Helpers;
57

68
namespace Genbox.SimpleS3.Core.Internals.Network;
79

@@ -14,9 +16,7 @@ public PostMapperFactory(IEnumerable<IPostMapper> postMappers)
1416
_postMappers = postMappers.ToDictionary(x =>
1517
{
1618
Type type = x.GetType();
17-
Type iType = type.GetInterfaces()[0];
18-
Type[] args = iType.GetGenericArguments();
19-
19+
Type[] args = type.GetTypeArguments();
2020
return $"{args[0].Name}-{args[1].Name}";
2121
}, x => x, StringComparer.Ordinal);
2222
}

0 commit comments

Comments
 (0)