Skip to content

Commit

Permalink
You can now call overloaded generic extension methods using TypeExten…
Browse files Browse the repository at this point in the history
…sions.Call
  • Loading branch information
vahid committed May 18, 2024
1 parent 6011b98 commit 23e8c76
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 8 deletions.
34 changes: 27 additions & 7 deletions 01-Core/Jinget.Core/ExtensionMethods/Reflection/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,44 @@ public static bool IsAnonymousType(this Type type)
/// <param name="caller">object used to call the method. if the method is static, then set this as `null`</param>
/// <param name="name">method name</param>
/// <param name="bindingFlags"></param>
/// <param name="parameters">parameters used to pass to the method</param>
/// <param name="parameterValues">parameters used to pass to the method</param>
/// <param name="generics">if the mthod is a generic method, then generic types should be specified</param>
/// <returns>Invoke the method and return the method's return value</returns>
public static object? Call(this Type type, object? caller, string name, BindingFlags bindingFlags, Type[]? parameterTypes, object?[] parameters, params Type[] generics)
public static object? Call(this Type type, object? caller, string name, BindingFlags bindingFlags, Type[]? parameterTypes, object?[] parameterValues, params Type[] generics)
{
MethodInfo method;
if (parameterTypes == null)
method = type.GetMethod(name, bindingFlags);
if (generics != null)
{
if (parameterTypes == null)
method = type.GetMethods(bindingFlags)

Check warning on line 54 in 01-Core/Jinget.Core/ExtensionMethods/Reflection/TypeExtensions.cs

View workflow job for this annotation

GitHub Actions / release

Converting null literal or possible null value to non-nullable type.
.Where(
x => x.Name == name &&
x.GetGenericArguments().Length == generics.Length)
.FirstOrDefault();
else
method = type.GetMethods(bindingFlags)

Check warning on line 60 in 01-Core/Jinget.Core/ExtensionMethods/Reflection/TypeExtensions.cs

View workflow job for this annotation

GitHub Actions / release

Converting null literal or possible null value to non-nullable type.
.Where(
x => x.Name == name &&
x.GetGenericArguments().Length == generics.Length &&
x.GetParameters().Any() &&
x.GetParameters().All(p => parameterTypes.ToList().Contains(p.ParameterType))
)
.FirstOrDefault();
}
else
method = type.GetMethod(name, bindingFlags, parameterTypes);

{
if (parameterTypes == null)
method = type.GetMethod(name, bindingFlags);

Check warning on line 72 in 01-Core/Jinget.Core/ExtensionMethods/Reflection/TypeExtensions.cs

View workflow job for this annotation

GitHub Actions / release

Converting null literal or possible null value to non-nullable type.
else
method = type.GetMethod(name, bindingFlags, parameterTypes);

Check warning on line 74 in 01-Core/Jinget.Core/ExtensionMethods/Reflection/TypeExtensions.cs

View workflow job for this annotation

GitHub Actions / release

Converting null literal or possible null value to non-nullable type.
}
if (method == null)
return null;
if (generics != null)
method = method.MakeGenericMethod(generics);
try
{
return method.Invoke(caller, parameters);
return method.Invoke(caller, parameterValues);
}
catch (Exception ex) when (ex.InnerException is JingetException)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@ public void should_call_overloaded_static_method_dynamically()
Assert.AreEqual(expectedResult, result);
}

[TestMethod()]
public void should_call_generic_overloaded_extension_method_dynamically()
{
string[] expectedResult = ["TestClass", "SoapSample"];

var result = typeof(TestClassExtensions).Call(
name: nameof(TestClassExtensions.Method1),
parameterTypes: [typeof(TestClass)],
parameterValues: [new TestClass()],
typeof(TestClass), typeof(SoapSample));

Assert.AreEqual(expectedResult.First(), ((string[])result).First());
Assert.AreEqual(expectedResult.Last(), ((string[])result).Last());
}

[TestMethod()]
public void shuold_return_all_reference_type_properties_except_string_types()
{
Expand Down
14 changes: 13 additions & 1 deletion Tests/Jinget.Core.Tests/_BaseData/Sample.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System;
using System.Collections.Generic;

namespace Jinget.Core.Tests._BaseData;
Expand All @@ -19,6 +20,17 @@ public class Type2
public string Name { get; set; }
public string SurName { get; set; }
}
public static class TestClassExtensions
{
public static string[] Method1<T>(this TestClass testClass)
{
return [typeof(T).Name];
}
public static string[] Method1<T, U>(this TestClass testClass)
{
return [typeof(T).Name, typeof(U).Name];
}
}
public class TestClass
{
public static int Method1(int a) => a;
Expand Down

0 comments on commit 23e8c76

Please sign in to comment.