1

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.

1
  • Are you using DynamicInvoke? And I'm not sure how knowing the types would help since the you still need to decide between the ambiguous methods. Commented Jan 17, 2011 at 22:27

3 Answers 3

3

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?

Sign up to request clarification or add additional context in comments.

1 Comment

@CodeInChaos true...but his other code that does the dynamic invoking would not work either...
1

Please try this:

        IList<Type> typeList = new List<Type>();

        foreach(object item in args)
        {
            typeList.Add(item.GetType());
        }

        typeList.ToArray();

Comments

0

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();
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.