[英]How can I access the property of a class in C# from Visual Basic?
I need help in the above topic.我需要上述主题的帮助。 I have done everything required for my VB6 application to work with a C# dll.
我已经完成了我的 VB6 应用程序使用 C# dll 所需的一切。 I got to a point where I am unable to access the property of a class to obtain the ID.
我到了无法访问类的属性来获取 ID 的地步。
I am calling this C# function:我正在调用这个 C# 函数:
public resultRetrieveWIP RetrieveWIP(string serialNumber)
{
string header = string.Format("Bearer {0}", User.Token);
var request = new RestRequest();
request.Method = Method.GET;
request.Resource = "api/wips";
request.AddParameter("serialNumber", serialNumber);
request.AddHeader("Authorization", header);
var client = new RestClient(_iFactoryURL);
var response = client.Execute(request);
var uri = client.BuildUri(request);
_lastRequest = uri.ToString();
_lastResponse = response.Content;
try
{
var settings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, MissingMemberHandling = MissingMemberHandling.Ignore };
return JsonConvert.DeserializeObject<resultRetrieveWIP>(response.Content,settings);
}
catch
{
throw new Exception(jsonErrorHandler("RetrieveWIP",response.Content));
}
}
resultRetrieveWIP has type as follows: resultRetrieveWIP 的类型如下:
public class resultRetrieveWIP
{
public List<wip> wips { get; set; }
}
public class wip
{
public long id { get; set; }
}
In my Visual Basic, I added the above CIMiFactory.dll as my reference then I do this:在我的 Visual Basic 中,我添加了上面的 CIMiFactory.dll 作为我的参考,然后我这样做:
Public oIFactory As New CIMiFactory.iFactory
Public Function iFactory_GetSubAssySN(ByVal strSerialNumber As String, ByVal strPartNumber As String) As String
On Error GoTo errHandler
Dim getWIP As New CIMiFactory.resultRetrieveWIP
Dim wip As New CIMiFactory.wip
iFactory_GetSubAssySN = ""
Set getWIP = oIFactory.RetrieveWIP(strSerialNumber)
**Set wip = getWIP.wips(0)**
iFactory_GetSubAssySN = CStr(wip.id)
Exit Function
errHandler:
iFactory_GetSubAssySN = Err.Description
End Function
I have problem at the line code marked ** **.我在标记为 ** ** 的行代码处遇到问题。
It gave error.它给出了错误。
"Wrong number of arguments or invalid property assignment".
“错误数量的参数或无效的属性分配”。
What is the correct method to get the "id" property value?.获取“id”属性值的正确方法是什么?
Generics are not supported in VB6, so you can't access List(Of T)
items, which is what getWIP.wips(0)
is doing. VB6 不支持泛型,因此您无法访问
List(Of T)
项,这正是getWIP.wips(0)
所做的。 You can use a non-generic Collection instead, in your resultRetrieveWIP
class:您可以在
resultRetrieveWIP
类中使用非通用集合:
public class resultRetrieveWIP
{
public Collection wips { get; set; }
}
See the following article for more details: How To Use a .NET Class with Lists in VB6有关更多详细信息,请参阅以下文章: 如何在 VB6 中使用带有列表的 .NET 类
As Étienne has said, generics cannot be exported to COM.正如Étienne所说,泛型不能导出到 COM。
One option is to add a separate IEnumerable
"wrapper" property just to be accessible through COM, like so:一种选择是添加一个单独的
IEnumerable
"wrapper" 属性,以便通过 COM 访问,如下所示:
public class resultRetrieveWIP
{
public List<wip> wips { get; set; }
[ComVisible(true)]
public IEnumerable GetWipsCOM() => wips;
}
This way code internal to C# can use the correct fully-typed generic list without any changes, and VB6 or whatever other COM consumer can also access the data.通过这种方式,C# 内部的代码可以使用正确的全类型泛型列表而无需任何更改,并且 VB6 或任何其他 COM 使用者也可以访问数据。
IEnumerable
is supported by COM & VB6. COM & VB6 支持
IEnumerable
。 ie you would be able to For Each
through the result of GetWipsCOM()
in VB6.即您将能够通过 VB6 中的
GetWipsCOM()
的结果进行For Each
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.