0
我有一個接口IDetail
和一個Detail
類,它只能執行IDetail
。在Detail
類中,實現的屬性ObjectId
設置爲一個值。在界面和類中,該屬性都以簡單的get
/set
的形式實現。但是,通過界面訪問時,該物業將返回null
。 Detail對象實際上是作爲通用參數傳遞的,其中T
是IDetail
。這是.NET中的某種錯誤嗎?我會在哪裏開始嘗試並弄清楚這一點?實現的接口屬性返回null?
這就是眼前的窗口顯示:
(detail as IDetail).ObjectId
null
(detail as Detail).ObjectId
"..."
下面的代碼:
public interface IDetail
{
string ObjectId { get; set; }
}
public class Detail : IDetail
{
public string ObjectId { get; set; }
}
public class SomeClass
{
public void SomeMethod()
{
SomeOtherClass.SomeOtherMethod(new Detail { ObjectId = "..." });
}
}
public class SomeOtherClass<T> where T : IDetail
{
public static void SomeOtherMethod<T>(T detail)
{
DoSomething(detail.ObjectId); // ObjectId is null here???
}
}
你需要一個橡皮鴨,http://en.wikipedia.org/wiki/Rubber_duck_debugging – asawyer 2014-09-12 15:34:11