2014-09-12 101 views
0

我有一個接口IDetail和一個Detail類,它只能執行IDetail。在Detail類中,實現的屬性ObjectId設置爲一個值。在界面和類中,該屬性都以簡單的get/set的形式實現。但是,通過界面訪問時,該物業將返回null。 Detail對象實際上是作爲通用參數傳遞的,其中TIDetail。這是.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??? 
    } 
} 

回答

1

我想通了,因爲我是打字的問題。我覺得很愚蠢!

原來在匆忙我其實實現我的接口類:

public class IDetail 
{ 
    public string ObjectId { get; set; } 
} 

這將做到這一點!

+4

你需要一個橡皮鴨,http://en.wikipedia.org/wiki/Rubber_duck_debugging – asawyer 2014-09-12 15:34:11