Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 14da213

Browse files
committed
Implement GetMethodInfo with Type[] params for .NET Core
1 parent 257b163 commit 14da213

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

.vscode/launch.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
"type": "coreclr",
77
"request": "launch",
88
"preLaunchTask": "build",
9-
"program": "${workspaceRoot}/tests/ServiceStack.Text.Tests/bin/Debug/netcoreapp1.0/ServiceStack.Text.Tests.dll",
10-
//"args": ["--test=ServiceStack.Text.Tests.AutoMappingTests.Can_create_Dictionary_default_value"],
9+
"program": "${workspaceRoot}/tests/ServiceStack.Text.Tests/bin/Debug/netcoreapp1.1/ServiceStack.Text.Tests.dll",
10+
//"args": ["--test=ServiceStack.Text.Tests.ReflectionExtensionTests", "--labels=All"],
11+
"args": ["--labels=All"],
1112
"cwd": "${workspaceRoot}",
1213
"stopAtEntry": false,
1314
"externalConsole": false

src/ServiceStack.Text/PlatformExtensions.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,19 @@ public static MethodInfo GetStaticMethod(this Type type, string methodName, Type
795795
public static MethodInfo GetMethodInfo(this Type type, string methodName, Type[] types = null)
796796
{
797797
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
798-
return type.GetRuntimeMethods().FirstOrDefault(p => p.Name.Equals(methodName));
798+
if (types == null)
799+
return type.GetRuntimeMethods().FirstOrDefault(p => p.Name.Equals(methodName));
800+
801+
foreach(var mi in type.GetRuntimeMethods().Where(p => p.Name.Equals(methodName)))
802+
{
803+
var methodParams = mi.GetParameters().Select(p => p.ParameterType);
804+
if (methodParams.SequenceEqual(types))
805+
{
806+
return mi;
807+
}
808+
}
809+
810+
return null;
799811
#else
800812
return types == null
801813
? type.GetMethod(methodName)

tests/ServiceStack.Text.Tests/ReflectionExtensionTests.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Text;
55
using NUnit.Framework;
6+
using ServiceStack;
67

78
namespace ServiceStack.Text.Tests
89
{
@@ -37,6 +38,21 @@ public int IntMethod()
3738
}
3839
}
3940

41+
public class MethodsForReflection
42+
{
43+
public string Result = String.Empty;
44+
45+
public void HelloVoid()
46+
{
47+
Result = "Hello";
48+
}
49+
50+
public void Hello(bool a, int b)
51+
{
52+
Result = String.Format($"Hello {a} {b}");
53+
}
54+
}
55+
4056
[TestFixture]
4157
public class ReflectionExtensionTests
4258
: TestBase
@@ -84,6 +100,26 @@ public void Can_create_intances_of_recursive_generic_type()
84100
{
85101
//Assert.That(typeof(GenericType<>).MakeGenericType(new[] { typeof(GenericType<>) }).CreateInstance(), Is.Not.Null);
86102
}
103+
104+
[Test]
105+
public void Can_get_method_from_type()
106+
{
107+
var testInstance = new MethodsForReflection();
108+
109+
var helloVoidMethod = typeof(MethodsForReflection).GetMethodInfo(nameof(MethodsForReflection.HelloVoid));
110+
Assert.That(helloVoidMethod, Is.Not.Null);
111+
var helloVoidDelegate = (Action<MethodsForReflection>)helloVoidMethod.MakeDelegate(typeof(Action<MethodsForReflection>));
112+
Assert.That(helloVoidDelegate, Is.Not.Null);
113+
helloVoidDelegate(testInstance);
114+
Assert.That(testInstance.Result, Is.EqualTo("Hello"));
115+
116+
var helloVoidBoolIntMethod = typeof(MethodsForReflection).GetMethodInfo(nameof(MethodsForReflection.Hello), new Type[] { typeof(bool), typeof(int)});
117+
Assert.That(helloVoidBoolIntMethod, Is.Not.Null);
118+
var helloVoidBoolIntDelegate = (Action<MethodsForReflection, bool, int>)helloVoidBoolIntMethod.MakeDelegate(typeof(Action<MethodsForReflection, bool, int>));
119+
Assert.That(helloVoidBoolIntDelegate, Is.Not.Null);
120+
helloVoidBoolIntDelegate(testInstance, true, 5);
121+
Assert.That(testInstance.Result, Is.EqualTo("Hello True 5"));
122+
}
87123
}
88124

89125
public class GenericType<T> { }

0 commit comments

Comments
 (0)