System.Reflection.TargetInvocationException: Exception has been thrown by the target of…
- User
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NotImplementedException: The method or operation is not implemented. at TestModule.TestItem.Buy(ILKPlayer player, TestConfig config) in C:\Users\zoro8\RiderProjects\LK\TestModule\TestModule.cs:line 29 at LKCoreApi.LKItemPrefab
1.Buy(ILKPlayer player, ItemConfig config) in C:\Users\zoro8\RiderProjects\LK\LKCoreApi\LKItemPrefab.cs:line 7 at LK.LKPlugin.OnBuyItem(LkPlayer player, LKItem registeredItem) in C:\Users\zoro8\RiderProjects\LK\LK\LKPlugin.cs:line 80 at LK.LKPlugin.<>c__DisplayClass20_0.<OnSelectProductItem>b__0(CCSPlayerController _, ChatMenuOption _) in C:\Users\zoro8\RiderProjects\LK\LK\LKPlugin.cs:line 74 at CounterStrikeSharp.API.Modules.Menu.BaseMenuInstance.OnKeyPress(CCSPlayerController player, Int32 key) in E:\github\CounterStrikeSharp\managed\CounterStrikeSharp.API\Modules\Menu\BaseMenu.cs:line 132 at CounterStrikeSharp.API.Modules.Menu.MenuManager.OnKeyPress(CCSPlayerController player, Int32 key) in E:\github\CounterStrikeSharp\managed\CounterStrikeSharp.API\Modules\Menu\MenuManager.cs:line 72 at CounterStrikeSharp.API.Core.Application.<>c.<Start>b__16_0(CCSPlayerController player, CommandInfo info) in E:\github\CounterStrikeSharp\managed\CounterStrikeSharp.API\Core\Application.cs:line 101 at CounterStrikeSharp.API.Core.Commands.CommandManager.HandleCommandInternal(Int32 playerSlot, IntPtr commandInfo) in E:\github\CounterStrikeSharp\managed\CounterStrikeSharp.API\Core\Commands\CommandManager.cs:line 159 at InvokeStub_Action2.Invoke(Object, Span`1) at System.Reflection.MethodBaseInvoker.InvokeWithFewArgs(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) --- End of inner exception stack trace --- at System.Reflection.MethodBaseInvoker.InvokeWithFewArgs(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.Delegate.DynamicInvokeImpl(Object[] args) at CounterStrikeSharp.API.Core.FunctionReference.b__18_0(fxScriptContext* context) in E:\github\CounterStrikeSharp\managed\CounterStrikeSharp.API\Core\FunctionReference.cs:line 100В чем ошибка?
public class TestItem : LKItemPrefab { public override bool Buy(ILKPlayer player, TestConfig config) { Server.PrintToChatAll(config.Duration.ToString()); return true; }
public override string Name => "123";}
public abstract class LKItemPrefab : ILKItemPrefab where T : ItemConfig { public bool Buy(ILKPlayer player, ItemConfig config) => Buy(player, (T)config); public abstract bool Buy(ILKPlayer player, T config); public abstract string Name { get; } }
public interface ILKItemPrefab { bool Buy(ILKPlayer player, ItemConfig config); string Name { get; } }
- Assistant
The exception you're encountering,
System.Reflection.TargetInvocationException, is a wrapper exception that indicates an error occurred within a method that was invoked through reflection, which in your case appears to be theBuymethod. The crucial part of the stack trace is:---> System.NotImplementedException: The method or operation is not implemented. at TestModule.TestItem.Buy(ILKPlayer player, TestConfig config) in C:\Users\zoro8\RiderProjects\LK\TestModule\TestModule.cs:line 29This inner exception,
System.NotImplementedException, indicates that theBuymethod inTestModule.TestItemthat takes parameters ofILKPlayerandTestConfigis being invoked but hasn't been implemented. However, based on the code snippet you've provided, we can see thatBuymethod is indeed implemented inTestItem.If you're seeing this error even though it looks like the method is implemented, there might be a few things to check or try:
-
Correct Implementation Checked: First, ensure that the implementation of
TestItem.Buythat's throwing the exception is the one you believe it to be. There might be multiple versions or overrides of the method, or you might be looking at an outdated version of the code. -
Interface and Abstract Class Contracts: Ensure that your
TestItemclass andLKItemPrefab<T>abstract class fulfill all contracts defined by the interfaces and base classes they inherit from or implement. This includes not just method signatures, but also return types and access modifiers. -
Casting Issues: Notice how your non-abstract
Buymethod inLKItemPrefab<T>does a cast:Buy(player, (T)config);. Ensure that at runtime,configis indeed of typeT, and that this cast is valid. A casting failure would not directly cause aNotImplementedException, but it's a common source of bugs in such polymorphic designs, especially if there's any possibility thatconfigmight not be of typeT. -
Recompile and Clean: Sometimes, especially when using IDEs like Rider or Visual Studio, the build process might not properly update all binaries, especially after significant changes in the project structure or after refactoring. Recompile your solution and consider doing a "clean" followed by a "rebuild" to ensure all binaries are up-to-date.
-
External Libraries or Dependencies: If
TestItemorLKItemPrefab<T>rely on any external libraries or project references, ensure those dependencies are correctly resolved and up-to-date. A missing or incorrect version of a library can sometimes cause unexpected behavior. -
Reflection Details: The stack trace mentions
System.Reflection.MethodBaseInvoker. Thoroughly check how reflection is used in your project to invokeBuy. Sometimes, the way a method is invoked via reflection can lead to issues not immediately apparent from simply reading the code. -
Debugging: If possible, use a debugger to step into the method invocation. This can provide insights into why the
NotImplementedExceptionis being thrown, especially the state of the program at the moment of the call.
Ensure all parts of your project are correctly updated and consider these checks. If the problem persists, you may need to investigate more specific details of how your project uses reflection and manages its dependencies.
-