views:

616

answers:

2

It seems that .NET CF is missing the very useful Reflection.Emit. So far, I found this library as an alternative: http://www.codeplex.com/EmitCF.

However it seems to be an abandoned early version, so I'm looking for more options.

Does anyone know of another alternative to Emit? Or perhaps someone used EmitCF and can comment on its status?

BTW, the bigger picture: I'm trying to get Emit for the CF, so that I can get http://dynamic.codeplex.com to work under the CF, so I can optimize the serialization code I'm using (http://www.codeproject.com/KB/XML/GR%5FCustomXmlSerializer.aspx)

+4  A: 

What you need is Cecil (http://mono-project.com/Cecil), a Mono project library to generate and inspect programs and libraries in CIL format. It's actively maintained, does a lot more than Reflection.Emit and it's used in a lot of projects, including some that target .NET CF.

Andreia Gaita
EmitCF is based on Cecil...I'll take a closer look at Cecil though, it's a good idea. The newest version of Cecil I can find is 0.6, from 2007. Are there newer ones?
Roy Peled
Cecil has since been moved to the Mono core, so the latest versions are distributed with the Mono class libraries. The source is at http://anonsvn.mono-project.com/viewvc/trunk/mcs/class/Mono.Cecil/, and if you download Mono, Mono.Cecil.dll will be included.
Andreia Gaita
+1  A: 

This isn't exactly an answer to your question, but since Reflection.Emit isn't supported in CF, an alternative approach you could take to serialization/deserialization would be to compile your classes into a regular Windows application, and make use of Reflection.Emit to programatically generate serialize and de-serialize methods for each class, which could then be incorporated back into the class in the CF version. Basically, you'd use Reflection.Emit in the full framework for code generation.

This would be more work (and a constant source of more work, of course), but it would perform better than a dynamic, Reflection.Emit-based approach (which doesn't work in CF anyway). Most CF classes will work unchanged in the full framework, although not necessarily, of course.

MusiGenesis
Great idea, it would make an ideal solution if it works :-)I'm not sure how to go about implementing it though. Could you elaborate on how you would incorporate the methods back into the class? Also, do you know of any projects using this technique, which I could use as an example?
Roy Peled
After looking into Reflection a little bit, I realized that you wouldn't have to do what I suggested at all. I was proposing a code generator that would just iterate through all the Fields and Properties (using Type.GetFields and Type.GetProperties) and construct custom Serialize and Deserialize methods that you would then copy and paste back into the original class. As it turns out, you don't need to use Reflection.Emit for this, so there's no reason you couldn't just do this iteration dynamically in custom Serialize and Deserialize methods in your CF class.
MusiGenesis
You could even just write a generic class with Serialize and Deserialize static methods that take an Object as the parameter and return the serialized stuff (for Serialize) or that takes the serialized piece and returns an Object (for Deserialize). GetFields and GetProperties (along with the FieldInfo and PropertyInfo classes) are available in the custom framework. You could even serialize to XML or to a byte[] array as you like, but since you want to handle future changes to the classes I think XML would be the way to go.
MusiGenesis
Let me know if I'm not being clear here, and I'll post some of my sample code that I wrote to test this this morning.
MusiGenesis
I'm not sure if I understood you correctly. Are you suggesting to write a code generator that uses reflection to create serialization methods? It will be great if you can post your sample code.
Roy Peled
This gave me the idea of using PostSharp to generate reflection-free serialization methods at compile time. I'll try to see if I can get this to work.
Roy Peled
@Roy: my initial suggestion was to write a code generator that uses Reflection to create serialization methods. When I looked at it further, though, I realized that the Reflection methods I was considering using for this (GetFields, GetProperties) were all present in the Compact Framework, so you wouldn't even *need* to do code generation - you could serialize and deserialize your classes with iterative runtime methods. Then I realized that you could just use the built-in .Net serialization to do the same thing. Then I closed my browser and went and had a beer.
MusiGenesis