c#XSD生成类的ToString方法

时间:2015-10-08 14:33:48

标签: c# xsd

我有一个使用XSD.exe生成的类,但是我想为这个对象设置一个ToString方法,所以我可以将它们添加到列表框等。我可能要在将来从XSD文件重新生成这些类,所以我不想手动覆盖生成的类上的方法。如果没有创建新类,有没有办法解决这个问题呢?

我生成的类看起来像这样;我想要一个ToString方法来显示一些类变量,而不是它当前正在进行的类的名称。

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.1432")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class cardOnFileResponse {

    private string paymentInstrumentRefField;

    private string partyIdField;

    private string friendlyNameField;

    private string cardholderNameField;

    private string maskedPANField;

    private string startDateField;

    private string expiryDateField;

    private string issueNoField;

    private cardType cardTypeField;

    private bool cardTypeFieldSpecified;

    /// <remarks/>
    public string paymentInstrumentRef {
        get {
            return this.paymentInstrumentRefField;
        }
        set {
            this.paymentInstrumentRefField = value;
        }
    }

    /// <remarks/>
    public string partyId {
        get {
            return this.partyIdField;
        }
        set {
            this.partyIdField = value;
        }
    }

    /// <remarks/>
    public string friendlyName {
        get {
            return this.friendlyNameField;
        }
        set {
            this.friendlyNameField = value;
        }
    }

    /// <remarks/>
    public string cardholderName {
        get {
            return this.cardholderNameField;
        }
        set {
            this.cardholderNameField = value;
        }
    }

    /// <remarks/>
    public string maskedPAN {
        get {
            return this.maskedPANField;
        }
        set {
            this.maskedPANField = value;
        }
    }

    /// <remarks/>
    public string startDate {
        get {
            return this.startDateField;
        }
        set {
            this.startDateField = value;
        }
    }

    /// <remarks/>
    public string expiryDate {
        get {
            return this.expiryDateField;
        }
        set {
            this.expiryDateField = value;
        }
    }

    /// <remarks/>
    public string issueNo {
        get {
            return this.issueNoField;
        }
        set {
            this.issueNoField = value;
        }
    }

    /// <remarks/>
    public cardType cardType {
        get {
            return this.cardTypeField;
        }
        set {
            this.cardTypeField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool cardTypeSpecified {
        get {
            return this.cardTypeFieldSpecified;
        }
        set {
            this.cardTypeFieldSpecified = value;
        }
    }
}

2 个答案:

答案 0 :(得分:3)

由于它是作为分部类生成的,因此您可以创建一个单独的部分类(不是自动生成的),在该部分类中重写ToString()。 e.g。

public partial class cardOnFileResponse 
{
    public override string ToString()
    {
        return String.Format("Response: {0} {1}", this.friendlyNameField, this.cardholderNameField);
    }
}

答案 1 :(得分:1)

它已被声明为部分类,因此在单独的文件中定义ToString():

public partial class cardOnFileResponse {
    public new string ToString()
    {
        string retn="";
        retn += ....
        return retn;
    }
}

当然,如果生成的代码删除或更改了您在ToString()实现中使用的字段,则可能需要更新它。