Hello
I am making an application that generates source code. I am facing some
problems.
1- How can I generate code that uses the c# 'params' keyword to specify
multiple arguments?
I tried 2 ways
method.Parameters.Add(new CodeParameterDeclarationExpression("params
object[]", name));
and
method.Parameters.Add(new
CodeParameterDeclarationExpression(typeof(object[]), name));
CodeAttributeDeclaration attr = new CodeAttributeDeclaration();
attr.Name = "ParamArrayAttribute";
method.Parameters[2].CustomAttributes.Add(attr);
The problem with the first way is that it will not work with generating
other languages (such as VB.NET), but it works fine with c#
The problem with the second method, is that ParamArrayAttribute can't be
use in c# so the generated code will not compile. I compiles in VB
2- I want to generate a while loop, but it seems that only for can be
generated. Here is an example
CodeIterationStatement Loop = new CodeIterationStatement();
Loop.InitStatement = new CodeSnippetStatement("");
Loop.IncrementStatement = new CodeSnippetStatement("");
Loop.TestExpression = Invoke(VarRef("reader"), "Read");
If I leave InitStatement and IncrementStatement code generation fails
with a NullReference exception, the above code generates
for (
; reader.Read();
)
Although this is equivalent to while(reader.Read()), but it doesn't look
nice. Also do-while loops are not supported. I know that they can be
implemented with goto, but it will not look good either. I would like to
know also how can I generate continue and break?
3- Is switch / case supported by CodeDom in anyway?
4- When strings in generated code are very long, they are divided to several
lines, I don't want this, or at least I can control it.
for example CSharpCodeProvider would generate "Hel" + "lo World" I would
rather make it "Hello " + "World"
5- JSharpCodeProvider would translate primitive types to keywords sometimes
when it is not appropriate. Ex.
new CodeFieldReferenceExpression(new
CodeTypeReferenceExpression(typeof(int)), "MinValue")
whould generate int.MinValue which produces a compiler error. It should be
System.Int32.MinValue
6 - Some of the class return file extensions with a '.' and some don't. For
example CSharpCodeProvider returns "cs" while VJSharpCodeProvider returns
".jsl". This is a very minor problem, and easy to solve, I have my code
check for the existence of '.' in the extension. But I think all classes
must be consistent.
To Solve all the above problems, I wanted to inherit the CSharpCodeGenerator
that does this, but I found that it is internal, and there is no way I can
have acces to it.
Sorry for my long post, and my bad english. And thanks a lot for taking time
to read this.
Best regards,
Sherif