[英]Pass VB6 object to C#
我尝试通过互操作将 vb6 object 传递给我的 c# 代码。 VB6 能够到达 c# 代码,但是当尝试读取 object 时,它没有任何属性。 谁能指出我在哪里弄错了?
[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());
}
}
我遇到了同样的问题,我认为按照您尝试的方式进行操作会很困难。 在我的情况下,解决方案是在 VB6 中创建一个方法,将对象转换为 JSON 并将其作为字符串传递给方法。 这样在 C# 中很容易检索您需要的数据。
在您的示例中,它看起来像这样:
{"School":{
"Student":{
"Name":"Bruce Willis",
"id":1
} }}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.