Pompair
Pompair

Reputation: 7311

C# Generic Tree with multiple payload types

Is there a way to accomplish a generic tree data structure where nodes on each hierarchy level could hold different (and typed) payload objects?

I'd like to be able to query the tree with a key and get back a typed result, not an object. I've tried with generic interfaces but it seems I'm still needing to write separate query routines for each payload type. A generic solution would be more elegant, albeit casting from just one generic method is problematic. Maybe there is no way?

Also all the implementations in CodePlex or GitHub seem to allow just one payload type that is then used in all nodes in the whole tree. I want separate payload objects at each hierarchy level.

Thanks, Pom

Upvotes: 0

Views: 211

Answers (1)

bruno.almeida
bruno.almeida

Reputation: 2896

You could create an object tree (i know you don't want it, but see no other option) and your method to query you can set a type.

For example, tree.Query<T>() and returns a list of T elements.

Hope it helps.

Upvotes: 1

Related Questions