Reflection GetMethod with a type paramenter
Asked Answered
A

1

1

The following line is not running properly, and I'm not sure why. The Error: System.Reflection.TargetParameterCountException: parameters do not match signature

And the line of code in question:

typeof(Resources).GetMethod("LoadAll", new Type[] { typeof(System.String), typeof(System.Type)});

Displaying all functions of the Resources class shows:

UnityEngine.Object[] FindObjectsOfTypeAll(System.Type)
T[] FindObjectsOfTypeAll[T]()
UnityEngine.Object Load(System.String)
T Load[T](System.String)
UnityEngine.Object Load(System.String, System.Type)
UnityEngine.ResourceRequest LoadAsync(System.String)
UnityEngine.ResourceRequest LoadAsync[T](System.String)
UnityEngine.ResourceRequest LoadAsync(System.String, System.Type)
UnityEngine.Object[] LoadAll(System.String, System.Type)
UnityEngine.Object[] LoadAll(System.String)
T[] LoadAll[T](System.String)
UnityEngine.Object GetBuiltinResource(System.Type, System.String)
T GetBuiltinResource[T](System.String)
Void UnloadAsset(UnityEngine.Object)
UnityEngine.AsyncOperation UnloadUnusedAssets()
Boolean Equals(System.Object)
Int32 GetHashCode()
System.Type GetType()
System.String ToString()
System.Reflection.MethodInfo[]

I'm trying to match UnityEngine.Object[] LoadAll(System.String, System.Type). Any thoughts on what may be the issue?

Bonus if you can make a line that returns a methodinfo object for "T[] LoadAllT", specific to a given type.

Actinology answered 20/2, 2018 at 0:34 Comment(4)
The issue is that you are trying to get an instance level method while the LoadAll is static. Try this one. typeof(Resources).GetMethod("LoadAll", System.Reflection.BindingFlags.Static, new Type[] { typeof(System.String), typeof(System.Type)}, null );Stallings
Had to add the a binder, I assumed the default. But it doesn't find the method. Here's the line I used: return typeof(Resources).GetMethod("LoadAll", System.Reflection.BindingFlags.Static, Type.DefaultBinder, new Type[] { typeof(System.String), typeof(System.Type) }, null);Actinology
I am mistaken, that line of code appears to work, my test just seemed to have failed... If you copy down I'll mark it as an answer.Actinology
Does this answer your question? Ambiguous match found when using reflection to find Generic methodAlgesia
S
0

The issue is that you are trying to get an instance level method while the LoadAll is static.

Try this one:

typeof(Resources).GetMethod("LoadAll", 
                            System.Reflection.BindingFlags.Static,   
                            new Type[] { typeof(System.String),typeof(System.Type)},
                             null);
Stallings answered 20/2, 2018 at 8:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.