I have an object[] which I am using to dynamically invoke a method via reflection. Due to an AmbiguousMatchException I need to know the types, and it would be difficult to know them ahead of time. So I want to take the "params object[] args" and get a Type[] of all of those.
3 Answers
If you have linq available you can easily get an array of types that matches the objects but I'm not exactly sure it will help in your end goal.
var types = args.Select(arg => arg.GetType()).ToArray();
Is it your intention to inspect the object array for types and then inspect the method signature and try to match them up appropriately?
1 Comment
Achilles
@CodeInChaos true...but his other code that does the dynamic invoking would not work either...
Give the Array.ConvertAll function a shot:
Type[] objTypes = Array.ConvertAll<object, Type>(
objArray,
delegate(object obj){
if(obj==null) return null;
return obj.GetType();
});