468,532 Members | 1,647 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,532 developers. It's quick & easy.

Strong-typed object attachment

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!

Nov 16 '05 #1
0 934

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

129 posts views Thread by Torbjørn Pettersen | last post: by
10 posts views Thread by Tony Jones | last post: by
8 posts views Thread by Tamir Khason | last post: by
6 posts views Thread by Manuel Lopez | last post: by
8 posts views Thread by per9000 | last post: by
6 posts views Thread by raylopez99 | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.