简体   繁体   English

C# 中的 MS Word 自动化 - 无法将类型为“System.String[*]”的 object 转换为类型“System.String[]”

[英]MS Word Automation in C# - Unable to cast object of type 'System.String[*]' to type 'System.String[]'

I use this code to get a String array of headings used in a MS Word 2007 document (.docx):我使用此代码获取 MS Word 2007 文档 (.docx) 中使用的标题字符串数组:

dynamic arr = Document.GetCrossReferenceItems(WdReferenceType.wdRefTypeHeading);

Using the debugger, I see that arr is dynamically assigned a String array with titles of all my headings in the document (about 40 entries).使用调试器,我看到arr被动态分配了一个字符串数组,其中包含我在文档中所有标题的标题(大约 40 个条目)。 So far so good.到目前为止,一切都很好。

Then, I want to access the strings, but no matter how I do it, I get the following exception:然后,我想访问字符串,但无论如何我都会得到以下异常:

InvalidCastException: 
           Unable to cast object of type 'System.String[*]' to type 'System.String[]'.

I have tried different ways of accessing the strings:我尝试了不同的访问字符串的方法:

By index:按索引:

String arr_elem = arr[1];

By casting to an IEnumerable:通过转换为 IEnumerable:

IEnumerable list = (IEnumerable)arr;

By using a simple foreach loop:通过使用一个简单的 foreach 循环:

foreach (String str in arr)
{
   Console.WriteLine(str);
}

However, no matter what I try, I always end up with the same exception as shown above.但是,无论我尝试什么,我总是会遇到与上图相同的异常。

Can anyone explain what I am missing here / what I am doing wrong?谁能解释我在这里缺少什么/我做错了什么? And especially String[*] - what does it mean?尤其是String[*] - 这是什么意思?

string[] is a vector - a 1-d, 0-based array. string[]是一个向量 - 一个从 0 开始的一维数组。 string[*] , however, is a regular array that just happens to have one dimension .然而, string[*]是一个恰好只有一维的常规数组。 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.基本上,您必须将其作为Array处理,然后将数据复制出来,或者使用Array API 而不是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).这与typeof(string).MakeArrayType() (向量)和typeof(string).MakeArrayType(1) (一维非向量)之间的区别相同。

try尝试

object arr_r = Document.GetCrossReferenceItems(WdReferenceType.wdRefTypeHeading);
Array arr = ((Array) (arr_r));

string myHeading = (string) arr.GetValue(1);

The problem is that you're using dynamic in a situation where it apparently wasn't intended.问题是您在显然不是故意的情况下使用dynamic 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处理:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# 无法将“system.string”类型的对象转换为“system.int32”类型 - C# unable to cast object of type 'system.string' to type 'system.int32' 无法将“System.DateTime”类型的对象转换为“System.String”类型。 C# - Unable to cast object of type 'System.DateTime' to type 'System.String'. c# 无法将类型“ System.String”强制转换为类型“ System.Object” - Unable to cast the type 'System.String' to type 'System.Object' 无法将“System.String”类型的对象转换为“System.Collections.Generic.List`1[System.String]”类型 - Unable to cast object of type 'System.String' to type 'System.Collections.Generic.List`1[System.String]' 无法将“System.String”类型的 object 转换为在 C# 中键入“Oracle.DataAccess.Client.OracleParameter” - Unable to cast object of type 'System.String' to type 'Oracle.DataAccess.Client.OracleParameter' in C# 无法将对象类型“ System.String [*]”转换为类型“ System.String []” - Unable To Cast object type 'System.String[*]' to type 'System.String[]' 无法将类型类的对象强制转换为“ System.String”类型 - Unable to cast object of type class to type 'System.String' 无法将“DapperRow”类型的对象转换为“System.String”类型 - Unable to cast object of type 'DapperRow' to type 'System.String' 无法将“System.string”类型的对象转换为“....Event”类型 - Unable to cast object of type 'System.string' to type '....Event' 无法将“AttachmentDetailsDto”类型的对象转换为“System.String”类型 - Unable to cast object of type 'AttachmentDetailsDto' to type 'System.String'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM