![](/img/trans.png)
[英]System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation
[英]How to fix “System.Reflection.TargetInvocationException has been thrown”
我正在按照制作discord bot的教程,在遵循設置機器人的基礎知識之后,我一直遇到這個異常:
拋出了System.Reflection.TargetInvocationException。 調用的目標拋出了異常。
這是我的代碼:
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表示這段代碼發生異常:
discord.ExecuteAndWait(async () =>
{
await discord.Connect("BOT_TOKEN", TokenType.Bot);
});
我不知道如何解決這個問題,所以任何幫助都會非常感激。
(由於這是我在這里發表的第一篇文章,對於如何改進我的帖子的任何建議也表示贊賞。)
在我的情況下,它是由一些內部代碼中的一些異常引起的,並且異常是通過堆棧拋出的,當到達表面並顯示異常窗口時,異常消息已被丟失,因此它顯示無用的消息System.Reflection.TargetInvocationException has been thrown.
解決方案是打開異常設置窗口,並檢查所有項目,以便在發生任何類型的異常時它會中斷,因此您可以在最初的位置捕獲異常,以便可以理解錯誤原因。
我的想法來自HERE 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.