无法使用参数类型为Array

问题描述

我有一个COM类,其方法带有以下签名:

public class MyComClass {
    public void SomeMethod(Array myarray) { }
}

现在在另一个项目中,我使用以下代码块将此注册的COM类称为:

Type t = Type.GetTypeFromProgID("ProgIdOf.MyComClass");
object o = Activator.CreateInstance(t);
object[] contentOfMyArray = new object[] { "Hello World" };
t.InvokeMember("SomeMethod",BindingFlags.InvokeMethod,null,o,new object[] { contentOfMyArray });

我正在尝试将contentOfMyArray作为第一个参数传递给SomeMethod方法。但是当我尝试得到这个异常时:

System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation.'
ArgumentException: Value does not fall within the expected range.

方法本身不会引发此异常,我对此进行了检查。 当我将签名更改为public void SomeMethod(object[] myarray) { }或根本不使用任何数组并传递值时,它可以完美地工作。当我这样创建Array类型的对象时:

Array contentOfMyArray = Array.CreateInstance(typeof(object),1);
contentOfMyArray.SetValue("Hello World",0);

并将其作为参数传递仍然会引发此异常。

我阅读了很多有关编组内容,但是我无法使其正常工作。 顺便说一句。我不能使用像object[]这样的“普通”数组,我必须使用Array类型。

我希望您能提供帮助,我对COM一点都不了解。谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)