0
我已經使用生成在Visual Studio 2010無法類型強制轉換爲的LinQ實體基類
[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Address")]
public partial class Address : INotifyPropertyChanging,INotifyPropertyChanged
{
//all the generated methods are here
}
然後生成的LINQ to SQL實體類創建接口ITask
public Interface ITask
{
//some interface methods go here
}
最後創建我班
class myclass : Address, ITask
{
//implement interface methods
}
別處在我的單元測試 我創建一個MyClass的OBJ ect發生以下情況:
myclass temp=new myclass();
Address adr=(Address)temp;
adr.GetType();//returns myclass when I watch the var in debugger
DataContext dc=new DataContext();//the linq data context
dc.Addresses.InsertOnSubmit(adr);//Object reference not set
//to an instance of an object NullReferenceException
ITask task=(ITask)temp;
temp.Callinterfacemethods();//works fine, iam able to call interface methods
我是一名C++程序員,這是我的第一個C#任務的一部分。我究竟做錯了什麼?以上在C++中完全有效。
謝謝。
大衛
我想你會在這裏找到答案:http://stackoverflow.com/questions/499436/linq-insertonsubmit-nullreferenceexception](http://stackoverflow.com/questions/499436/ linq-insertonsubmit-nullreferenceexception) – 2011-12-20 06:39:58
嘗試使用地址adr = temp作爲地址; if(adr!= null){doSomething(); }。請看下面這個:http://msdn.microsoft.com/en-us/library/cscsdfbt%28v=vs.71%29.aspx – Lucian 2011-12-20 06:41:40
@Malcome O'Hare - 該鏈接顯示我如何克服這個問題,即擴展部分類。它沒有回答爲什麼繼承類不起作用(正如我上面的問題所解釋的)。 – david 2011-12-20 10:11:42