简体   繁体   中英

Missing Method Exception - find out required method

I have a data grid view that is bound to a list of a user class.

When I create a new row I get a missing method exception "The constructor on User.cs not found". The issue is that I have a default constructor already so I was wondering if there is a way to find out what the parameters are so that I can implement the class constructor.

Here is the class and constructor

    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>();
    }

the error:

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: 

You don't have a default constructor. A default constructor is a constructor without any parameters:

public User()
{
    ...
}

What you have is a constructor where all parameters have default values. That is a big difference, and the reason therefore is the way that default values for parameters work in .NET:
In .NET, the compiler copies the default values to every location where you call that constructor (or any other method). So basically, default values for parameters are only syntactic sugar.

Example:
Assume you have a method like this:

public void Method(int para = 12)
{
}

Now, somewhere in your code, you call it like this:

Method();

The compiler will change this and the code that actually gets compiled will look like this:

public void Method(int para)
{
}

Method(12);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM