In Brief

In this chapter, we took a look at two important topicsattributes and reflection. Here's an overview of this chapter's coverage:

  • Attributes let you specify metadata, including directives to the compiler, in your code. Reflection is a set of techniques that lets an application read and work with its own metadata. Reflection is made up of four parts : accessing metadata, discovering and examining types, late binding, and reflection emit.

  • You can create your own custom metadata attributes with the [AttributeUsage] attribute, which you use on the class that holds the support code for the attribute. You can pass data to that class's constructor when you use the custom attribute, as well as set property values, like Text in this example we saw: [Author("Grace Kelly", "11/25/48", Text="Hi Cary!")] .

  • You can extract metadata from attributes, such as the System.Reflection.MemberInfo class's GetCustomAttributes method at runtime.

  • There are various techniques that let you discover types at runtime. For example, you can load an assembly with the Assembly.Load method, creating an Assembly object. You can then call the GetTypes method of that object, returning an array of Type objects.

  • Late binding means that you don't have to specify which object you're calling a method on until runtime. You can call the Invoke method of MethodInfo objects to support late binding.

  • Reflection emit is the process of generating your own code at runtime. (As we've seen in this chapter, that means creating our own MSIL and running it, a rather involved process.)



Microsoft Visual C#. NET 2003 Kick Start
Microsoft Visual C#.NET 2003 Kick Start
ISBN: 0672325470
EAN: 2147483647
Year: 2002
Pages: 181

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net