2014-09-12 153 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