【问题标题】:interop class wants arrays of type [*]互操作类需要 [*] 类型的数组
【发布时间】:2017-02-09 12:02:48
【问题描述】:

我有一个 Labview interop assembly,它需要 string[* ]int[* ]customType[*] 用于各种结构和方法。

例如,这是元数据中结构的声明:

public struct AppImage__32Properties
{
   ...
    public FPGA__32Properties[] fPGA__32Properties;
...}

fPGA__32Properties 是一个结构体数组。

现在我想给它分配一个相同类型的空数组 (其实例未初始化),然后继续填充其字段。

AppInfoDummy[0].appImage__32Properties.fPGA__32Properties =  
          new FPGA__32Properties[1];   

但它会导致

"无法将类型 'DeployImage_RAD.FPGA_32Properties[]' 隐式转换为 DeployImage_RAD.FPGA_32Properties[*]".

我对来自该interop 程序集的type [*] 的其他struct、int 和string arrays 也有同样的问题。

显然someType[*] 是一个数组,它的起始索引不是零。 (What does System.Double[*] mean) 并且有一种方法可以访问它的值。但是如何给它赋值呢?

顺便说一句:我之前在 Visual Studio 2012 Profession 中处理过这个项目,但没有出现这个问题。现在我使用 Visual Studio 2015 Prof.

【问题讨论】:

    标签: c# arrays interop marshalling labview


    【解决方案1】:

    您需要直接使用Array 类。在这种情况下,C# 并不能真正帮助您(VB.NET 将使这变得容易得多,因为它与 C# 不同,本机支持自定义上下界)。

    作为一个示例,如果您需要一个从 1 到 10 的双精度数组,则将其定义为:

    Array doubleArray = Array.CreateInstance(typeof(double), new []{ 10 }, new []{ 1 });
    

    访问索引也要经过Array

    doubleArray.SetValue(42d, 3); // Set value 42d at index 3
    doubleArray.GetValue(3); // Get value at index 3 (offset 2 in this case)
    

    Array 实例创建一个安全的包装器(或静态类型的副本)可能是一个好主意,这只是为了使其更易于使用,并且更明确哪个Array 是哪个。

    【讨论】:

    • 谢谢卢安。 Visual Studio 接受这一点,我可以构建解决方案。但是,我还无法对其进行测试,因为该代码中的 string[*] 存在另一个问题。当我测试成功后,我会标记你的答案。
    • 嗨 Luaan,我无法使用 Array.CreateInstance 创建字符串 [ * ] 类型的实例。其中一些数组是 string[ * ].
    • @Simorgh 它对我来说很好用。您在初始化字符串数组中的值时遇到问题吗?你有什么例外吗?
    猜你喜欢
    • 1970-01-01
    • 2017-09-19
    • 2022-06-17
    • 2013-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-27
    相关资源
    最近更新 更多