I am somehow producing a C# object that contains another object structure. I am not sure how I can get hold of its content.
I have an array whose value is
{object[1,1]}
and type
object[,]
If I unfold it I get:
arr[0,0]
with value
{object[1..1]}
and with type
object{object[]}
and a structure that tells
[1]
with value (for the sake of argument)
myString
and with type
object{string}
I am not sure how I can get hold of the encapsulated string myString that I am searching for.
From the Watch window ((-) means that I unfolded the toggle point (+)):
Name Value Type
(-) arr {object[1,1]} object[,]
L (-) [0,0] {object[1..1]} object{object[]}
L [1] "myString" object{string}
arr[0,0][1] Cannot apply indexing with [] to an expression of type 'object'
Your array seems to be this, based on your last edit:
//an array with length 1 and index starts with 1 instead of 0
var mySpecialArray = Array.CreateInstance(typeof(object), new [] { 1 }, new [] { 1 });
mySpecialArray.SetValue("myString", 1);
var objArray = new object[,]
{
{mySpecialArray},
};
To get the value myString
in code behind you can use this
Console.WriteLine(((Array)objArray[0,0]).GetValue(1));
objArray[0,0][1]
won't work because you aren't casting objArray[0,0]
to the type it is (an array), you can't use [1]
on an instance of type object
visual studio is doing underneath magic and will do the cast internally to access [1]
Here you can find a demo: