5

I'm dynamically compiling code, using the CompileAssemblyFromSource with multiple sources.

In the event of a compile error I can retreive the line number etc. from the Errors collection.

However the line number is the line number within all sources. What I need is which source and the line number from within the failed source (among the added sources).

Is that possible without doing calculation acrobatics?

casperOne
  • 73,706
  • 19
  • 184
  • 253
Henrik Brinch
  • 161
  • 1
  • 6
  • 3
    I'd consider using `CompileAssemblyFromFile` instead (at least as a debug or verbose option) this gives you the ability to look at the sources after the fact and see what was going on. If you use temporary files you can always clean up afterwards... – Paul Roberts Oct 14 '11 at 17:35
  • 1
    @Paul That should be an answer! – Chris Shouts Oct 14 '11 at 19:16
  • @Paul: Writing to a temporary file I can't use I'm afraid. In my solution (that has the specific problem), the sources are gathered from many individual sources (even from end user) and I need only to see the actual source that has the problem. – Henrik Brinch Oct 14 '11 at 19:57
  • Is the line number from the individual source, or is from all the sources appended together ? i.e. if you get back line 5, it could be line 5 in any of the source. – user957902 Oct 14 '11 at 21:40
  • @Henrik: are you in an environment in which you cannot write temporary files? I'm guessing from your response that you a debug/verbose solution is not ok, because you know you might get errors in a production scenario (code from an end user data pretty much guarantees this!). The temp file approach might still be appropriate, just make sure to delete all your temp files when you are done. If you can't write temp files, then calculation acrobatics may be your only solution... – Paul Roberts Oct 14 '11 at 23:39

1 Answers1

0

I do this in one of my apps, and if there is an error, i write out the source that was compiled - the source that generated the compiler error - along with all error messages generated during the compile. I put these error messages in comments and append them to the end of-the source file. The source file gets written with File.WriteAllText() if i remember correctly, in a file in the user temp directory, and then i throw an exception with the path to that source module. All this happens only if there's a compile error. If no error, then i don't write out the source this way.

There really is just one module in my case because i concatenate all source into a single module. There is no confusion about what "line 143" means in this situation.

Cheeso
  • 189,189
  • 101
  • 473
  • 713