11.07.2015 Views

y - Net Developer

y - Net Developer

y - Net Developer

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 11 ■ WORKING WITH DATA 345ramrameshreddy.blog.com■Note Primitive types and certain types, such as DateTime and XmlElement, are treated as primitivebecause they are always known to the .NET runtime. As a result, you do not need to add them via the[KnownType] attribute. The only exception to this is when using arrays of primitive types.Sometimes both the endpoints involved will not be aware of the types and therefore warrantthe use of the [KnownType] attribute. These situations are as follows:• As stated earlier, the data type sent is inherited from the type that is being expected.• The type sent is declared as an interface as opposed to a concrete implementation suchas a class, structure, or enumeration. Since you cannot know in advance what typeimplements that interface, the [KnownType] attribute is required.• The declared type that is being sent is declared of the type Object; because every typeinherits from Object, the type cannot be known in advance.• Some of the types, even though declared, might not fall in one of the previous three situations.For example, a HashTable internally stores the actual object using the Object type.Listing 11-5 shows an example of how you can use the [KnownType] attribute when usinga HashTable. The class QuickReturnPortfolio implements a HashTable, and because of the[KnownType] attribute, the runtime is aware that only the types QuickReturnStock andQuickReturnBond are stored in the HashTable.Listing 11-5. Data Contract Using [KnownType] Attribute[DataContract]public class QuickReturnStock { }[DataContract]public class QuickReturnBond { }[DataContract][KnownType(typeof(QuickReturnStock))][KnownType(typeof(QuickReturnPortfolio))]public class QuickReturnPortfolio{[DataMember]System.Collections.Hashtable thePortfolio;}ramrameshreddyramrameshreddy

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!