Reflection: -
Reflection Demos
is used to:
-
Discover type information from an existing assembly by examining its metadata.
For example, in the TestHarness, we need to use reflection to distinguish between test driver libraries, defined to be types that implement the ITest interface, from other libraries that contain code
to be tested.
-
We also use reflection to learn about the structure of an existing assembly so that we can write code that uses the assembly effectively.
The lecture today provides an example of that use in Reflection Demos.
Delegates: -
delegates.htm
are used for two purposes:
-
To allow reusable library code to signal application specific code of a library processing event. Examples are button clicks in a GUI
and the Directory Navigation component we discussed in the last
lecture. Use of delegates allows us to remove all application specific code from our library components so that they can be reused
without change.
-
To bind to lambdas, allowing your code to transport a lambda's processing and data to some other part of your code that needs to use
the lambda. An example is the construction of thread pools where an application defines processing for a thread in a lamda, then enqueues a delegate bound to
the lambda. When a thread is available it dequeues the delegate and invokes its lambda processing. We will say a lot more about lambdas
when we cover threading.
Lambdas: -
lambdas.htm
Lamdas provide means to define processing in a block of code and provide data that processing needs. We then can store the lambda for
later use or transport it to some other piece of code that needs it. Lambdas are used to:
-
Encapsulate processing and data to be used in some other code location.
-
Define a snippet of processing that is used in several places in some function without cluttering up the using class with private
functions and private data that are only used locally in that one function.
-
Define anonymous methods, often bound to a delegate for access, that syntactically are very similar to
C++'s global (unbound) functions.
If that sounds like creating a class and sending an instance of the class somewhere, that's exactly what it is.
Lambdas are a quick and easy way to define objects and use them, letting the compiler generate code that defines the class.