Showing posts with label Code Generation. Show all posts
Showing posts with label Code Generation. Show all posts

Tuesday, 9 June 2009

Code Generation - yield and Lambdas

So, while I don't profess to be an expert on Code Generation, I do hope that some of the tips that I put up here may save someone else the time it took me to figure out how to accomplish what appear to be simple steps.

At the time of writing this post, and having spent several hours scouring the internet, I couldn't for the life of me find a way to get a yield return statement.

The reason for this will be explained in another post (TODO:addlink) but the basic premise was to have the following line in the generated class:

   01 yield return () => SomeStringProperty;

All the properties returned strings, so this was fine.

Trying to generate this was proving a problem, until I came across this...hmm...workaround (don't want to use the word hack here)
While it ties the generated code to be specifically C#, this, again was not a problem for the code base that we were working against.

Here is the code that produced the line:

  01 generatedProperty.GetStatements.Add(new CodeSnippetExpression(string.Format("yield return () => {0}", existingProperty.Name)));

The genereatedProperty is of type CodeMemberProperty as is the existingProperty.

I wouldn't recommend this for all solutions, but it worked in this case where we were not trying to add to a generic code generator.

Submit this story to DotNetKicks Shout it

Wednesday, 3 June 2009

Code Generation - Attributes with Enum Values

Adding attributes to a class seemed straight forward when writting a class to do some code generation, adding Enum values to the attribute didn't.

Here is the code for adding an attribute with a string value:

    1 type.CustomAttributes.Add(
    2     new CodeAttributeDeclaration(new CodeTypeReference(typeof (RangeAttribute)),
    3                                  new CodeAttributeArgument("Min",
    4                                                            new CodePrimitiveExpression("1"))));

Knowing how to add the Enum wasn't at first obvious until (after some googling and experimentation) I figured out that an enum value is really a field...obvious...

...ok, not so, but here is the result:

    1 var enumTypeExpression = new CodeTypeReferenceExpression(typeof(Format));
    2 var enumValueExpression = new CodeFieldReferenceExpression(enumTypeExpression, Format.Bool.ToString());
    3 
    4 type.CustomAttributes.Add(
    5     new CodeAttributeDeclaration(
    6         new CodeTypeReference(typeof (FormatAttribute)),
    7         new CodeAttributeArgument("Format", enumValueExpression)));

Submit this story to DotNetKicks Shout it

Testing Code Generation and System.Xml file version

I found a very interesting problem the other day whilst trying to write automated test for a class that generated code.

I started by creating a file with what I expected the generated code to look like as my code generator was dumping its output straight to a file and then comparing the outcome. All went well and my tests past. Things only started to get strange when I checked in my code and it hit the CI server. The test started to fail.

By default the Code generator was putting the following attribute at the top of my classes:

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "TOKEN_VERSION")]

Where TOKEN_VERSION was the file version of the System.Xml.dll assembly. Wierdly enough the version of this file on the CI server was different, but only at the file level. Now My machine and the CI server bothe had 3.5 SP1 installed and so I was baffled. I did some googling and found that many different applications can install different version of this file. So then I decided to use a token in my expected file and change this with the actual version when the test was ran. I could have chosen to remove the attribute altogether, but this was the chosen path...

I was unaware of the following class but found it very useful in getting the file version, here is the end code:

    1 var assembly = AppDomain.CurrentDomain.GetAssemblies().First(a => a.GetName().Name == "System.Xml");
    2 
    3 var version = FileVersionInfo.GetVersionInfo(new Uri(assembly.CodeBase).AbsolutePath).ProductVersion;

Simple eh?

Submit this story to DotNetKicks Shout it