0

I am just trying to make a small, custom unit conversion library. I have all the units and conversion values in an XML file

<unit name="m">
   <multiply>1</multiply>
</unit>
<unit name="mm">
   <multiply>1E-3</multiply>
</unit>

and, at compile time, I would like to have them converted (wrong word probably) into an enum for other projects to use

public enum Units{m, mm, ...};

Is this possible? Is there a better way to accomplish the same thing?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
john
  • 4,043
  • 7
  • 27
  • 39
  • 2
    Sounds like something t4 or another build-tool could do... – Marc Gravell Jan 17 '12 at 22:36
  • At compile time you don't get any enums. At best you get MSIL assemblies or at worst compile time error messages. Are you trying to build C# code from XML? – Darin Dimitrov Jan 17 '12 at 22:36
  • The first step is get a normal C# method to produce a string containing your code, once you've done that converting it into a T4 template shouldn't be too tough. – George Duckett Jan 17 '12 at 22:39
  • 4
    If this is something you want done at compile time, You should probably just make them as enums in the first place – Sam I am says Reinstate Monica Jan 17 '12 at 22:39
  • 1
    I agree with Sam. Unit conversion rules don't change over time, I'd say hardcode those suckers. – MPelletier Jan 17 '12 at 22:40
  • Also make sure not to copy http://stackoverflow.com/questions/8902428/type-parameter-constraints-no-generics-or-nearest-offer solution for the same problem if it happens to be homework for the same school. – Alexei Levenkov Jan 17 '12 at 23:23

2 Answers2

2

Have a look at T4 templates. They automatically create any text file (*.cs, *.txt, *.html, *.xml, *.vb, etc.) in Visual Studio. With such a template you would read your xml-file and generate a *.cs file containing your enum.

See Code Generation and T4 Text Templates on MSDN.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
1

Normal C# code will likely be better for this particular case since number of units is relatively small. You will gain IntelliSence to edit values and clear compile time errors when you mess types up. Debugging several layers of transformation (XML -> CS -> some internal structures) is interesting and give you useful insights, but unless this project is purely for entertainment/learning it is not very productive. Another benefit is ability for someone else to understand code easier.

Note that your XML may look unnecessary complicated for units with non-linear differences from the base unit - easy to express with code, much more interesting to implement one-off XML data.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • Interesting. I searched for ways to create a unit conversion library and they all seemed to use xml files in one way or another (e.g. [here](http://www.codeproject.com/Articles/12520/Thunder-Measurement-Unit-Conversion-Framework)). Would you be able to provide an alternative way? I also noticed your earlier comment to look [here](http://stackoverflow.com/questions/8902428/type-parameter-constraints-no-generics-or-nearest-offer). – john Feb 03 '12 at 22:51