CODEDOM

  • Thread starter Thread starter AA2e72E
  • Start date Start date
A

AA2e72E

With

CodeCompileUnit codeCompileUnit = new CodeCompileUnit();

it is possible to write/compile and assembly as illustrated by several
examples found on the Web.

I am looking for a way to see the text of the code that gets compiled i.e.
something like the *.CS files with VS2008 when editing/building the code
using that IDE.

How can I see the code?

Thanks.
 
With

CodeCompileUnit codeCompileUnit = new CodeCompileUnit();

it is possible to write/compile and assembly as illustrated by several
examples found on the Web.

I am looking for a way to see the text of the code that gets compiled i.e.
something like the *.CS files with VS2008 when editing/building the code
using that IDE.
There is no such code. CodeDOM operates on abstract graphs, not on a textual
representation. You can view the generated IL (and translate it into C#)
using a tool like Reflector (http://www.red-gate.com/products/reflector/).
 
There is no such code. CodeDOM operates on abstract graphs, not on a textual
representation. You can view the generated IL (and translate it into C#)
using a tool like Reflector (http://www.red-gate.com/products/reflector/).

You most certainly can get the code - I have written a code generator using
CodeDOM... Here is a snip-it:

public static string GenerateCode ( CodeGenerationParameters parameters )
{
// build the output stream
StringBuilder code = new StringBuilder ();
StringWriter writer = new StringWriter ( code );

// set options
CodeGeneratorOptions options = new CodeGeneratorOptions ();
options.BlankLinesBetweenMembers = false;
options.BracingStyle = "C";
options.VerbatimOrder = true;
options.IndentString = "\t";

// our class
CodeTypeDeclaration dataEntity = new CodeTypeDeclaration ( parameters.ClassName );
dataEntity.IsClass = true;

// make it serializable
dataEntity.CustomAttributes.Add ( new CodeAttributeDeclaration ( "Serializable" ) );

// add the fields
dataEntity.Members.AddRange ( GenerateFields ( parameters ) );

// add the default constructor
CodeConstructor defaultConstructor = new CodeConstructor ();
defaultConstructor.Attributes = MemberAttributes.Public;
dataEntity.Members.Add ( defaultConstructor );

// add the non-default constructor
dataEntity.Members.Add ( GenerateArgumentConstructor ( parameters ) );

// add the properties
dataEntity.Members.AddRange ( GenerateProperties ( parameters ) );

// generate the namespace code
CodeDomProvider provider = GetCodeDomProvider ( parameters.CodeGenerationLanguage );
provider.GenerateCodeFromType ( dataEntity, writer, options );

return code.ToString ();
}

private static CodeDomProvider GetCodeDomProvider (CodeGenerationLanguage language)
{
return ( language == CodeGenerationLanguage.CSharp ) ? (CodeDomProvider) new CSharpCodeProvider () : (CodeDomProvider) new VBCodeProvider ();
}
 
You most certainly can get the code - I have written a code generator using
CodeDOM...

You're absolutely right, I was absolutely wrong, and the CodeDOM
documentation clearly states so as well so there's no excuse. I was
projecting my knowledge of Lightweight Code Generation
(System.Reflection.Emit) onto CodeDOM.
 
Thank you Tom.

Permit me to ask another question: using CodeDom, how do I specify

[ClassInterface(ClassInterfaceType.AutoDual)] ?

I've tried everything I can think of without success.

Thanks for your help.
 
Back
Top