randomgui
randomgui

Reputation: 61

System.Reflection.TargetException: Object does not match target type

I have two dlls right next to each other in a directory. When I load an assembly invoke a method from DLL A onto DLL B then invoke a method from DLL B back onto DLL A, but it fails at the last step and throws the following error

 System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Reflection.TargetException: Object does not match target type.
    at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)
    at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
    at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
    at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
    at ServerSDK.BootStrapper.GetPlayerSteamID(UInt16 playerid)

And here is the code.

public void Init()
{            
    string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
    string file = Path.Combine(assemblyFolder, "NetworkSystem.dll");
    Assembly assembly = Assembly.LoadFile(file);
    type = assembly.GetType("ScriptingAPI.ScriptEngine");

    getSteamMethod = type.GetMethod("GetSteamID", new Type[] { typeof(ushort) });
    Console.WriteLine("bootstrap module executed");       
}

public static string GetPlayerSteamID(ushort playerid)
{
    var steamid = getSteamMethod.Invoke((object)type, new object[1] { playerid });
    return steamid.ToString();
}

and here is the method that from DLL A I am attempting to invoke

public string GetSteamID(ushort playerID)
{
    Console.WriteLine(playerID.GetType().Name);
    return connectedPlayers[playerID].steamID;
}

Also I feel like I need something like Activator.GetInstance instead of CreateInstance as the DLL is already running and if i was to use CreateInstance it would in turn throw null reference exceptions.

Upvotes: 2

Views: 3545

Answers (1)

randomgui
randomgui

Reputation: 61

Alright so the solution for me in this case was to pass the object that invoked Init along as a parameter with it. Thank you for the replies though!

Upvotes: 1

Related Questions