2016-03-02 29 views
2

我有一個數據網格視圖綁定到用戶類的列表。缺少方法例外 - 找出所需的方法

當我創建一個新行時,我得到一個缺少的方法異常「未找到User.cs上的構造函數」。問題是我有一個默認的構造函數,所以我想知道是否有辦法找出參數是什麼,以便我可以實現類構造函數。

這裏是類和構造

public string Username { get; set; } 
    public byte[] HashedPassword; 
    public byte[] Salt ; 
    public string sSalt { get { return Encoding.ASCII.GetString(Salt); } set; } 
    public string sPass { get { return Encoding.ASCII.GetString(HashedPassword); } set; } 
    public bool Admin { get; set; } 
    public List<AnswerClass> answers { get; set; } 
    public Tuple<int, int> sessionScore; 

    public User(string UsernameArg = "", byte[] PasswordArg = null, byte[] SaltArg = null, bool AdminArg = false) 
    { 
     sessionScore = new Tuple<int, int>(0, 0); 
     Username = UsernameArg; 
     HashedPassword = PasswordArg; 
     Salt = SaltArg; 
     Admin = AdminArg; 
     answers = new List<AnswerClass>(); 
    } 

錯誤:

System.MissingMethodException was unhandled 
HResult=-2146233069 
Message=Constructor on type 'QuizProject_SourceControl_.User' not found. 
Source=mscorlib 
StackTrace: 
    at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark& stackMark) 
    at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes) 
    at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture) 
    at System.SecurityUtils.SecureCreateInstance(Type type, Object[] args, Boolean allowNonPublic) 
    at System.ComponentModel.BindingList`1.AddNewCore() 
    at System.ComponentModel.BindingList`1.System.ComponentModel.IBindingList.AddNew() 
    at System.Windows.Forms.CurrencyManager.AddNew() 
    at System.Windows.Forms.DataGridView.DataGridViewDataConnection.AddNew() 
    at System.Windows.Forms.DataGridView.DataGridViewDataConnection.OnNewRowNeeded() 
    at System.Windows.Forms.DataGridView.OnRowEnter(DataGridViewCell& dataGridViewCell, Int32 columnIndex, Int32 rowIndex, Boolean canCreateNewRow, Boolean validationFailureOccurred) 
    at System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32 columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean validateCurrentCell, Boolean throughMouseClick) 
    at System.Windows.Forms.DataGridView.OnCellMouseDown(HitTestInfo hti, Boolean isShiftDown, Boolean isControlDown) 
    at System.Windows.Forms.DataGridView.OnCellMouseDown(DataGridViewCellMouseEventArgs e) 
    at System.Windows.Forms.DataGridView.OnMouseDown(MouseEventArgs e) 
    at System.Windows.Forms.Control.WmMouseDown(Message& m, MouseButtons button, Int32 clicks) 
    at System.Windows.Forms.Control.WndProc(Message& m) 
    at System.Windows.Forms.DataGridView.WndProc(Message& m) 
    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 
    at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 
    at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 
    at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) 
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 
    at System.Windows.Forms.Application.Run(Form mainForm) 
    at QuizProject_SourceControl_.Program.Main() in d:\Programming\Repos\QuizProject(SourceControl)\QuizProject(SourceControl)\Program.cs:line 19 
    at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
    at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
    at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
    at System.Threading.ThreadHelper.ThreadStart() 
InnerException: 
+2

Just becau se構造函數具有所有值的默認值,不會使其成爲默認構造函數 – stuartd

+0

@stuartd但是,它是在沒有參數傳遞時將被調用的構造函數,據我所知 – Cjen1

+0

[在C#中](https:// msdn .microsoft.com/en-us/library/aa645608(v = vs.71).aspx)默認的構造函數必須是'無參數' – stuartd

回答

5

您沒有默認構造函數。默認構造函數是不帶任何參數的構造函數:

public User() 
{ 
    ... 
} 

你有什麼是構造函數,所有參數都有默認值。這是一個很大的區別,因此,參數的默認值在.NET中工作的方式如下:在.NET中,編譯器將默認值複製到您調用構造函數(或任何其他方法)的每個位置。所以基本上,參數的默認值只是語法糖。

例子:
假設你有一個這樣的方法:在你的代碼

public void Method(int para = 12) 
{ 
} 

現在,某個地方,你怎麼稱呼它是這樣的:

Method(); 

編譯器將改變這和實際得到編譯的代碼如下所示:

public void Method(int para) 
{ 
} 

Method(12); 
+0

啊謝謝,這是有道理的,但它似乎有點違反直覺 – Cjen1