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