gpt4 book ai didi

c# - 缺少方法异常 - 找出所需的方法

转载 作者:行者123 更新时间:2023-12-03 19:38:38 25 4
gpt4 key购买 nike

我有一个绑定(bind)到用户类列表的数据 GridView 。

当我创建新行时,出现缺少方法异常“未找到 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:

最佳答案

您没有默认构造函数。默认构造函数是不带任何参数的构造函数:

public User()
{
...
}

您拥有的是一个构造函数,其中所有参数都有默认值。这是一个很大的差异,原因是参数默认值在 .NET 中的工作方式:
在 .NET 中,编译器将默认值复制到调用该构造函数(或任何其他方法)的每个位置。所以基本上,参数的默认值只是语法糖。

示例:
假设您有这样的方法:

public void Method(int para = 12)
{
}

现在,在代码中的某个位置,您可以这样调用它:

Method();

编译器会改变这一点,实际编译的代码将如下所示:

public void Method(int para)
{
}

Method(12);

关于c# - 缺少方法异常 - 找出所需的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35747920/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com