简体   繁体   English

如何从 Visual Basic 访问 C# 中类的属性?

[英]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.

相关问题 如何在c#中访问属性属性中的类变量? - How can I access to class variable in property attribute in c#? 如何从 C# 中的变量访问类属性? - How Do I Access Class Property From Variable in C#? 如何将C#库集成到Visual Basic中? - How can I integrate C# libraries into Visual Basic? 如果在Visual Studio解决方案中使用类的属性,我如何检查C#? - How can I check in C# if a property of class is used in a Visual Studio solution? 我如何通过C#访问泛型类中的动态类属性? - How i access dynamic class property in generic class by c#? 使用C#,如何使用参数访问VB类的默认属性的getter / setter? - Using C#, how can I access the getter/setter of a VB class's default property with parameter? C# - 使用对基类的引用,如果我知道我的引用是对继承类的引用,我如何访问继承类的属性? - C# - with a reference to a base class, how can I access a property of the inherited class if I know that my reference is to the inherited class? 如何正确使用C#Visual Studio 2010中另一个类中的一个类的方法 - How can I properly use methods from a class in another class in C# Visual Studio 2010 如何从另一个类 C#/MVC 访问属性 - How to Access to a property from another class C#/MVC 在Visual Basic中访问与C#相反的属性 - Accessing a property in Visual Basic as opposed to C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM