I use this code to get a String array of headings used in a MS Word 2007 document (.docx):
dynamic arr = Document.GetCrossReferenceItems(WdReferenceType.wdRe
try
object arr_r = Document.GetCrossReferenceItems(WdReferenceType.wdRefTypeHeading);
Array arr = ((Array) (arr_r));
string myHeading = (string) arr.GetValue(1);
string[]
is a vector - a 1-d, 0-based array. string[*]
, however, is a regular array that just happens to have one dimension. Basically, you are going to have to handle it as Array
, and either copy the data out, or use the Array
API rather than the string[]
API.
This is the same as the difference between typeof(string).MakeArrayType()
(the vector) and typeof(string).MakeArrayType(1)
(a 1-d non-vector).
The problem is that you're using dynamic
in a situation where it apparently wasn't intended. When the dynamic runtime sees a 1D array, it assumes a vector, and tries to index into it or enumerate it as if it were a vector. This is one of those rare cases where you have a 1D array that is not a vector, so you have to handle it as an Array
:
Array arr = (Array)(object)Document.
GetCrossReferenceItems(WdReferenceType.wdRefTypeHeading);
// works
String arr_elem = arr.GetValue(1);
// now works
IEnumerable list = (IEnumerable)arr;
// now works
foreach (String str in arr)
{
Console.WriteLine(str);
}