
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
780
|
Chapter 13: Reflection
determination can be based on an assembly name, a type name, or a method name,
though the assembly name is not required if the method exists in the same assembly
as the invoking code, if you already have the
Assembly object, or if you have a Type
object for the class the method is on.
This technique may seem similar to delegates since both can dynamically determine
at runtime which method is to be called. Delegates, on the whole, require you to
know signatures of methods you might call at runtime, whereas with reflection, you
can invoke methods when you have no idea of the signature, providing a much
looser binding. More dynamic invocation can be achieved with
Delegate.
DynamicInvoke
, but this is more of a reflection-based method than the traditional del-
egate invocation.
The
InvokeMethod method shown in the Solution section contains all the code
required to dynamically invoke a method. This code first loads the assembly using its
assembly name (passed in through the
asmPath parameter). Next, it gets the Type
object for the class containing the method to invoke (the class name is gotten from
the
Test element’s className attribute). The method name is then retrieved (from the
Test element’s methodName attribute). Once you have all of the information from the
Test ...