Context
Here's a very specific problem for ya. To sum things up, I have a class which queues delegates to run later. I know each method will only have one argument, so I stores these in a list of Action(Object). I can't use generics because different methods can have parameters of different types, so I use Object instead.
When it's time for the delegate to execute, the user passes an argument which will then be passed to the delegate method. The problem is in type checking: I wan't to throw a specific exception if the type of the parameter they supplied doesn't match the type that the delegate is expecting.
For example, if pass this method into my class:
Sub Test(SomeNumber As Integer)
and then try to run:
MyClass.ExecuteDelegate("DelegateArgument")
I want to be able to throw a certain exception saying that they tried to use a String as an Integer and include some custom information. The trouble is I can't find a way of doing this.
Problem
Since I'm storing the delegates as Action(Object) I have no idea what the actual type of the parameter is. I have not found any way to find this info, so I cannot compare the type of the parameter to the type of the user-supplied argument. When I use Action(Object).Method.GetParameters
it only returns Object
Alternatively I've tried using a Try-Catch block to check for an InvalidCastException when I try to call Action(Object).Invoke()
but that also catches any InvalidCastException within the delegate method, which i don't want.
Is there any way to achieve what I'm trying to do?