This will be short. I didn't go into much detail on the workaround I settled on for the aforementioned problem (lack of Unary and a few Binary expressions in le CodeDom). Someone asked, so it was specifically this:

  1. subclass CodeSnippetExpression with a CustomSnippet class
  2. your CustomSnippet class has a slot for a static ICodeProvider - this will determine the target language when generating code
  3. derive the classes you need from this eg:
    • BinaryOperatorExpression : CustomSnippet
    • UnaryOperatorExpression : CustomSnippet
  4. put the things you need in these, so:
    • Binary: a Left and Right CodeExpression as well as your own enum for the missing BinaryOperators
    • Unary: a single expression with a custom enum for the UnaryOperators
  5. in each of these, override the Value method. Well, I lie - it is actually sealed, so you have to use 'new'. Ugh.
  6. Just to get the concept first, in the Value method, you can auto detect the current type of ICodeProvider, and use this to determine how to convert your operator to text. Eg. a VB provider will return 'Not' and CSharp provider will return '!'.
  7. Now generate a string (still in the Value method) from the ICodeProvider's generator. For a binary method it goes something like
    gen(left) + op + gen(right)
    all these being strings of course. For unary it is either
    gen(expr) + op
    or
    op + gen(expr)
    ...depending on the operator and situation. Ultimately you return a string. This solves the problem of nested nested nested unary operators, as it recursively drills down, always returning the lower strings first.
  8. Now obviously, better than having a switch statement for every language you wish to support in each Value method (which means there is no way for someone else to add a language without you modifying and recompiling), you can just do something like have CustomSnippet add a static GenerateCustomSnippets class, that knows how to convert these custom expressions to text. The value method then just calls methods in that class when it needs to convert any custom stuff you've done.

This ensures your CodeComipleUnits do not contain any unrecognizable classes (all tools that work with ccu's can use these ccu's, as long as a GenerateCustomSnippets is available for the target language). It still allows you to work with Left, Right, typesafe Operators, etc - text representation is only generated when generating code. Generating code will not overwrite your structure. Of course, your CodeCompileUnit will NOT be useable to go to a language that hasn't implemented this hackish little system. Well, such is life.

If anyone wants to see the code, just let me know - it is pretty straightforward though. I understand this all may be obvious, I post this mostly in case you have a better way that I didn't think of ; ).

Shit, I forgot to diss VB.

posted on Friday, November 07, 2003 4:21 AM
Feedback
  • # re: Fustrated with CodeDOM
    aspZone.com
    Posted @ 3/7/2004 11:25 PM


  • # re: CodeCom reloaded
    kevin niu
    Posted @ 5/30/2004 9:00 PM
    I am working on a csharp parser and I try to create codedom tree. I am very interested in your code. If possible, please send it to me.

    Best Regards

    Kevin

Blog Stats

  • Posts - 121
  • Stories - 1
  • Comments - 1441
  • Trackbacks - 47

.Net Blogs

01101 Blogs

Flash Blogs

Graphics

People