我想強烈鍵入一個對象屬性已被定義爲接口中的接口。這裏是我的樣品如何強制鍵入已定義爲接口的屬性?
// interfaces
public interface IMyInterfaceA
{
string A { get; set; }
IMyInterfaceB B { get; set; }
}
public interface IMyInterfaceB
{
string B { get; set; }
}
// POCOs
public class pocoOneB : IMyInterfaceB
{
public B { get; set; }
public C { get; set; } // extending the poco with a non-interfaced property
}
public class pocoOneA : IMyInterfaceA
{
string A { get; set; }
pocoOneB B { get; set; } // fails, can I strongly type an interface??
}
public class pocoTwoB : IMyInterfaceB
{
public B { get; set; }
public D { get; set; } // extending the poco with a non-interfaced property
}
public class pocoTwoA : IMyInterfaceA
{
string A { get; set; }
pocoTwoB B { get; set; } // fails, can I strongly type an interface??
}
問題是我不能做
pocoOneB B { get; set; } // fails, can I strongly type an interface??
或
pocoTwoB B { get; set; } // fails, can I strongly type an interface??
即使他們是接口的實現,編譯器說,我沒有在poco上正確實現IMyInterfaceA。我理解錯誤,但是我想知道是否有強制鍵入具有接口的屬性的方法?
解決此問題的一種方法是不具有接口IMyInterfaceA定義接口IMyInterfaceB屬性的所有和範圍在poco的,但我試圖執行該屬性是使用接口實現的。
我需要強烈鍵入poco屬性的主要原因是因爲我使用JSON來消滅線。
感謝您的任何指導。
你得到的錯誤信息是什麼? –
不知道這是否會有所幫助,但如果在類定義中右鍵單擊接口,則可以選擇實現接口明確性。這將接口的屬性和方法定義爲interface.property,可能值得一看 – WraithNath