简体   繁体   中英

C# generate class to namespace

I am guessing the answer is no, but is there a way to move a generated class to a different namespace?

Background:

I have a XSD file that defines a class, which is turned into a C# class via xsd.exe. By default xsd.exe put the class into the global namespace:

XSD:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema
    elementFormDefault="qualified"
    xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
  <xs:complexType name="Delays">
    <xs:sequence>
      <xs:element name="Delay" minOccurs="0" maxOccurs="unbounded">
        <xs:complexType>
          <xs:simpleContent>
            <xs:extension base="xs:double">
              <xs:attribute name="function" type="xs:string"/>
            </xs:extension>            
          </xs:simpleContent>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>

  <xs:element name="Driver">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Delays" type="Delays" minOccurs="1" maxOccurs="1"/>
      </xs:sequence>
      <xs:attribute name="name" type="xs:string"/>
    </xs:complexType>
  </xs:element>

</xs:schema>

generated class:

//------------------------------------------------------------------------------
// <auto-generated>
//     Dieser Code wurde von einem Tool generiert.
//     Laufzeitversion:4.0.30319.42000
//
//     Änderungen an dieser Datei können falsches Verhalten verursachen und gehen verloren, wenn
//     der Code erneut generiert wird.
// </auto-generated>
//------------------------------------------------------------------------------

using System.Xml.Serialization;

// 
// Dieser Quellcode wurde automatisch generiert von xsd, Version=4.6.1055.0.
// 


/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class Driver {

    private DelaysDelay[] delaysField;

    private string nameField;

    /// <remarks/>
    [System.Xml.Serialization.XmlArrayItemAttribute("Delay", IsNullable=false)]
    public DelaysDelay[] Delays {
        get {
            return this.delaysField;
        }
        set {
            this.delaysField = value;
        }
    }

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

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class DelaysDelay {

    private string functionField;

    private double valueField;

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

    /// <remarks/>
    [System.Xml.Serialization.XmlTextAttribute()]
    public double Value {
        get {
            return this.valueField;
        }
        set {
            this.valueField = value;
        }
    }
}

I would prefer the class to be in a custom namespace. However the generation of the .cs file is not in my control. Editing the generated .cs file is bad practice and the .cs file is also not part of the SCM. The "Driver" class is also extended in a separate file that is stored in the SCM.

So, is there anyway to move the class into a custom namespace without

  1. editing the generated code?
  2. modify the generation?
  3. modify the call to xsd.exe?

To change the namespace of generated class you need to pass additional parameter to xsd.exe call:

/n[amespace]**:**namespace Specifies the runtime namespace for the generated types. The default namespace is Schemas.

After regenerating classes you need to move other partial(s) for this classes to new namespace as well.

Okay, I came up with a solution...

I just inherit from the generated class and use MyDriver instead of Driver in my own code:

namespace DriverXMLSerialization
{
    [System.Xml.Serialization.XmlRoot("Driver")]
    public class MyDriver : Driver
    {
        // foo
    }
}

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