Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

12. Reflection > Using Reflection

Using Reflection

So now that you know all about reflection, what do you do with it? In other words, what is metaprogramming?

Next, we will look at two specific applications of .NET reflection: declarative programming and writing plug-in systems.

Declarative Programming

Perhaps the simplest thing to do when using reflection is to create more declarative code. Rather than hardcoding data into properties and methods, you can encode data by using attributes instead. This leads to potentially cleaner and more readable code.

Consider Example 12-8, which defines a function determineBoxToUse to calculate the proper container to ship an item in. Each item that can be shipped inherits from the ShippingItem class and overrides the Weight and Dimension properties, which are used to determine the correct box size.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint