2009-01-29 71 views
4

我在兩個類中做了完全相同的事情,在一個編譯器允許它很好,但另一個給我一個錯誤。爲什麼是雙重標準?有使用同樣的模式15類,但只有一個拒絕編譯,說了以下錯誤:C#雙重標準?

'AWWAInvoicingXML.AwwaTransmissionInfo' does not implement interface member 'AWWAInvoicingXML.IXmlSerializable.fromXML(System.Xml.XmlDocumentFragment)'. 'AWWAInvoicingXML.AwwaTransmissionInfo.fromXML(System.Xml.XmlDocumentFragment)' is either static, not public, or has the wrong return type.

這裏是我的源代碼,如果我註釋掉AwwaTransmissionInfo類,文件的剩餘部分只是編譯好,所以我知道這不是編譯器在第一個錯誤之後死亡的那個。我知道,我知道,這裏有內置的東西,我想在這裏做什麼,但只是假設我真的知道我在做什麼,並跳過內置序列化器出於某種原因:)

public interface IXmlSerializable { 
    //if this interface is implemented, the object can be serialized to XML 
    string toXML(); 
    IXmlSerializable fromXML(XmlDocumentFragment inXml); 
} 

public class AwwaTransmissionInfo : IXmlSerializable { 

    public DateTime DateTime = DateTime.Now; 
    public int ItemCount; 

    public string toXML() { 
     throw new Exception("The method or operation is not implemented."); 
    } 

    public AwwaTransmissionInfo fromXML(XmlDocumentFragment inXml) { 
     throw new Exception("The method or operation is not implemented."); 
    } 

} 

public class CEmail { 
    public string Email = ""; 

    public string toXML() { 
     throw new System.Exception("The method or operation is not implemented."); 
    } 

    public CEmail fromXML(XmlDocumentFragment inXml) { 
     throw new System.Exception("The method or operation is not implemented."); 
    } 
} 
+0

CEmail未標記爲實施IXmlSerializable。那是故意的嗎? – Rob 2009-01-29 22:55:05

+0

Bang你明白了...... OMG我盯着這很久沒有意識到這一點!作爲回答發佈,所以我可以給你的觀點:) – Jasmine 2009-01-29 22:56:53

+0

沒有看到森林樹木的經典案例。 =) – 2009-01-29 23:03:57

回答

1

的原因CEmail類是編譯罰款,是因爲它沒有標記爲實現該接口。當我這樣做時,我得到了和其他班級一樣的錯誤。以下是它是如何...我很愚蠢,因爲沒有注意到這一點。我們現在處於很大的壓力之下,我無法相信自己在發佈問題之前一個小時就盯着這段代碼,而且事情太簡單了......它總是我猜的:)

public interface IXmlSerializable { 
    //if this interface is implemented, the object can be serialized to XML 
    string toXML(); 
    IXmlSerializable fromXML(XmlDocumentFragment inXml); 
} 

public class AwwaTransmissionInfo : IXmlSerializable { 

    public DateTime DateTime = DateTime.Now; 
    public int ItemCount; 

    public string toXML() { 
     throw new Exception("The method or operation is not implemented."); 
    } 

    public IXmlSerializable fromXML(XmlDocumentFragment inXml) { 
     throw new Exception("The method or operation is not implemented."); 
    } 

} 

public class CEmail : **IXmlSerializable** { 
    public string Email = ""; 

    public string toXML() { 
     throw new System.Exception("The method or operation is not implemented."); 
    } 

    public IXmlSerializable fromXML(XmlDocumentFragment inXml) { 
     throw new System.Exception("The method or operation is not implemented."); 
    } 
} 
1

AwwaTransmissionInfo.fromXml需要返回IXmlSerializable類型,而不是AwwaTransmissionInfo

0

您的CEmail類根本沒有實現IXmlSerializable接口。實現它並將CEmail和AWwaTransmissionInfo的返回類型從XML更改爲IXmlSerializable。

public IXmlSerializable fromXML(XmlDocumentFragment inXml){ 
    throw new System.Exception("The method..."); 
} 
10

問題是方法簽名必須完全匹配接口。

最簡單的解決辦法是改變

public AwwaTransmissionInfo fromXML(XmlDocumentFragment inXml) { 

public IXmlSerializable fromXML(XmlDocumentFragment inXml) { 

如果你不滿意,你可以明確地實現接口。補充一點:

public IXmlSerializable IXmlSerializable.fromXML(XmlDocumentFragment inXml) { 
     return this.fromXML(inXml); 
    } 

然後,你將有fromXML(),一個兩個定義時使用被稱爲是類的實例,以及一個用於當通過接口調用。

0

派生的AwwaTransmissionInfo類的「fromXml」方法返回AwwaTransmissionInfo類型,其中接口定義該方法應返回IXmlSerializable類型。由於AwwaTransmissionInfo派生自IXmlSerializable,所以你會認爲,很酷,可行,但它不在界面上。我認爲這將是不一致的(反對或合作,我永遠不會記得哪一個)?

如果你想用泛型重構這個,那麼你可以使用一個約束來讓你解決這個問題。

2

IXmlSerializable已經存在於.NET框架中。我建議你在自己脫身並重新發明車輪之前執行該步驟。

查看msdn 1

至於你的代碼將不能編譯的原因:

fromXml需要返回IXmlSerializable的方法。

public IXmlSerializable fromXML(XmlDocumentFragment inXml) 
{ 
    throw new Exception("The method or operation is not implemented."); 
} 

如果您想返回其他內容,請考慮使用通用接口。

IE。

public interface IXmlSerializable<T> 
{ 
    //if this interface is implemented, the object can be serialized to XML 
    string toXML(); 
    T fromXML(XmlDocumentFragment inXml); 
} 

public class AwwaTransmissionInfo : IXmlSerializable<AwwaTransmissionInfo> 
{ 

    public DateTime DateTime = DateTime.Now; 
    public int ItemCount; 

    public string toXML() 
    { 
     throw new Exception("The method or operation is not implemented."); 
    } 

    public AwwaTransmissionInfo fromXML(XmlDocumentFragment inXml) 
    { 
     throw new Exception("The method or operation is not implemented."); 
    } 
}