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?
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.