将COM对象序列化为JSON? [英] Serialize a COM object to JSON?

查看:130
本文介绍了将COM对象序列化为JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在C#代码中设置了对COM DLL(从VB6编译)的引用.

I have set a reference in C# code to a COM DLL (compiled from VB6).

我正在尝试使用JSON.NET(NewtonSoft)从该DLL中的类型序列化COM对象.

I am trying to serialize a COM object from a type in that DLL using JSON.NET (NewtonSoft).

到目前为止我尝试过的代码:

Code that I've tried so far:

    var settings = new JsonSerializerSettings
    {
        Formatting = Formatting.Indented,                
    };

    var serializer = JsonSerializer.Create(settings);

    using (var gz = new GZipStream(File.OpenWrite(filespec), CompressionMode.Compress))
    using (var sw = new StreamWriter(gz))
    using (var writer = new JsonTextWriter(sw))
    {
        serializer.Serialize(writer, objectToSerialize);
    }

结果文件仅包含:

{}

对于本机类,此代码将产生全面的序列化.

whereas for a native class this code would produce a comprehensive serialization.

实际的objectToSerialize具有许多深达几层的对象属性.

The actual objectToSerialize has a number of object properties several layers deep.

是否可以通过某种方式使序列化程序正确使用这种类型的类?

Is there some way to get the serializer to correctly work with this type of class?

注意:

  • 使用由Visual Studio 2015自动生成的标准RCW互操作DLL加载DLL

  • The DLL is loaded using a standard RCW interop DLL autogenerated by Visual Studio 2015

与此类对象的典型用法一样,运行时属性检查器显示该对象属性的本机视图"和动态视图".我怀疑该对象的处理方式与JSON.NET相似,并且如果仅查看本机"视图,则可能错过了所有真实属性.另一方面,VS2015显然可以检查真实对象,因此我希望可以使它起作用.

As is typical for this type of object, the runtime property inspector shows a "Native View" and a "Dynamic View" of the properties of the object. I have a suspicion that this treatment of the object is similar to what JSON.NET is doing, and if it only looks at the "native" view maybe it missed all the real properties. On the other hand VS2015 obviously can inspect the real object thus I'm hopeful that this can be made to work.

谢谢

推荐答案

只要要序列化的对象使用interop生成的类的类型(而不是接口),此方法就可以工作.

This can work as long as the object being serialized uses the type of the interop-generated class - not an interface.

以示例为例,问题是原始VB6代码中objectToSerialize的类型为SomeType.在Visual Studio对象浏览器中,您可以在互操作库中看到几个粗略的等效项:

Say in the example in the question that the type of objectToSerialize was SomeType in the original VB6 code. In the Visual Studio object browser you could see several rough equivalents in the interop library:

  • 接口SomeType

接口_SomeType

Class SomeTypeClass

这是必须用于序列化的最后一个.

现在,奇怪的是,即使SomeType是一个接口,您也可以创建一个new一个接口,并以一种看似正常的方式对生成的对象进行操作.但也很明显,该对象不允许JSON.net序列化使用的反射类型.

Now, curiously even though SomeType is an interface you can create a new one and operate on that resulting object in an apparently normal way. But also apparent is that this object won't allow the type of reflection used by JSON.net serialization.

注意- ObjectDumper 的行为应与这些类型相同.因此,这似乎并不是JSON.NET的特定限制.

Note - ObjectDumper should behave in the same way wrt these types. So all this doesn't seem to be a specific JSON.NET limitation.

这篇关于将COM对象序列化为JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆