3
Answers

invoking method declaration without reflection

steve

steve

14y
3.5k
1

Hi,
I have a base class (order) with a set of sub classes (productorder, specialorder, partsorder etc).
Only Some of these sub classes implement a particular interface (ITrackingCustomer) which has a single method declaration (object getcustdetails()).
As part of my solution all of my orders are processed in a central place, i.e. any crud methods pass through a central layer. Within this central layer I want to do the following:
If order is of type ITrackingCustomer
Then invoke method getcustdetails()
I have this working using the following code:
if (typeof(ITrackingCustomer).IsAssignableFrom(Order.GetType()))
                    {
                        MethodInfo theMethod = Order.GetType().GetMethod("getcustdetails");
                        object y = theMethod.Invoke(Order, null);
                    }

I am happy with the first part using isassignablefrom but would like to use a less performance intensive method for the second part (i.e. the reflection  using invoke). My question is:
Is there a more efficient way of doing this as I have read that using the invoke command is costly.
Thanks
Steve

Answers (3)