2010-09-27 87 views
0

我使用實體框架和我已經創建了租賃交易的接口:是否可以在實體對象接口上實現接口屬性?

public interface ILeaseTransaction 
{ 
    int ID { get; } 
    DateTime Date { get; } 
    decimal Amount { get; } 
} 

然後實施對實體對象我創建了一個空的部分類接口:

public partial class Type1LeaseTransaction : ILeaseTransaction 
{ 

} 

該作品很好,但是交易也可以有零個或一個也是實體對象的空洞。我試圖實現的空隙如下:

public interface ILeaseTransactionVoid 
{ 
    int TransactionID { get; } 
    DateTime Date { get; } 
    int TypeID { get; } 
} 

和空部分類...:

public partial class Type1LeaseTransactionVoid : ILeaseTransactionVoid 
{ 

} 

我遇到的問題是,當我嘗試將ILeaseTransactionVoid添加爲屬性在LeaseTransaction接口:

public interface ILeaseTransaction 
{ 
    int ID { get; } 
    DateTime Date { get; } 
    decimal Amount { get; } 
    ILeaseTransactionVoid Void { get; } // This throws an error 
} 

當我嘗試和建設,我得到以下錯誤:

'DomainModel.Models.Type1LeaseTransaction' does not implement interface member 'DomainModel.Abstract.ILeaseTransaction.Void'. 'DomainModel.Models.Type1LeaseTransaction.Void' cannot implement 'DomainModel.Abstract.ILeaseTransaction.Void' because it does not have the matching return type of 'DomainModel.Abstract.ILeaseTransactionVoid'.

我想錯誤是有道理的,因爲返回類型不是接口本身,即使它實現了接口。我對這一切都很陌生,所以我完全失去了這一點。

有沒有什麼辦法讓我在ILeaseTransaction上實現嵌套接口屬性ILeaseTransactionVoid

謝謝!

回答

1

因此,這似乎不是很流行,但我還是設法找到答案,所以希望有人認爲這有用的天:

解決的辦法是保持接口的簽名與上面相同:

public interface ILeaseTransaction 
{ 
    int ID { get; } 
    DateTime Date { get; } 
    decimal Amount { get; } 
    ILeaseTransactionVoid Void { get; } // This throws an error 
} 

,並更改實體類通過如下添加接口特定的屬性實現接口的方式:

public partial class Type1LeaseTransactionVoid : ILeaseTransactionVoid 
{ 
    ILeaseTransactionVoid ILeaseTransaction.Void 
    { 
     get { return (ILeaseTransactionVoid)Void; } 
    } 
} 
+0

我也正是這個問題,所以我是音響很開心nd這個解決方案....但是我得到2錯誤與我的執行: 1.顯式執行(我相當於你的Void屬性)錯誤: – Laurence 2014-02-26 12:37:58

+0

忽略以前的評論 - 我想出了我的問題。非常感謝發佈答案 - 它在這裏做了很大的改變! – Laurence 2014-02-26 12:45:14