[英]How cast a managed array in C++ CLI?
我正在將一些C#代碼遷移到C ++ CLI,並且需要轉換ushort
的托管數組
這是C#代碼
foreach(ushort v in mo.Properties["ChassisTypes"].Value as ushort[])
到目前為止,這是我的嘗試
for each(USHORT ^v in (USHORT[] ^) mo.Properties["ChassisTypes"].Value)
不幸的是,這給了我一些錯誤
1 IntelliSense:不允許輸入不完整的類型
2 IntelliSense:預期為')'
3 IntelliSense:表達式必須具有類類型
在C ++ CLI中強制轉換此類數組的正確方法是什么?
您可以嘗試以下代碼:
for each(UInt16 v in (array<UInt16>^)(mo->Properties["ChassisTypes"]->Value))
{
}
為了安全起見,您可以使用@Ben所說的dynamic_cast
。
array<UInt16>^ chassisTypes = dynamic_cast<array<UInt16>^>(mo->Properties["ChassisTypes"]->Value);
if (chassisTypes != nullptr)
for each(UInt16 v in chassisTypes)
{
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.