Reflection Invoke Technology Sample
This sample demonstrates how to implement late binding to any managed assembly with public classes and methods. From the command line you select an assembly, type, method, and parameters. The sample will attempt to load the assembly, locate the type and method, and invoke the method with the passed parameters. The sample even attempts to do the necessary type conversions for method calls that take parameters that are not strings. The reflection classes make this advanced functionality surprisingly simple. A simple DLL project named Tester.dll is included with this sample. The tester project provides a simple type with some methods to try with the Invoke sample.
For information about using the samples, see the following topics:
To build the sample using the command prompt
Navigate to one of the language-specific subdirectories for the sample.
Type msbuild InvokeCS.sln or msbuild InvokeVB.sln, depending on your choice of programming language, at the command line.
To build the sample using Visual Studio
Open Windows Explorer and navigate to one of the language-specific subdirectories for the sample.
Double-click the icon for InvokeCS.sln or InvokeVB.sln, depending on your choice of programming language, to open the file in Visual Studio.
On the Build menu, click Build Solution.
Note |
---|
This sample builds a console application. You must launch it using the command prompt in order to view its output. Invoke.exe accepts command line parameters indicating what you would like to invoke. Sample usage is displayed if it is run with no parameters. Use the following syntax to invoke a method on a type: |
Invoke.exe [Assembly] [Type] [Method] [Parameters...]
To run the sample
Navigate to the directory containing Invoke.exe and Tester.dll.
Type Invoke.exe [Assembly] [Type] [Method] [Parameters...] at the command line.
Remarks
The msbuild tool builds two files, Invoke.exe and Tester.dll.
In the following example, the ShowMessage
method on type SomeType
is called with three parameters, for example:
Invoke.exe Tester.dll Microsoft.Samples.SomeType ShowMessage "This is an example of a message." "Question:" 4
For more information about reflection invoke, see the comments in the source code files.
The following items briefly describe the technologies and classes used by this sample:
Reflection
Assembly Used to load an assembly and to search for a type in that assembly.
Type Used to obtain an array of MemberInfo instances, as well as to refer to a type when creating an instance if needed.
MethodInfo Used to discover information about a single method, including parameters and method name. The sample compares information from this type with the information provided on the command line. MethodInfo is also used to invoke a method.
ParameterInfo Used primarily to find the type of method parameters, so that the sample can cast command line arguments appropriately.
Activator Used to create an instance of a type, if the sample finds that a call to an instance method is needed.
System
See Also
Reference
Activator
Array
Assembly
AssemblyBuilderAccess
Convert
MethodAttributes
MethodInfo
ParameterInfo
System.Reflection
Type
Other Resources
Reflection
Emitting Dynamic Methods and Assemblies
Dynamic Source Code Generation and Compilation