2

Inside our application (C# - .NET 4.5) we have a report generation tool. This tool must receive only the SQL command, verify it and from that, create a whole new report with available fields with the same name as specified by the SQL command and corresponding data types, similar to what an ORM tool would do.

Because of the nature of this tool we're using Reflection and Emit to create a whole new class. From fields provided by a dataReader (System.Data.SqlClient.SqlDataReader) we can create the type and populate it with the corresponding data.

The result of this is a IQueryable object that I can use on my reports.

This whole process is done and tested but to keep both the report, the generated class and the SQL command together we need to save this new type on the database and because or our database layout and system definitions, this requires me to provide an XML-like file or string to a method that will compress and convert to a Base64 string before saving it.

It would be a simple task if I were to save the report into a DLL file, just like shown HERE.

But since this new type must be transformed into an XML-like format I'm a little bit lost in here.

I have done the opposite on the past: fully create a type from an pure XML file, manually. I also know that I can do something similar but it would require me to loop into every detail/property/method/member/etc of the class to create the XML file.

Is there any way (like a helper from .NET framework) that could help me here?
Instead of doing it 100% manually I'd like to delegate the XML generation/parse to a tool probably with a better perforance too...

Edit:

People, READ THE TITLE BEFORE POSTING COMMENTS! I'm trying to save an XML for the type. TYPE. Not the object. The type.

@Mark Gravell Thanks for the tip. I'll check that. But about the schema: any way to save/load it automatically?

Anderson Matos
  • 3,132
  • 1
  • 23
  • 33

1 Answers1

2

For saving the type, I would say either simply store the schema, and re-create a compatible type at runtime, or just use AssemblyBuilder etc and configure the dynamic-assembly as saveable, and write it as a .dll to disk (or elsewhere). Then just load the .dll at runtime and find the type. Either approach can work. If you already have the code to create a Type from a schema, the first may be easier.

For saving the data, my first instinct would be XmlSerializer, however that works via Assembly generation, so it might not like working against a fully-dynamic Type, from TypeBuilder. If XmlSerializer isn't happy, you could try protobuf-net; that also works in-memory (by default), so should be pretty happy.

However! I should note that you might also want to consider simply using a DataTable. While I don't have tons of love for DataTable, it is designed for exactly this scenario:

  • it can model fields that are defined only at runtime
  • it has inbuilt serialization of both schema and data
  • it implements the ComponentModel APIs for declaring runtime-models
    • which means most tools work with it for free
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • After you mentioned the Xml Schema solution, I have fount this answer to be something quite similar to what I need: http://stackoverflow.com/questions/3680353/how-do-i-programmatically-generate-an-xml-schema-from-a-type I'll take a look into that. – Anderson Matos Jun 21 '12 at 03:11