.NET CompileAssemblyFromSource: Determine which source failed?

852 views Asked by At

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?

1

There are 1 answers

0
Cheeso On

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.