简体   繁体   中英

Pass VB6 object to C#

I try to pass vb6 object to my c# code via interop. The VB6 able to reach the c# code, but when try to read the object, it does not have any properties in it. Can anyone pls point out where did i get it wrong?

[VB6]

Set CSharp = CreateObject("CSharp.School")    
Dim p As School.Student
Set p = New School.Student
p.Name = "Bruce Willis"
p.id = 1
    
Call CSharp.Register(p)

[C#]

[Guid("1e54b90a-8909-41f5-abcd-87406c261f90")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[ComVisible(true)]
public interface ISchool
{
    Register(object student)
}

Guid("c2d41a00-d000-49e7-abcd-236c6cb9d862")]
[ClassInterface(ClassInterfaceType.None), ProgId("CSharp.School")]
[ComVisible(true)]
public class School : ISchool
{
    public void Register(object student)
    {
        Type myType = student.GetType();
        IList<PropertyInfo> props = new List<PropertyInfo>(student.GetProperties());
        //no property found in the object argument
        Console.WriteLine("Count : " + props.Count.ToString());
    }
}

I had the same problem, I think it will be difficult to do the way you are trying. In my case the solution was to make a method in VB6 to convert the objects into JSON and pass it to the method as a string. That way in C# it's easy to retrieve the data you need.

In your example it would look something like this:

 {"School":{
  "Student":{
     "Name":"Bruce Willis",
     "id":1
  }   }}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM