Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Suppose I have Node<T> so I'm using Node<int> and Node<long> etc.

At some point I want the get the bytes for the T parameter in the class so I can serialize to disk. i.e. int becomes 4 bytes and long becomes 8 bytes.

The constraints are that it must be extremely fast.

How is this possible.

EDIT :

I don't want to use a switch and typeof(T) if possible.

I'm not looking for serializers also.
Posted
Updated 9-Nov-11 4:23am
v2

Well, what you are looking for is a serializer anyway, no matter if you are looking for a serialization or not. So, it might be a custom serializer.

Now, you want to make it agnostic to the type, and that means Reflection, which is relatively expensive as you know.

Again, you can address to the experience of creation of available serializers. Do you how they use Reflection to make it faster? They generate serialization assemblies on the fly. Even though you Node type is abstracted from the type representing its data element type T, each assembly may have only a finite number of instantiations of these generic, so the number of actually used data element types is statically known, and each of these types is statically known, so it's possible to generate an assembly which is loaded during run-time in your Application Domains and support serialization of the Node for every actually used type T. Solving this problem does not seem to be easy for me; and apparently well beyond Quick Questions & Answers. Hope somebody did that. I would advise you to look for some existing open-source serializers on CodeProject and elsewhere, even though you are not looking for them. :-) Not for immediate use but for learning the technique. It should use System.Reflection.Emit. If you look at Visual Studio project settings, you will find "generate serialization assembly", but, of course, it only applies to the serialization in .NET libraries.

You can also try to create a serialization assembly on demand, each time a new type T is used. To re-use existing implementations you can use a dictionary with the key of System.Type.

I also saw an alternative approach based on delegates, closure and cashed results of Reflection.

Another idea might work if you only use primitive types or simple structures. The problem is that the mechanism of generic constraints does not accept any constraints for primitive type, so you won't know if your type if "good" or not for substitution for T. The idea is using unsafe code, typeof and pointers to move data from/to an array of bytes to/from a piece of memory of your variable of the unknown type T (only the size is used), pinned in memory using fixed. Well, it's unsafe. :-)

—SA
 
Share this answer
 
Comments
Mehdi Gholam 9-Nov-11 11:23am    
Can I pass a delegate or something similar to the generic Node of T since I know the type that I am creating ie Node of int
Sergey Alexandrovich Kryukov 9-Nov-11 11:53am    
Not quite clear. "Pass to the generic node" means having some method of a type to pass to. What do you mean? Generic method or not? Short answer is: yes you can, so what?
--SA
Sergey Alexandrovich Kryukov 9-Nov-11 11:54am    
Basically, your problem might need some research...
--SA
Mehdi Gholam 9-Nov-11 12:03pm    
Got the code working with typeof<T> == typeof<int> etc
Sergey Alexandrovich Kryukov 9-Nov-11 12:50pm    
Thank you for the vote and accepting my answer, but can you share some detail?
--SA
I'm not sure how fast you actually need it to be, but I've always found the NSerializer library extremely useful. It'll serialize stuff that the XmlSerializer can't like generic Dictionaries:

http://code.google.com/p/nserializer/[^]

Hope it helps.
 
Share this answer
 
Comments
Mehdi Gholam 9-Nov-11 10:25am    
Thanks but I'm not looking for a serializer.
Sergey Alexandrovich Kryukov 9-Nov-11 11:09am    
Just a note: I understand why this solution might be not good for you, but, after all, serializer is just a concept. As you formulate you problem, its solution, if it's found, would be called serializer and nothing else, just by its definition. OK, please take a look at my answer.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900