Skip to content

Commit 2880dba

Browse files
author
Stewart Miles
committed
Fixed execution of methods with interface args using reflection.
GetType() returns the concrete type of an object which caused InvokeMethod() and InvokeStaticMethod() to not match methods that take arguments that require interface arguments. This changes the type check of arguments, to handle overloaded methods, to check whether an argument is assignable given an argument which checks for compatibility with the expected interface. Bug: 138224253 Change-Id: I575a03b10bd3b8576fb1c4503c57ddf9052587ca
1 parent 56d476e commit 2880dba

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

source/VersionHandler/src/VersionHandler.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,10 @@ public static object InvokeMethod(
516516
if (position < numberOfPositionalArgs) {
517517
var positionalArg = args[position];
518518
// If the parameter type doesn't match, ignore this method.
519-
if (positionalArg != null && parameterType != positionalArg.GetType()) break;
519+
if (positionalArg != null &&
520+
!parameterType.IsAssignableFrom(positionalArg.GetType())) {
521+
break;
522+
}
520523
parameterValues[position] = positionalArg;
521524
matchedPositionalArgs ++;
522525
} else if (parameter.RawDefaultValue != DBNull.Value) {
@@ -525,7 +528,10 @@ public static object InvokeMethod(
525528
object namedArg;
526529
if (namedArgs.TryGetValue(parameter.Name, out namedArg)) {
527530
// If the parameter type doesn't match, ignore this method.
528-
if (namedArg != null && parameterType != namedArg.GetType()) break;
531+
if (namedArg != null &&
532+
!parameterType.IsAssignableFrom(namedArg.GetType())) {
533+
break;
534+
}
529535
namedValue = namedArg;
530536
matchedNamedArgs ++;
531537
}

source/VersionHandler/test/reflection/Assets/PlayServicesResolver/Editor/TestReflection.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ public static string GenericHelloWithCustomerNameAndPronoun(string customerName,
5050
return String.Format("{0} {1}", GenericHelloWithPronoun(pronoun: pronoun), customerName);
5151
}
5252

53+
public static string GenericHelloWithCustomerNameAndSuffixes(
54+
string customerName, IEnumerable<string> suffixes = null) {
55+
string fullName = Greeter.GenericHelloWithCustomerName(customerName);
56+
if (suffixes != null) {
57+
foreach (var suffix in suffixes) fullName += " " + suffix;
58+
}
59+
return fullName;
60+
}
61+
62+
5363
private string MyNameIs() {
5464
return String.Format(", my name is {0}", name);
5565
}
@@ -91,9 +101,10 @@ static TestReflection() {
91101
TestInvokeStaticMethodWithNamedArg,
92102
TestInvokeStaticMethodWithArgAndNamedArgDefault,
93103
TestInvokeStaticMethodWithArgAndNamedArg,
104+
TestInvokeStaticMethodWithArgAndNamedInterfaceArg,
94105
TestInvokeInstanceMethodWithNoArgs,
95106
TestInvokeInstanceMethodWithNamedArgDefault,
96-
TestInvokeInstanceMethodWithNamedArg
107+
TestInvokeInstanceMethodWithNamedArg,
97108
}) {
98109
var testName = test.Method.Name;
99110
Exception exception = null;
@@ -213,6 +224,16 @@ static bool TestInvokeStaticMethodWithArgAndNamedArg() {
213224
new Dictionary<string, object> { { "pronoun", "Mrs" } } ));
214225
}
215226

227+
// Invoke a static method with a positional and named interface arg.
228+
static bool TestInvokeStaticMethodWithArgAndNamedInterfaceArg() {
229+
IEnumerable<string> suffixes = new string[] { "BSc", "Hons", "PhD", "Kt", "MPerf" };
230+
return CheckValue("Hello Angie BSc Hons PhD Kt MPerf",
231+
(string)VersionHandler.InvokeStaticMethod(
232+
typeof(Greeter), "GenericHelloWithCustomerNameAndSuffixes",
233+
new object[] { "Angie" },
234+
new Dictionary<string, object> { { "suffixes", suffixes } }));
235+
}
236+
216237
// Invoke an instance method with no args.
217238
static bool TestInvokeInstanceMethodWithNoArgs() {
218239
return CheckValue("Hello, my name is Sam",

0 commit comments

Comments
 (0)