我已将此发布到您的other question,但您说得对,它更适合这个:
我假设您通过使用“添加服务引用...”添加此第三方服务来使用它,该服务会为 Reference.cs 中的每个类自动生成一些代码,其签名可能看起来像这样:
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.thirdpartyguys.net")]
public partial class qux: object, System.ComponentModel.INotifyPropertyChanged {
你希望它不是 qux,而是 Qux。如果到目前为止这一切都与您的模型相似,那么您只需将 qux 更改为 Qux,但将 TypeName="qux" 添加到 XmlTypeAttribute,并更改引用中对此类的所有引用。这会在 SOAP 中维护正确的 xml 架构,但让我们在项目中更改名称:
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.thirdpartyguys.net", TypeName = "qux")]
public partial class Qux: object, System.ComponentModel.INotifyPropertyChanged {
当然,如果该 XmlType 属性尚未在定义命名空间的类上,您可以添加它。它只是没有命名空间参数。我刚刚对此进行了测试,它确实允许我使用该服务,并且只需在我使用它的任何地方以不同的名称调用一个对象。
这对你有用吗?
编辑:(向未来的读者简要介绍 SchemaImporterExtension 的想法)
据我了解,当从 WSDL 添加服务引用时,此扩展类可以调用与默认代码生成行为的偏差。您最终仍然拥有一些 Reference.cs 作为您的项目和服务之间的链接,但您可以更改生成的内容。因此,如果我们希望对象始终以大写字母开头,例如,我认为我们的想法是做这样的事情(未经测试):
public class test : SchemaImporterExtension
{
public override string ImportSchemaType(string name, string ns, XmlSchemaObject context,
XmlSchemas schemas, XmlSchemaImporter importer, CodeCompileUnit compileUnit,
CodeNamespace codeNamespace, CodeGenerationOptions options, CodeDomProvider codeGenerator)
{
if (name[0].CompareTo('a') >= 0) //tests if first letter is lowercase
{
CodeExpression typeNameValue = new CodePrimitiveExpression(name);
CodeAttributeArgument typeNameParameter = new CodeAttributeArgument("TypeName", typeNameValue);
CodeAttributeDeclaration xmlTypeAttribute = new CodeAttributeDeclaration("XmlTypeAttribute", typeNameParameter);
compileUnit.AssemblyCustomAttributes.Add(xmlTypeAttribute);
return name.Substring(0, 1).ToUpper() + name.Substring(1);
}
return null;
}
}
理论上,这将写入 XmlType 属性并将名称更改为正确的大小写,从而在 SOAP 中保持正确的 XML 映射。理论上,使用 SchemaImporterExtension 的优点是对服务引用的更新不会覆盖更改。此外,可以进行一般更改,而不是针对每个特定参考。
欢迎成功使用 SchemaImporterExtension 的人发表评论或编辑。