WinForm转Console Message=未能从程序集“, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null”中加载类型“没有 RVA
1.异常截图
2.源代码及运行结果
3.相关异常及解决办法
1.异常截图
异常具体信息如下:
未处理System.TypeLoadException
HResult=-2146233054
Message=未能从程序集“LockTestV2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null”中加载类型“LockTestV2.aaa”,因为方法“AllocConsole”没有实现(没有 RVA)。
Source=LockTestV2
TypeName=LockTestV2.aaa
StackTrace:
在 LockTestV2.Form1.button4_Click(Object sender, EventArgs e)
在 System.Windows.Forms.Control.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
在 System.Windows.Forms.Button.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.Run(Form mainForm)
在 LockTestV2.Program.Main() 位置 d:\TEST\Program.cs:行号 18
在 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()
InnerException:
2.源代码及运行结果
2.1源代码如下:
/// <summary>
/// 启动控制台
/// </summary>
/// <returns></returns>
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
public static extern Boolean AllocConsole();
/// <summary>
/// 释放控制台
/// </summary>
/// <returns></returns>
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
public static extern Boolean FreeConsole();
private void button4_Click(object sender, EventArgs e)
{
AllocConsole();
Console.WriteLine(DateTime.Now.ToString("yyyy/mm/dd HH:MM:ss ")+"-------------------------");
Console.WriteLine(DateTime.Now.ToString("yyyy/mm/dd HH:MM:ss ") + "测试,第一条消息");
Console.WriteLine(DateTime.Now.ToString("yyyy/mm/dd HH:MM:ss ") + "测试,第二条消息");
Console.WriteLine(DateTime.Now.ToString("yyyy/mm/dd HH:MM:ss ") + "测试,第三条消息");
Console.WriteLine(DateTime.Now.ToString("yyyy/mm/dd HH:MM:ss ") + "-------------------------");
Console.ReadLine();
FreeConsole();
}
2.2运行结果如下:
3.相关异常及解决办法
如以上修改过还是报同样的错,
解决步骤:
1.检查源代码 引用
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
public static extern Boolean AllocConsole();
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
public static extern Boolean FreeConsole();
2.查看生成的路径环境,应当改为如下 步骤:解决方案-> 属性(右键)->生成->目标平台->x86
(如果旁边,“首选32位置(G)” 可以选择的话也可选中)
备注:
1如反过来,Console转WinForm显示,则可看https://blog.****.net/shengmingzaiyuxuexi/article/details/73784152 文章,
2.或者直接转成控制台应用程序:解决方案-> 属性(右键)->输出类型->控制台应用程序
如有其他疑问,请留言
如有疑问,请留言
如有错误,请指正
与君共勉,望共同进步!
谢谢老板们阅读此文章
点个赞 或则关注就是对我最大的肯定,