2

A function returns an object, with an object member filled with an anonymous array of items; how do I get back the individual items?

the object that is returned from a function:

public class FunctionCallResult
{
   ...blah blah members...
    public object ResultObject { get; set; }
}

The function:

FunctionCallResult SomeCrazyFunction(string irrelevant_param1, int some_other_irrelevant_param2)
{
    ... some heavy duty code that raises eyebrows ...
    return new FunctionCallResult{ new object[] { SomeCrazyClassX, AnotherCraxyClassY } };
}

An example call of function:

var myresult = SomeCrazyFunction( "I am the walrus", 42);

But now, how do I get back the individual objects, which are of different classes?

// can't do this... can't index type of object
SomeCrazyClassXType classX = myresult.ResultObject[0];
AnotherCraxyClassYType classY = myresult.ResultObject[1];

So...how can I get these different class types out of the result returned?

4
  • 1
    Anonymous types doesn't really apply here. An anonymous type is something like var t = new { A=1, B=2 }; Commented Aug 29, 2015 at 3:20
  • In your example, you have 2 ints in an anonymous array. My example just has different members than int's. Why is my example not an anonymous array? What would you call it? Commented Aug 30, 2015 at 23:03
  • What I mean is, in my class, there is an 'object' member; so is "new object [] { ... , ... }" fundamentally different from "new [] { ... , ... }" ? Commented Aug 30, 2015 at 23:08
  • Note that my example doesn't have []. see this for syntax An anonymous type is not bound to a name.* Your array doesn't have an anonymous type. It's type is Object. There may be a boxing or reference conversion involved, but the array is still composed of named types. (Note from above: the anonymous type has a name, but the compiler doesn't tell you what it is directly, and you can't use it to instantiate the type.) Commented Aug 31, 2015 at 1:47

1 Answer 1

1

If you "know" ResultObject is an object[] at runtime and the number of items in it and you know their types, you could cast them to SomeCrazyClassXType and AnotherCraxyClassYType after casting ResultObject to an object[] from object.

So:

var resultObjectAsAnObjectArray = (object[])myresult.ResultObject;
var classX = (SomeCrazyClassXType)(resultObjectAsAnObjectArray[0]);
var classY = (AnotherCraxyClassYType)(resultObjectAsAnObjectArray[1]);

I'd have to see more code/what you're actually doing to give a better recommendation.

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

3 Comments

If he knows the order (and then inherently the size) then it's better to create a Tuple.
While it worked just fine, (and thank you!) I will suggest a Tuple
@Gusman I agree that would be better - to be fair the question was, specifically, how do I get back the individual objects - I was just trying to change the least amount of code as possible and answer the question.

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.