Che*_*eva 7 c# interop casting visual-foxpro
当我试图将ArrayVFP9语言COM/DLL返回到我的.NET C#项目时,我收到一个System.Object[*]数组,我无法转换为System.Object[](没有星号).
蒂姆维的解决方案应该可行.你可以使用Linq做一些简单的事情:
object[] newArray = sourceArray.Cast<object>().ToArray();
Run Code Online (Sandbox Code Playgroud)
如果您需要重新创建一个System.Object[*]传回VFP,您可以使用此重载的的Array.CreateInstance方法:
public static Array CreateInstance(
Type elementType,
int[] lengths,
int[] lowerBounds
)
Run Code Online (Sandbox Code Playgroud)
您可以按如下方式使用它:
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);
Run Code Online (Sandbox Code Playgroud)
不幸的是,你无法直接投射它.但是,您可以创建一个新的类型数组object[]并复制数据.就像是...
Array sourceArray = ...;
if (sourceArray.Rank != 1)
throw new InvalidOperationException("Expected a single-rank array.");
object[] newArray = new object[sourceArray.Length];
Array.Copy(sourceArray, sourceArray.GetLowerBound(0),
newArray, 0, sourceArray.Length);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2472 次 |
| 最近记录: |