Skip to content

Commit b479bf3

Browse files
authored
fix reflect constructor error when ValueTupe's elements more than 7 (#307)
* fix: create instance of tuple/valuetuple exception when parameters number more than 7 * fix: add valuetuple more than 7 elements unit test
1 parent 47793df commit b479bf3

File tree

4 files changed

+113
-6
lines changed

4 files changed

+113
-6
lines changed

sample/AspectCore.Extensions.DependencyInjection.ConsoleSample/Program.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Linq;
3+
using System.Reflection;
24
using AspectCore.DependencyInjection;
35
using Microsoft.Extensions.DependencyInjection;
46

@@ -13,11 +15,11 @@ static void Main(string[] args)
1315
services.AddTransient<ILogger, ConsoleLogger>();
1416
services.AddTransient<ISampleService, SampleService>();
1517
var serviceProvider = services.BuildServiceContextProvider();
16-
// var container = services.ToServiceContext();
17-
// container.AddType<ILogger, ConsoleLogger>();
18-
// container.AddType<ISampleService, SampleService>();
19-
// var serviceResolver = container.Build();
20-
// var sampleService = serviceResolver.Resolve<ISampleService>();
18+
// var container = services.ToServiceContext();
19+
// container.AddType<ILogger, ConsoleLogger>();
20+
// container.AddType<ISampleService, SampleService>();
21+
// var serviceResolver = container.Build();
22+
// var sampleService = serviceResolver.Resolve<ISampleService>();
2123
var sampleService = serviceProvider.GetService<ISampleService>();
2224
sampleService.Invoke();
2325
Console.ReadKey();

src/AspectCore.Extensions.Reflection/Extensions/TypeExtensions.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Concurrent;
33
using System.Linq;
44
using System.Reflection;
5+
using System.Runtime.CompilerServices;
56
using System.Threading.Tasks;
67

78
namespace AspectCore.Extensions.Reflection
@@ -181,5 +182,21 @@ public static bool IsNullableType(this Type type)
181182
return type.GetTypeInfo().IsGenericType &&
182183
type.GetGenericTypeDefinition() == typeof(Nullable<>);
183184
}
185+
186+
public static bool IsTupleType(this Type type)
187+
{
188+
if (type is null)
189+
{
190+
throw new ArgumentNullException(nameof(type));
191+
}
192+
#if NET461
193+
return false;
194+
#elif NETSTANDARD2_0
195+
return false;
196+
#else
197+
return type.IsGenericType && typeof(ITuple).IsAssignableFrom(type.GetTypeInfo().GetGenericTypeDefinition());
198+
#endif
199+
200+
}
184201
}
185202
}

src/AspectCore.Extensions.Reflection/ParameterReflector.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Linq;
33
using System.Reflection;
4+
using System.Runtime.CompilerServices;
45

56
namespace AspectCore.Extensions.Reflection
67
{
@@ -24,7 +25,11 @@ public partial class ParameterReflector : ICustomAttributeReflectorProvider
2425
private ParameterReflector(ParameterInfo reflectionInfo)
2526
{
2627
_reflectionInfo = reflectionInfo ?? throw new ArgumentNullException(nameof(reflectionInfo));
27-
_customAttributeReflectors = _reflectionInfo.CustomAttributes.Select(data => CustomAttributeReflector.Create(data)).ToArray();
28+
29+
if (!reflectionInfo.ParameterType.IsTupleType())
30+
{
31+
_customAttributeReflectors = _reflectionInfo.CustomAttributes.Select(data => CustomAttributeReflector.Create(data)).ToArray();
32+
}
2833
HasDeflautValue = reflectionInfo.HasDefaultValueByAttributes();
2934
if (HasDeflautValue)
3035
{
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using Xunit;
8+
9+
namespace AspectCore.Extensions.DependencyInjection.Test.Issues
10+
{
11+
// https://github.com/dotnetcore/AspectCore-Framework/issues/305
12+
public class ValueTupleMorethan7ElementsTests
13+
{
14+
[Fact]
15+
public void ValueTupleMorethan7Elements_Constructor_Test()
16+
{
17+
var services = new ServiceCollection();
18+
services.AddScoped<ITestService, TestService>();
19+
var serviceProvider = services.BuildServiceContextProvider();
20+
var testService = serviceProvider.GetService<ITestService>();
21+
Assert.NotNull(testService);
22+
23+
var (a, b, c, d, e, f, g, h) = testService.Wrap(("a", "b", "c", "d", "e", "f", "g", "h"));
24+
Assert.Equal("a", a);
25+
Assert.Equal("b", b);
26+
Assert.Equal("c", c);
27+
Assert.Equal("d", d);
28+
Assert.Equal("e", e);
29+
Assert.Equal("f", f);
30+
Assert.Equal("g", g);
31+
Assert.Equal("h", h);
32+
}
33+
public interface ITestService
34+
{
35+
//void Update((string, string, string, string, string, string, string) tupleKey);
36+
void Update((string a, string b, string c, string d, string e, string f, string g, string h) tupleKey);
37+
(string a, string b, string c, string d, string e, string f, string g, string h) Wrap((string a, string b, string c, string d, string e, string f, string g, string h) tupleKey);
38+
//void Update2((string a, string b, string c, string d, string e, string f, string g) tupleKey);
39+
//void Update3((string a, string b, string c, string d, string e, string f, string g, string h, (string i, string j)) tupleKey);
40+
}
41+
42+
public class TestService : ITestService
43+
{
44+
//public void Update((string, string, string, string, string, string, string) tupleKey)
45+
//{
46+
47+
//}
48+
public void Update((string a, string b, string c, string d, string e, string f, string g, string h) tupleKey)
49+
{
50+
}
51+
52+
public (string a, string b, string c, string d, string e, string f, string g, string h) Wrap((string a, string b, string c, string d, string e, string f, string g, string h) tupleKey)
53+
{
54+
return tupleKey;
55+
}
56+
57+
//public void Update2((string a, string b, string c, string d, string e, string f, string g) tupleKey)
58+
//{
59+
60+
//}
61+
62+
//public void Update3((string a, string b, string c, string d, string e, string f, string g, string h, (string i, string j)) tupleKey)
63+
//{
64+
65+
//}
66+
67+
//public void Update6((string a, string b, string c, string d, string e, string f, string g, (string h, string i, string j) ff) tupleKey)
68+
//{
69+
70+
//}
71+
72+
//public void Update4((string, string, string, string, string, string, string) tupleKey)
73+
//{
74+
75+
//}
76+
77+
//public void Update5((string, string, string, string, string, string, string, string, string) tupleKey)
78+
//{
79+
80+
//}
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)