I have a method
public Action FirstAction(object data)
{
}
I have to pass two variables to this class which are different types for example int
and string
or any other type of objects.
I read that I can do it by
FirstAction( new{int a, string b} )
My question is how to access the two separated variables within FirstAction
method?
Note: I cannot change the object
parameters to object[]
or params object[]
; it needs to be done with the current signature.
If you cannot change the function signature, then you can always use dynamic:
public Action FirstAction(object data) {
dynamic dataAsDynamic = data;
int a = dataAsDynamic.a;
string b = dataAsDynamic.b;
}
Note that this is very brittle as the minute someone changes the names of these fields the code will break. I don't actually recommend doing this, but if you have no control over the function signature then you're kinda stuck.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With