VBA调用C#对象方法,传递double数组参数
2005-12-21 17:25
921 查看
VBA方法可以通过COM Interop来调用C#对象方法。基本方法是公开.NET对象通过COM Interop,然后就可以调用.net 对象方法并传递参数了,但是参数如果包含double数组,VBA将出现如下错误信息
"Function or interface marked as restricted, or the function uses an Automation type not supported in Visual Basic"
下面将解决这个问题
//interfaceExposer.cs
using System;
namespace blah
{
public interface interfaceExposer
{
int callableMethodSimple(double a);
}
}
//cssClass.cs
using System;
namespace blah
{
public class callableClass : interfaceExposer
{
public int callableMethodSimple(double a)
{
return (int)a;
}
}
}
重点:为项目注册COM Interop。VS 2003中选择“项目属性”-“配置属性”-“生成”设置“为COM Interop注册”为True,再编译。
VBA:选择工具引用选择COM对象。
'VBA code
Public cssObject As New SendArray.callableClass 'SendArray 是项目名称
Dim iClass As interfaceExposer
Sub MyRoutine()
Set iClass = cssObject
Dim result As Integer
result = IClass.callableMethodSimple(5.0)
End Sub
VBA传递double数组
//cssClass.cs
using System;
using System.Reflection;
namespace blah
{
public class callableClass : interfaceExposer
{
.. .
public int callableMethodArray(object a)
{
double[] thisVect = LoadComObjectIntoDoubleArray(a);
return 0;
}
private double[] LoadComObjectIntoDoubleArray(object comObject)
{
Type thisType = comObject.GetType();
Type dblType = Type.GetType("System.Double[*]");
double[] doubleArray = new double[1];
if(thisType == dblType)
{
object[] args = new object[1];
int numEntries = (int)thisType.InvokeMember("Length", BindingFlags.GetProperty, null, comObject, null);
doubleArray = new double[numEntries];
for(int j1=0; j1 < numEntries; j1++)
{
args[0] = j1+1;
doubleArray[j1] = (double)thisType.InvokeMember("GetValue", BindingFlags.InvokeMethod, null, comObject, args);
}
} // End if(thisType == dblType)
return doubleArray;
} // End LoadComObjectIntoDoubleArray()
}
}
'VBA code
Public cssObject As New SendArray.callableClass 'SendArray 是项目名称
Dim iClass As interfaceExposer
Sub MyRoutine()
Set iClass = cssObject
Dim result As Integer
Dim SendArray(1 To 2) As Double
SendArray(1) = 5.2
SendArray(2) = 7.5
result = iClass.callableMethodArray(SendArray)
End Sub
"Function or interface marked as restricted, or the function uses an Automation type not supported in Visual Basic"
下面将解决这个问题
VBA调用C#对象方法
C#类callableClass必须继承自接口interfaceExposer。接口interfaceExposer的方法被公开可以使用VBA调用。//interfaceExposer.cs
using System;
namespace blah
{
public interface interfaceExposer
{
int callableMethodSimple(double a);
}
}
//cssClass.cs
using System;
namespace blah
{
public class callableClass : interfaceExposer
{
public int callableMethodSimple(double a)
{
return (int)a;
}
}
}
重点:为项目注册COM Interop。VS 2003中选择“项目属性”-“配置属性”-“生成”设置“为COM Interop注册”为True,再编译。
VBA:选择工具引用选择COM对象。
'VBA code
Public cssObject As New SendArray.callableClass 'SendArray 是项目名称
Dim iClass As interfaceExposer
Sub MyRoutine()
Set iClass = cssObject
Dim result As Integer
result = IClass.callableMethodSimple(5.0)
End Sub
VBA传递double数组
//cssClass.cs
using System;
using System.Reflection;
namespace blah
{
public class callableClass : interfaceExposer
{
.. .
public int callableMethodArray(object a)
{
double[] thisVect = LoadComObjectIntoDoubleArray(a);
return 0;
}
private double[] LoadComObjectIntoDoubleArray(object comObject)
{
Type thisType = comObject.GetType();
Type dblType = Type.GetType("System.Double[*]");
double[] doubleArray = new double[1];
if(thisType == dblType)
{
object[] args = new object[1];
int numEntries = (int)thisType.InvokeMember("Length", BindingFlags.GetProperty, null, comObject, null);
doubleArray = new double[numEntries];
for(int j1=0; j1 < numEntries; j1++)
{
args[0] = j1+1;
doubleArray[j1] = (double)thisType.InvokeMember("GetValue", BindingFlags.InvokeMethod, null, comObject, args);
}
} // End if(thisType == dblType)
return doubleArray;
} // End LoadComObjectIntoDoubleArray()
}
}
'VBA code
Public cssObject As New SendArray.callableClass 'SendArray 是项目名称
Dim iClass As interfaceExposer
Sub MyRoutine()
Set iClass = cssObject
Dim result As Integer
Dim SendArray(1 To 2) As Double
SendArray(1) = 5.2
SendArray(2) = 7.5
result = iClass.callableMethodArray(SendArray)
End Sub
相关文章推荐
- VBA调用C#对象方法,传递double数组参数
- dwr调用参数为 对象数组 的方法(js中直接传递obj数组)
- 怎样在前端Javascript中调用C#方法(2)传递参数(附源码+高手勿入)
- c# - 外部对象作为参数调用方法时需要注意的问题
- C#调用c++的dll,结构体数组作为引用参数的传递方式
- java学习笔记(四)----对象、数组作为参数传递,静态变量、静态方法的使用,内部类,使用文档注释
- C#调用vb dll 参数传递--数组、整型等(c#调用com)
- PB中调用API函数时传递数组参数的方法
- c#调用存储过程时 SqlParameter 中传递参数时ref参数的设置方法
- android ndk 开发jni调用对象方法,数组参数
- C# 实例对象作为参数传递与方法内部的形参关系
- Android-调用本地方法传递数组参数/NDK-JNI开发实例(五)
- Javascript 调用C# 代码并传递参数的两种方法
- JAVA本地方法调用(2)数组参数传递
- c#方法中调用参数的值传递方式和引用传递方式,以及ref与out的区别
- 【原创】PB调用SAP的Web Service结构体数组参数方法详解(使用C#调用Web Service生成dll)
- PB调用SAP的Web Service结构体数组参数方法详解(使用C#调用Web Service生成dll)
- Delphi动态事件深入分析(对象方法在调用的时候会传递一个隐含的Self指针,而该指针的值在EAX中。即左边第一个参数)
- c#方法中调用参数的值传递方式和引用传递方式,以及ref与out的区别
- 学习笔记--静态方法调用,多肽,操作符重载,3中参数传递方式,二维数组与交错数组