1

I'm trying to implement a data structure in C# 2.0 that could be represented like this:

  | field1 | field2 | field3 | ... (variable length) ...
  +--------+--------+--------+-------------------
1 | int0   | float0 | bool0  | ...
2 | int1   | float1 | bool1  | ...
. |
. |
. |
(variable) ...

Currently, I'm thinking on something really basic:

public class DataStructure {
    string name;
    List<string> fieldNames;
    List<Item> items;
}

public class Item {
    List<object> values;
}

Then, you could access with methods that implicitly define which type you're retrieving (i.e. bool GetBool (string field), int GetInteger (string field)), so you work with a small set of built in types and custom clases (e.g. CustomBaseObject).

Is there any existing data structure implementation that does this? Is this another (better) approach to solve the problem I'm presenting here? (Taking into account access & modification operation's complexity)

Also, I don't know what's the best way of boxing/unboxing the List<object> values, if there's any other than just casting it like (type)values[index].

frarees
  • 2,190
  • 3
  • 17
  • 22
  • How about using an old-school System.Data.DataTable? – Jake H Mar 26 '13 at 16:01
  • Looks nice! However, I see that Unity3D (Mono 2.10.2) doesn't seem to have the namespace `System.Data` available, so I don't have access to `DataTable` :( – frarees Mar 26 '13 at 16:23

1 Answers1

1

In C# 4.0 you can get a decent implementation of these kinds of structures using ExpandoObject, which gives you better development experience with the dynamic keyword. See: http://msdn.microsoft.com/en-us/magazine/ff796227.aspx for a good intro.

In C# 2.0, you can get similar behavior with an object that implements IExpando. See What is IExpando and where is it used? for details.

If you choose not to go that route, then you'll probably want to switch the internal implementation from two lists to an IDictionary<String, Object> since it is easier to implement and quickly overtakes the dual-list implementation in performance as fields are added.

Community
  • 1
  • 1
codekaizen
  • 26,990
  • 7
  • 84
  • 140
  • That's C# 4.0. No way to do this on C# 2.0. – frarees Mar 26 '13 at 15:57
  • Great. About `IExpando`, I will take a look at it. I wasn't using `Dictionary`, cause I'm using Unity3D and it cannot serialize structures like `Dictionary` and `Hashtable` (nor found a way to do it) but it does for `List`. I need all this data to be serialized by their internal implementation. Will I be able to get my `DataStructure` class serialized if I use `IDictionary` and handle `List`s internally? – frarees Mar 26 '13 at 16:12
  • You should be able to serialize the contents of the dictionary, even if you need to do it yourself. I'm not quite sure how Unity3D does serialization, but if they rely on .Net serialization you should just be able to implement `ISerializable` and handle storage of dictionary values in the `GetObjectData` method. If they do serialization some other way, there should be hooks that allow you to do it, and it should be straightforward, since it's just a set of key/value pairs. – codekaizen Mar 26 '13 at 16:22
  • I think they use a custom implementation (more about this topic: http://blogs.unity3d.com/2012/10/25/unity-serialization/). What do you mean by hooks? Any hint to "find them"? I was fiddling with `ISerializable` and a way to properly write `GetObjectData` and `SaveData` for my specific case, but couldn't find a way to make it work. – frarees Mar 26 '13 at 16:33
  • By hooks I mean a way to control the serialization more directly than by using attributes. I'm not sure if this is possible, and http://answers.unity3d.com/questions/19047/serialize-class-with-generic-dictionary-that-holds.html indicates it may not be, but then Unity3 might have solved it. – codekaizen Mar 26 '13 at 16:44
  • It seems it doesn't http://answers.unity3d.com/questions/425317/serializing-data-structures-the-unity-way-possible.html. I see there's nothing sensible to do in that direction. – frarees Mar 26 '13 at 20:51
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/26978/discussion-between-codekaizen-and-frarees) – codekaizen Mar 26 '13 at 21:35