背景信息:
我正在构建一个 SCADA 系统,它在 VBA 上运行,我寻求 C# 的一些功能。我用 C# 构建了一个 DLL 库,并获得了在 DLL 和 VBA 之间流动的基本数据。
[ComVisible(true), ClassInterface(ClassInterfaceType.AutoDual)]
public class BE_Log
{
public string DateTime
{
[return: MarshalAs(UnmanagedType.BStr)]
get;
[param: MarshalAs(UnmanagedType.BStr)]
set;
}
public string User
{
[return: MarshalAs(UnmanagedType.BStr)]
get;
[param: MarshalAs(UnmanagedType.BStr)]
set;
}
public string SCADA
{
[return: MarshalAs(UnmanagedType.BStr)]
get;
[param: MarshalAs(UnmanagedType.BStr)]
set;
}
public string Tag
{
[return: MarshalAs(UnmanagedType.BStr)]
get;
[param: MarshalAs(UnmanagedType.BStr)]
set;
}
public string Area1
{
[return: MarshalAs(UnmanagedType.BStr)]
get;
[param: MarshalAs(UnmanagedType.BStr)]
set;
}
public string Area2
{
[return: MarshalAs(UnmanagedType.BStr)]
get;
[param: MarshalAs(UnmanagedType.BStr)]
set;
}
public string Description
{
[return: MarshalAs(UnmanagedType.BStr)]
get;
[param: MarshalAs(UnmanagedType.BStr)]
set;
}
public string ValueOld
{
[return: MarshalAs(UnmanagedType.BStr)]
get;
[param: MarshalAs(UnmanagedType.BStr)]
set;
}
public string ValueNew
{
[return: MarshalAs(UnmanagedType.BStr)]
get;
[param: MarshalAs(UnmanagedType.BStr)]
set;
}
public BE_Log(string DataTime, string User, string SCADA, string Tag, string Area1, string Area2,string Description)
{
this.DateTime = DateTime;
this.User = User;
this.SCADA = SCADA;
this.Tag = Tag;
this.Area1 = Area1;
this.Area2 = Area2;
this.Description = Description;
}
public BE_Log(string DataTime, string User, string SCADA, string Tag, string Area1, string Area2, string Description, string ValueOld, string ValueNew)
{
this.DateTime = DateTime;
this.User = User;
this.SCADA = SCADA;
this.Tag = Tag;
this.Area1 = Area1;
this.Area2 = Area2;
this.Description = Description;
this.ValueOld = ValueOld;
this.ValueNew = ValueNew;
}
}
我像这样返回了课程:
[ComVisible(true), ClassInterface(ClassInterfaceType.AutoDual)]
public class TI
{
private BLL_LogBook bll;
public TI()
{
bll = new BLL_LogBook();
}
[return: MarshalAs(UnmanagedType.SafeArray, SafeArraySubType=VarEnum.VT_USERDEFINED)] // SafeArrayUserDefinedSubType = typeof(BE_Log)
public BE_Log[] CreateLogBook()
{
List<BE_Log> logs = bll.GetLogEntry();
return logs.ToArray();
}
}
我的数据层:
public class BLL_LogBook
{
public List<BE_Log> GetLogEntry()
{
List<BE_Log> logs = new List<BE_Log>();
logs.Add(new BE_Log("05-05-2015", "some user", "scada01", "LA010NDA10CU12XQ12", "Ribe", "Esbjerg", "Some short description"));
logs.Add(new BE_Log("06-05-2015", "test user", "scada01", "LA010NDA10CU12XB05", "Herning", "KBH", "Some long description"));
logs.Add(new BE_Log("07-05-2015", "normal user", "scada02", "LA010NDA10CU12YQ01", "Åhus", "Tønder", "Some test description"));
return logs;
}
}
VBA 调用的静态方法:
static class UnmanagedExports
{
[DllExport]
[return: MarshalAs(UnmanagedType.IDispatch)]
static Object TI_Object()
{
return new TI();
}
}
在 VBA 中,我以这种方式获取数据:
Declare Function TI_Object Lib "<path>\\TJI.dll" () As Object
Sub TestTheTestClass()
Dim TJI As Object
Set TJI = TI_Object()
Dim test As Variant
test = TJI.CreateLogBook()
Dim log As Variant
Set log = test(0)
Debug.Print log.User
End Sub
现在我的问题:
我如何返回类“BE_Log”的数组或列表
编辑: 这是我卡住的地方:http ://puu.sh/hnPGe/472ff863d0.png
我一直在尝试编写一些 Microsoft 的文档,但运气不佳。
我遵循的原始指南是这个:
http ://www.analystcave.com/excel-use-c-sharp-in-excel-vba/
他陈述了以下内容,但是我不完全理解。
如果您使用数组作为参数,请务必使用 C# “ref” get by reference 选项,例如 ref int[] ar
我认为这与“MarshalAs”或我在 VBA 中读取数据的方式有关