gpt4 book ai didi

c# - XSD 模式到 COM 接口(interface)

转载 作者:太空狗 更新时间:2023-10-29 21:17:49 26 4
gpt4 key购买 nike

我必须支持旧版 Visual Basic 6.0 客户端,它需要解析 XML 文件。这些由相当大且复杂的 XSD 模式描述。为了简化解析过程,我通过 Windows SDK xsd.exe 工具创建了 C# 类,将它们添加到 C# 库项目中,并设置了“Make assembly COM-Visible”属性。不幸的是,生成的类型库没有任何值(value),因为它只是为所有复杂类型公开了空接口(interface)。

为说明此行为,请考虑以下 XSD 架构:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:customers" xmlns:c="urn:customers">
<xsd:element name="catalog" type="c:CatalogData"/>
<xsd:complexType name="AddressData">
<xsd:sequence>
<xsd:element name="no" type="xsd:integer"/>
<xsd:element name="road" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="CustomerData">
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="address" type="c:AddressData"/>
<xsd:element name="order_date" type="xsd:date"/>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:string"/>
</xsd:complexType>
<xsd:complexType name="CatalogData">
<xsd:sequence>
<xsd:element name="customer" type="c:CustomerData" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>

xsd 工具创建以下源文件:

//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.34209
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System.Xml.Serialization;

//
// This source code was auto-generated by xsd, Version=4.0.30319.33440.
//


/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:customers")]
[System.Xml.Serialization.XmlRootAttribute("catalog", Namespace="urn:customers", IsNullable=false)]
public partial class CatalogData {

private CustomerData[] customerField;

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("customer", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public CustomerData[] customer {
get {
return this.customerField;
}
set {
this.customerField = value;
}
}
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:customers")]
public partial class CustomerData {

private string nameField;

private AddressData addressField;

private System.DateTime order_dateField;

private string idField;

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string name {
get {
return this.nameField;
}
set {
this.nameField = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public AddressData address {
get {
return this.addressField;
}
set {
this.addressField = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, DataType="date")]
public System.DateTime order_date {
get {
return this.order_dateField;
}
set {
this.order_dateField = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string id {
get {
return this.idField;
}
set {
this.idField = value;
}
}
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:customers")]
public partial class AddressData {

private string noField;

private string roadField;

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, DataType="integer")]
public string no {
get {
return this.noField;
}
set {
this.noField = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string road {
get {
return this.roadField;
}
set {
this.roadField = value;
}
}
}

生成的类型库如下所示:

// Generated .IDL file (by the OLE/COM Object Viewer)
//
// typelib filename: xsd.tlb

[
]
library xsd
{

importlib("mscorlib.tlb");

importlib("stdole2.tlb");

// Forward declare all types defined in this typelib
interface _CatalogData;
interface _CustomerData;
interface _AddressData;

[
]
coclass CatalogData {
[default] interface _CatalogData;
interface _Object;
};

[
]
coclass CustomerData {
[default] interface _CustomerData;
interface _Object;
};

[
]
coclass AddressData {
[default] interface _AddressData;
interface _Object;
};

[
]
interface _CatalogData : IDispatch {
};

[
]
interface _CustomerData : IDispatch {
};

[
]
interface _AddressData : IDispatch {
};
};

我知道,我可以手动创建所需的 COM 接口(interface)以公开所有嵌套属性。然而,由于复杂的 XSD 架构,生成的 C# 类文件超过 3000 行,我需要永远为每个部分类创建一个接口(interface)。

是否有替代方案可以加快该过程?或者有人知道另一种工具,它可以从 XSD 架构生成 COM 接口(interface)/类,最好是通过 ATL 或 C++?

最佳答案

您可能使用了“项目”>“属性”>“应用程序”>“程序集信息”按钮并勾选了“使程序集 COM 可见”选项。一种使程序集中所有具有默认构造函数的公共(public)类对 COM 客户端应用程序可见的非常快速的方法。这使用 [ClassInterface] attribute 的默认值,因为它没有明确应用于类,所以它是 ClassInterfaceType.AutoDispatch

这是一个非常安全的设置,它有助于客户端代码对公开类中的更改更具弹性。当类更改但客户端应用程序未重新编译时,您将获得的运行时错误更易于解释。早期绑定(bind)有很多更糟糕的失败模式,包括使用完全错误的属性或客户端应用程序因 AccessViolation 异常而失败。

考虑到您公开的数据容易频繁更改,这并不是一个坏主意。

但不是您所要求的。更改默认的 [ClassInterface] 非常简单。打开 Properties > AssemblyInfo.cs 源代码文件,使其看起来像这样:

// Setting ComVisible to false makes the types in this assembly not visible 
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(true)]
[assembly: ClassInterface(ClassInterfaceType.AutoDual)]

添加了最后一行。重建您的项目,您现在将看到界面不再为空,并且自动完成功能在 VB6 IDE 中正常工作。

关于c# - XSD 模式到 COM 接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31525644/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com