可以元帅字符串[]对SAFEARRAY变种? [英] Can on marshal a string[] to a safearray variant?
问题描述
当我我的元帅函数返回一个的String []
为 UnmanagedType.Struct
与 SafeArraySubType = VarEnum.VT_ARRAY
在
When I marshal my function returning a string[]
as UnmanagedType.Struct
with SafeArraySubType = VarEnum.VT_ARRAY
as in
namespace StackOverflow
{
[ComVisible(true)]
[Guid("4BDC43D4-8FD7-4F58-BEE5-E57C3C144C1B")]
public class Array
{
[return: MarshalAs(UnmanagedType.Struct, SafeArraySubType = VarEnum.VT_ARRAY)]
public string[] StringArray()
{
return new string[] { "foo", "bar" };
}
}
}
我期待得到一个变种( UnmanagedType
枚举)
结构体结果
变种,它是用于封送托管格式化类和值类型。
Struct
A VARIANT, which is used to marshal managed formatted classes and value types.
不过VBScript代码
However the VBScript code
WScript.Echo TypeName(CreateObject("StackOverflow.Array").StringArray)
报告字符串()
(这是不是一个变()
,所以我会得到类型
missmatch错误后accesing阵列时)。
reports String()
(which is not a Variant()
and therefore I will get type
missmatch errors later when accesing the array).
当我我的代码更改为
public object[] ObjectArray()
{
return new object[] { "foo", "bar" };
}
public object PlainObject()
{
return new object[] { "foo", "bar" };
}
VBScript代码似乎正常工作,但是我想知道为什么我不能返回的String []
并手动编组到含变种的
SAFEARRAY的变体。
the VBScript code seems to works fine, however I would like to know why I cannot return a string[]
and manually marshall it to a variant containing a
safearray of variants.
看来, SafeArraySubType
不起作用。什么是我的错?
It seems that the SafeArraySubType
has no effect. What is my mistake?
推荐答案
那是因为SafeArraySubType只适用于SAFEARRAYS。
Thats because the SafeArraySubType only applies to SafeArrays.
MarshalAs (UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_VARIANT)]
这篇关于可以元帅字符串[]对SAFEARRAY变种?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!