Podstrony
- Strona startowa
- (ebook pdf) Teach Yourself SQL in 21 Days
- Linux. .Mandrake.10.Podręcznik.Użytkownika.[eBook.PL] (3)
- Bruce Morris Adventure Guide Florida Keys & Everglades National Park (2005)
- (eBook) James, William The Principles of Psychology Vol. II
- [eBook] DirectX 3D Graphics Programming Bible
- MS03
- w strone milosci montgomery
- Professional Feature Writing Bruce Garrison(3)
- Wieczor panienski Izabela Pietrzyk
- Orson Scott Card Siodmy syn (2)
- zanotowane.pl
- doc.pisz.pl
- pdf.pisz.pl
- tohuwabohu.xlx.pl
[ Pobierz całość w formacie PDF ]
.Viewing the MetadataThe custom attribute from the previous section is ready to use.All we need to do is add oneor more entries to another class to see it at work.The example places attributes in twoplaces to make it easier to discuss the effect of the attribute on application metadata.However, the three most important entries appear at the class level, because we ll usereflection to read them in the next section.Here are the three entries we ll add to the class:[EntityDescribe("11/27/2001","John","Created the new form for test application.")][EntityDescribe("11/28/2001","Rebecca","OK Button Click Event Handler",Comment="Needs more work")][EntityDescribe("11/29/2001","Chester","Added Attribute Test Button")]public __gc class QuickForm : public FormAs you can see, the three custom attribute entries all describe aspects of the target class,QuickForm.One of the entries also makes use of the optional Comment property.Noticethat you need to provide the name of the custom property to use it.After you add some custom attribute entries to the application, you can compile it.Open theapplication using the ILDASM utility (discussed in several places in the book).Figure 14-1shows the results of the three entries we made to the class.Notice that ILDASM doesn tmake them that readable, but you can definitely see them in the list (starting with thehighlighted entry in the figure).Figure 14-1: ILDASM allows you to see any custom attributes you make.If you want to see the full effect of an attribute entry, you need to attach it to a data membersuch as a pushbutton.Open the disassembly of the data member by double-clicking it inILDASM.Figure 14-2 shows an example of the [EntityDescribe] attribute attached to thebtnOK object.Notice that you can see all three of the default entries.Figure 14-2: Use a disassembly to see the full effect of any custom attribute you create.It s important to realize that the compiler stores the property values as they appear for theconstructor.In other words, the compiler doesn t change the attribute in any way.The datayou add to an assembly is readable in the disassembly, even if the attribute would normallyencrypt the data.This security problem means you shouldn t store company secrets in theattributes, and you might want to remove attributes from applications before you send themto a customer.Here s an example of the readable data for the btnOK object:Using ReflectionOne of the reasons to include metadata within an assembly is to document the assembly sfeatures and problems.Of course, reading these comments with ILDASM is hardly the bestway to make use of the information.You could easily view the source code instead andprobably save time by doing so.Reflection enables an application to read its own metadataand the metadata contained in other assemblies.This feature enables you to create utilitiesto read the metadata and do something with it.The nice part of this feature is that it alsoapplies to the Microsoft assemblies, which means you can finally search them for tidbits ofinformation.Creating an application that relies on reflection isn t difficult, but it does look at tad strange atfirst.Listing 14-7 shows an example of reflection at work.In this case, clicking the Attributebutton on the example application will fill the txtOutput textbox with the content of the higherlevel attributes for the sample application.Listing 14-7void QuickForm::Attribute_Click(Object* sender, EventArgs* e){Object *Attr[]; // Array of attributes.EntityDescribe *CurrAttr; // Current Attribute.// Gain access to the member information for the class.MemberInfo *Info = __typeof(QuickForm); // Grab the custom attribute information.Attr = __try_cast(Attribute::GetCustomAttributes(Info,__typeof(EntityDescribe)));// Look at each attribute in the array.for (int Counter = 0; Counter Length; Counter++){// Obtain the current attribute.CurrAttr = __try_cast(Attr[Counter]);// Display the information.txtOutput->AppendText(CurrAttr->Date);txtOutput->AppendText("\t");txtOutput->AppendText(CurrAttr->Author);txtOutput->AppendText("\t");txtOutput->AppendText(CurrAttr->Purpose);txtOutput->AppendText("\t");txtOutput->AppendText(CurrAttr->Comment);txtOutput->AppendText("\r\n\r\n");}}The first task is to gain access to the member information that you want to use to search forattributes.In this case, we ll gain access to the QuickForm class and view the class levelattributes.The Info variable contains this information.The next task is to search for the attributes.You ll find a number of overrides for theGetCustomAttributes() method.Listing 14-7 shows just one option.In this case, we llsearch for all of the EntityDescribe attributes found in the QuickForm class.Notice the use ofthe __try_cast attribute.You need to order the statements as shown in the listing or VisualC++ will display an error message during the compile cycle and not provide much in the wayof a helpful answer about what s wrong with the code.(In short, the precise sequence ofevents is important.)NoteThe.NET Framework actually provides two methods for gaining access to acustom attribute s information.The first is GetCustomAttribute(), whichreturns a single attribute the first one it finds.The second isGetCustomAttributes(), which returns an array of all of the attributes that fitwithin the search criteria.After you gain access to the attributes, you need some way to convert them to readable orstorable data.The first step in the For loop is to cast the individual attribute entry into aEntityDescribe data type.After that, you can use the data to display information on screen,send it to the printer for a report, or place it within a database for later use.In this case, we lluse the AppendText() method to place the data in the txtOutput textbox.Following is theoutput from the application.State Information MaintenanceMetadata fill one other purpose that you ve probably already thought about.Look again atthe disassembly in Figure 14-1
[ Pobierz całość w formacie PDF ]