简体   繁体   English

如何修复“System.Reflection.TargetInvocationException已被抛出”

[英]How to fix “System.Reflection.TargetInvocationException has been thrown”

I'm following a tutorial for making a discord bot, and after following the basics for setting up a bot I keep running into this exception: 我正在按照制作discord bot的教程,在遵循设置机器人的基础知识之后,我一直遇到这个异常:

System.Reflection.TargetInvocationException has been thrown. 抛出了System.Reflection.TargetInvocationException。 Exception has been thrown by the target of an invocation. 调用的目标抛出了异常。

This is the code I have: 这是我的代码:

using Discord.Commands;
using Discord;
using System;
namespace TestBot
{
    public class MyBot
    {
        DiscordClient discord;

        public MyBot()
        {
            discord = new DiscordClient(x =>
            {
                x.LogLevel = LogSeverity.Info;
                x.LogHandler = Log;
            });
            discord.UsingCommands(x =>
            {
                x.PrefixChar = '!';
                x.AllowMentionPrefix = true;
            });
            var commands = discord.GetService<CommandService>();

            commands.CreateCommand("test")
                    .Do(async (e) =>
            {
                await e.Channel.SendMessage("response");
            });
             discord.ExecuteAndWait(async () =>
            {
                await discord.Connect("BOT_TOKEN", TokenType.Bot);
            });

        }

        private void Log(object sender, LogMessageEventArgs e)
        {
            Console.WriteLine(e.Message);
        }
    }
}

MonoDevelop says that the exception is happening with this piece of code: MonoDevelop表示这段代码发生异常:

discord.ExecuteAndWait(async () =>
            {
                await discord.Connect("BOT_TOKEN", TokenType.Bot);
            });

I don't know how to fix this, so any help would be greatly appreciated. 我不知道如何解决这个问题,所以任何帮助都会非常感激。

(As this is my first post here, any suggestions on how to improve my posts are also appreciated.) (由于这是我在这里发表的第一篇文章,对于如何改进我的帖子的任何建议也表示赞赏。)

The true cause for your exception is being hidden from you. 您的异常的真正原因是对您隐藏。 To fix this issue you will need to view the inner exception (and if required the inner exception of that exception and so on..). 要解决此问题,您需要查看内部异常(如果需要,还需要查看该异常的内部异常等等)。 This will allow you to see the root cause of the issue and allow you to workout how to fix it. 这将允许您查看问题的根本原因,并允许您锻炼如何解决它。

You can view the inner exception when your code breaks in debug mode; 当代码在调试模式中断时,您可以查看内部异常;

在此输入图像描述

Clicking view detail will allow you to see more information about your exception; 单击查看详细信息将允许您查看有关例外的更多信息;

在此输入图像描述

Once you get to the true cause of your exception I suggest you post a new question asking how to solve that specific issue (if you can't solve it yourself). 一旦你找到了你的异常的真正原因,我建议你发一个新问题,询问如何解决这个特定问题(如果你自己无法解决)。

In my case, it's caused by some exception in some inner code, and the exception was throwed up through the stack, when come to the surface and show exception window, the exception message has been losed so it shows a useless message System.Reflection.TargetInvocationException has been thrown. 在我的情况下,它是由一些内部代码中的一些异常引起的,并且异常是通过堆栈抛出的,当到达表面并显示异常窗口时,异常消息已被丢失,因此它显示无用的消息System.Reflection.TargetInvocationException has been thrown.

The solution is, open Exception Settings Window, and check all items so that it'll break when any type of Exception happens, so you can catch the exception at the most initial position that the error reason can be esaily undederstood. 解决方案是打开异常设置窗口,并检查所有项目,以便在发生任何类型的异常时它会中断,因此您可以在最初的位置捕获异常,以便可以理解错误原因。

在此输入图像描述

My idea comes from HERE . 我的想法来自HERE

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.Reflection.TargetInvocationException: &#39;调用的目标已抛出异常 - System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation System.Reflection.TargetInvocationException :调用的目标已抛出异常 - System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation System.Reflection.TargetInvocationException:调用的目标已引发异常 - System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation 使用 Task.Run 时如何修复“System.Reflection.TargetInvocationException: 'Exception has been throwed by the target of an invocation.'” - How to fix “System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation.'” when use Task.Run Xamarin Forms System.Reflection.TargetInvocationException: &#39;调用的目标已抛出异常。&#39; - Xamarin Forms System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation.' Xamarin.form-Masterdetailpage:System.Reflection.TargetInvocationException:调用的目标已引发异常 - Xamarin.form - Masterdetailpage : System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation Xamarin System.Reflection.TargetInvocationException:调用的目标已引发异常 - Xamarin System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation Xamarin 表单:System.Reflection.TargetInvocationException:调用的目标已抛出异常 - Xamarin Forms: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation 为什么我收到异常 System.Reflection.TargetInvocationException: &#39;Exception has been throwed by the target of an invocation.&#39;? - Why I'm getting exception System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation.'? System.Reflection.TargetInvocationException - System.Reflection.TargetInvocationException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM