C
C#12mo ago
Thomoski

✅ Getting multiple return values from a method called using InvokeMember

So I'm using InvokeMember to call methods on a variety of different objects, all of which use a tuple return type to return multiple values. The issue I'm having is when one of these methods does return the multiple values, I'm getting an InvalidCastException. This is the implementation I've got for the InvokeMember - is it possible to use Tuple return types with InvokeMember, or am I just doing it wrong here?
No description
21 Replies
Thomoski
ThomoskiOP12mo ago
Ignore my awful class naming
cap5lut
cap5lut12mo ago
whats the exact cast error? it should give the exact return type
Thomoski
ThomoskiOP12mo ago
No description
cap5lut
cap5lut12mo ago
thats the difference, ValueTuple<T1, T2> (a struct), vs Tuple<T1, T2> (a class)
Thomoski
ThomoskiOP12mo ago
So do I need to define a specific tuple class first before using it to cast the returned object from the method to returnValues? Or is it that the method is returning the struct?
cap5lut
cap5lut12mo ago
no, instead of casting to Tuple, u should cast to ValueTuple
Thomoski
ThomoskiOP12mo ago
Ah, okay Is that due to my implementation, or is this just how this works with InvokeMethod, It should be a struct?
cap5lut
cap5lut12mo ago
if the method is something like public (int, int) Example(); the return type is ValueTuple<int, int> u can not simply cast a ValueTuple to a Tuple
MODiX
MODiX12mo ago
cap5lut
REPL Result: Failure
ValueTuple<int, int> vt = (1, 2);
Tuple<int, int> t = (Tuple<int, int>)vt;
ValueTuple<int, int> vt = (1, 2);
Tuple<int, int> t = (Tuple<int, int>)vt;
Exception: CompilationErrorException
- Cannot convert type '(int, int)' to 'System.Tuple<int, int>'
- Cannot convert type '(int, int)' to 'System.Tuple<int, int>'
Compile: 391.152ms | Execution: 0.000ms | React with ❌ to remove this embed.
cap5lut
cap5lut12mo ago
it highlights this part: (Tuple<int, int>)vt
Thomoski
ThomoskiOP12mo ago
Ahh, okay
Thomoski
ThomoskiOP12mo ago
So from what I understand, I should still be able to access the values in a ValueTuple the same way though, right? After changing to a ValueTuple type, I now cant access them using the returnTypes.Item1 way
No description
Thomoski
ThomoskiOP12mo ago
Ah, don't worry, I figured it out, thank you
cap5lut
cap5lut12mo ago
whats the error there? kk
Thomoski
ThomoskiOP12mo ago
Was giving me this
No description
Thomoski
ThomoskiOP12mo ago
But apparently it should be returnTypes.Value.Item1
cap5lut
cap5lut12mo ago
ah yeah, Nullable<T> is weird 😂
Thomoski
ThomoskiOP12mo ago
Thanks for your help!
cap5lut
cap5lut12mo ago
glad i could o7 if this answered ur questions, dont forget to $close the thread
MODiX
MODiX12mo ago
Use the /close command to mark a forum thread as answered
Thomoski
ThomoskiOP12mo ago
Shall do
Want results from more Discord servers?
Add your server