Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix reflect constructor error when ValueTupe's elements more than 7 #307

Merged
merged 2 commits into from
May 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Linq;
using System.Reflection;
using AspectCore.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;

Expand All @@ -13,11 +15,11 @@ static void Main(string[] args)
services.AddTransient<ILogger, ConsoleLogger>();
services.AddTransient<ISampleService, SampleService>();
var serviceProvider = services.BuildServiceContextProvider();
// var container = services.ToServiceContext();
// container.AddType<ILogger, ConsoleLogger>();
// container.AddType<ISampleService, SampleService>();
// var serviceResolver = container.Build();
// var sampleService = serviceResolver.Resolve<ISampleService>();
// var container = services.ToServiceContext();
// container.AddType<ILogger, ConsoleLogger>();
// container.AddType<ISampleService, SampleService>();
// var serviceResolver = container.Build();
// var sampleService = serviceResolver.Resolve<ISampleService>();
var sampleService = serviceProvider.GetService<ISampleService>();
sampleService.Invoke();
Console.ReadKey();
Expand Down
17 changes: 17 additions & 0 deletions src/AspectCore.Extensions.Reflection/Extensions/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Concurrent;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;

namespace AspectCore.Extensions.Reflection
Expand Down Expand Up @@ -181,5 +182,21 @@ public static bool IsNullableType(this Type type)
return type.GetTypeInfo().IsGenericType &&
type.GetGenericTypeDefinition() == typeof(Nullable<>);
}

public static bool IsTupleType(this Type type)
{
if (type is null)
{
throw new ArgumentNullException(nameof(type));
}
#if NET461
return false;
#elif NETSTANDARD2_0
return false;
#else
return type.IsGenericType && typeof(ITuple).IsAssignableFrom(type.GetTypeInfo().GetGenericTypeDefinition());
#endif

}
}
}
7 changes: 6 additions & 1 deletion src/AspectCore.Extensions.Reflection/ParameterReflector.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;

namespace AspectCore.Extensions.Reflection
{
Expand All @@ -24,7 +25,11 @@ public partial class ParameterReflector : ICustomAttributeReflectorProvider
private ParameterReflector(ParameterInfo reflectionInfo)
{
_reflectionInfo = reflectionInfo ?? throw new ArgumentNullException(nameof(reflectionInfo));
_customAttributeReflectors = _reflectionInfo.CustomAttributes.Select(data => CustomAttributeReflector.Create(data)).ToArray();

if (!reflectionInfo.ParameterType.IsTupleType())
{
_customAttributeReflectors = _reflectionInfo.CustomAttributes.Select(data => CustomAttributeReflector.Create(data)).ToArray();
}
HasDeflautValue = reflectionInfo.HasDefaultValueByAttributes();
if (HasDeflautValue)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace AspectCore.Extensions.DependencyInjection.Test.Issues
{
// https://github.com/dotnetcore/AspectCore-Framework/issues/305
public class ValueTupleMorethan7ElementsTests
{
[Fact]
public void ValueTupleMorethan7Elements_Constructor_Test()
{
var services = new ServiceCollection();
services.AddScoped<ITestService, TestService>();
var serviceProvider = services.BuildServiceContextProvider();
var testService = serviceProvider.GetService<ITestService>();
Assert.NotNull(testService);

var (a, b, c, d, e, f, g, h) = testService.Wrap(("a", "b", "c", "d", "e", "f", "g", "h"));
Assert.Equal("a", a);
Assert.Equal("b", b);
Assert.Equal("c", c);
Assert.Equal("d", d);
Assert.Equal("e", e);
Assert.Equal("f", f);
Assert.Equal("g", g);
Assert.Equal("h", h);
}
public interface ITestService
{
//void Update((string, string, string, string, string, string, string) tupleKey);
void Update((string a, string b, string c, string d, string e, string f, string g, string h) tupleKey);
(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);
//void Update2((string a, string b, string c, string d, string e, string f, string g) tupleKey);
//void Update3((string a, string b, string c, string d, string e, string f, string g, string h, (string i, string j)) tupleKey);
}

public class TestService : ITestService
{
//public void Update((string, string, string, string, string, string, string) tupleKey)
//{

//}
public void Update((string a, string b, string c, string d, string e, string f, string g, string h) tupleKey)
{
}

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)
{
return tupleKey;
}

//public void Update2((string a, string b, string c, string d, string e, string f, string g) tupleKey)
//{

//}

//public void Update3((string a, string b, string c, string d, string e, string f, string g, string h, (string i, string j)) tupleKey)
//{

//}

//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)
//{

//}

//public void Update4((string, string, string, string, string, string, string) tupleKey)
//{

//}

//public void Update5((string, string, string, string, string, string, string, string, string) tupleKey)
//{

//}
}
}
}