如何投放System.Object的[*]以System.Object的[] [英] How to cast System.Object[*] to System.Object[]

查看:178
本文介绍了如何投放System.Object的[*]以System.Object的[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我试图在VFP9语言COM / DLL中的阵列回到我的.NET C#项目
我收到系统。对象[*] 阵列,我不能转换为 System.Object的[] (不带星号)。


< DIV CLASS =h2_lin>解决方案

Timwi的解决方案应该很好地工作。你可以做一些使用LINQ简单一点:

 对象[] newArray = sourceArray.Cast<对象>()ToArray的( ); 

在情况下,你需要重新创建一个 System.Object的[*] 传回VFP,您可以使用的这种超负荷 Array.CreateInstance 方法:

 公共静态数组的CreateInstance(
类型元素类型,
INT []的长度,
INT [] lowerBounds


$ b $

  [对象] normalArray = ... 
$ b $是b

您可以按如下方式使用它b //创建一个下界1 $ ​​b $ b阵列arrayStartingAt1 =
Array.CreateInstance(
的typeof(对象),
新的[] {} normalArray.Length,$ b $的数组b新的[] {1});

Array.Copy(normalArray,0,arrayStartingAt1,1,normalArray.Length);


When I Tried to return an Array in VFP9 language COM/DLL to my .NET C# project I receive a System.Object[*] array and I can not cast to System.Object[] (Without asterisk).

解决方案

Timwi's solution should work fine. You can do something a bit simpler using Linq:

object[] newArray = sourceArray.Cast<object>().ToArray();

In case you need to recreate a System.Object[*] to pass it back to VFP, you can use this overload of the Array.CreateInstance method:

public static Array CreateInstance(
    Type elementType,
    int[] lengths,
    int[] lowerBounds
)

You can use it as follows:

object[] normalArray = ...

// create array with lower bound of 1
Array arrayStartingAt1 =
    Array.CreateInstance(
        typeof(object),
        new[] { normalArray.Length },
        new[] { 1 });

Array.Copy(normalArray, 0, arrayStartingAt1, 1, normalArray.Length);

这篇关于如何投放System.Object的[*]以System.Object的[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆