Hi all!
I found an interesting way to implement strong-typed object attactment
with generic:
// Attachment should be a Dictionary<K,V> or some user-defined
container, rather
// than primitive types such as string, int or DateTime.
class AttachableObject<AttachmentType> where AttachmentType: new() {
AttachmentType attachment;
AttachmentType Attachment {
get {
if (this.attachment == default(AttachmentType))
this.attachment = new AttachmentType();
return this.attachment;
}
}
}
Attachment is useful for those who create/use the object to *attach*
some properties at runtime, without modifying the original object/class
or the creator's class structure. And if the object itself also wants
to use the attachment, just add more "where" constraint.
Cheers!