Reflection Invoke Sample

Sample Overview

This sample illustrates how to invoke different kinds of methods through reflection. Because the names of the methods being invoked are stored in strings, this mechanism provides the capability to specify methods to be invoked at runtime, rather than at design-time, providing the scope to give your users control over which particular method gets called. Although this sample focuses on invoking methods, you can also set and get properties and fields, if required.

In many coding scenarios, you know the task that you want to carry out before you want to do it. Therefore, you can specify the methods that you need to invoke, and the parameters you need to pass them. However, there are also situations where you might want to dynamically invoke methods, based upon specific scenarios, or user actions. This capability is available through the Reflection namespace, by using the InvokeMember method on the Type object.

See the sample source code for more detailed comments.

Sample Source and Build Output Locations

The sample source is found in sscli\samples\howto\reflection\invoke. 

The source file is:

The build output location is %TARGETCOMPLUS%\samples\howto\reflection\invoke.  The output file is an executable assembly named reflectioninvoke.exe.

Building the Sample

All samples are built from the buildall script. 

You can also build all the samples by switching to the root of the sample directory, sscli\samples, and typing build -c.

You can build this specific sample by switching to the sample directory and typing build -c.

Running the Sample

These steps require that the Shared Source CLI (SSCLI) be already built and functional.

  1. Run env.bat or source the env.csh or env.sh script files depending on your platform.
  2. Switch to the %TARGETCOMPLUS%\samples\howto\reflection\invoke directory.
  3. Type the following command:

    clix reflectioninvoke.exe


Copyright (c) 2002 Microsoft Corporation. All rights reserved.