11using System ;
22using System . CodeDom . Compiler ;
33using System . Collections . Concurrent ;
4- using System . Collections . Generic ;
5- using System . IO ;
64using System . Linq ;
75using System . Reflection ;
86using System . Text ;
9- //using Microsoft.CSharp;
10- using Microsoft . CodeDom . Providers . DotNetCompilerPlatform ;
117
128namespace Westwind . Scripting
139{
@@ -71,6 +67,12 @@ public class CSharpScriptExecution
7167 /// </summary>
7268 public bool SaveGeneratedCode { get ; set ; }
7369
70+ /// <summary>
71+ /// If true throws exceptions rather than failing silently
72+ /// and returning error state. Default is false.
73+ /// </summary>
74+ public bool ThrowExceptions { get ; set ; }
75+
7476
7577 #region Error Handling Properties
7678
@@ -219,6 +221,17 @@ public void AddAssembly(Type type)
219221 AddAssembly ( type . Assembly . Location ) ;
220222 }
221223
224+ /// <summary>
225+ /// Adds a list of assemblies to the References
226+ /// collection.
227+ /// </summary>
228+ /// <param name="assemblies"></param>
229+ public void AddAssemblies ( params string [ ] assemblies )
230+ {
231+ foreach ( var assembly in assemblies )
232+ AddAssembly ( assembly ) ;
233+ }
234+
222235 /// <summary>
223236 /// Adds a namespace to the referenced namespaces
224237 /// used at compile time.
@@ -247,6 +260,10 @@ public void AddNamespaces(params string[] namespaces)
247260 }
248261 }
249262
263+ /// <summary>
264+ /// Adds basic System assemblies and namespaces so basic
265+ /// operations work.
266+ /// </summary>
250267 public void AddDefaultReferencesAndNamespaces ( )
251268 {
252269 AddAssembly ( "System.dll" ) ;
@@ -273,6 +290,8 @@ public void AddDefaultReferencesAndNamespaces()
273290 /// <returns></returns>
274291 public object ExecuteMethod ( string code , string methodName , params object [ ] parameters )
275292 {
293+ ClearErrors ( ) ;
294+
276295 object instance = ObjectInstance ;
277296
278297 if ( instance == null )
@@ -293,7 +312,7 @@ public object ExecuteMethod(string code,string methodName, params object[] param
293312 Assembly = CachedAssemblies [ hash ] ;
294313
295314 // Figure out the class name
296- Type type = Assembly . ExportedTypes . First ( ) ;
315+ var type = Assembly . ExportedTypes . First ( ) ;
297316 GeneratedClassName = type . Name ;
298317 GeneratedNamespace = type . Namespace ;
299318 }
@@ -316,6 +335,8 @@ public object ExecuteMethod(string code,string methodName, params object[] param
316335 /// <returns></returns>
317336 public object Evaluate ( string code , params object [ ] parameters )
318337 {
338+ ClearErrors ( ) ;
339+
319340 if ( string . IsNullOrEmpty ( code ) )
320341 throw new ArgumentException ( "Can't evaluate empty code. Please pass code." ) ;
321342
@@ -335,6 +356,8 @@ public object Evaluate(string code, params object[] parameters)
335356 /// <returns></returns>
336357 public object ExecuteCode ( string code , params object [ ] parameters )
337358 {
359+ ClearErrors ( ) ;
360+
338361 code = ParseCodeNumberedParameters ( code , parameters ) ;
339362
340363 return ExecuteMethod ( "public object ExecuteCode(params object[] parameters)" +
@@ -426,6 +449,7 @@ private StringBuilder GenerateClass(string code)
426449
427450 if ( SaveGeneratedCode )
428451 GeneratedClassCode = sb . ToString ( ) ;
452+
429453 return sb ;
430454 }
431455
@@ -441,6 +465,8 @@ private StringBuilder GenerateClass(string code)
441465 /// <returns></returns>
442466 public object ExecuteCodeFromAssembly ( string code , Assembly assembly , params object [ ] parameters )
443467 {
468+ ClearErrors ( ) ;
469+
444470 Assembly = assembly ;
445471
446472 ObjectInstance = CreateInstance ( ) ;
@@ -457,6 +483,8 @@ public object ExecuteCodeFromAssembly(string code, Assembly assembly, params obj
457483 /// <returns></returns>
458484 public bool CompileAssembly ( string source )
459485 {
486+ ClearErrors ( ) ;
487+
460488 if ( OutputAssembly == null )
461489 Parameters . GenerateInMemory = true ;
462490 else
@@ -472,28 +500,42 @@ public bool CompileAssembly(string source)
472500
473501 CompilerResults = Compiler . CompileAssemblyFromSource ( Parameters , source ) ;
474502
475- //if (CompilerMode == ScriptCompilerModes.Roslyn)
476- // CompilerResults = Compiler.CompileAssemblyFromSource(Parameters, source);
477- //else
478- // CompilerResults = CompilerClassic.CompileAssemblyFromSource(Parameters, source);
479-
480503 if ( CompilerResults . Errors . HasErrors )
481504 {
482- Error = true ;
483-
484505 // *** Create Error String
485506 ErrorMessage = CompilerResults . Errors . Count + " Errors:" ;
486507 for ( int x = 0 ; x < CompilerResults . Errors . Count ; x ++ )
487- ErrorMessage = ErrorMessage + "\r \n Line: " + CompilerResults . Errors [ x ] . Line . ToString ( ) + " - " +
508+ ErrorMessage = ErrorMessage + "\r \n Line: " + CompilerResults . Errors [ x ] . Line + " - " +
488509 CompilerResults . Errors [ x ] . ErrorText ;
510+
511+ SetErrors ( new ApplicationException ( ErrorMessage ) ) ;
512+
489513 return false ;
490514 }
491515
492516 Assembly = CompilerResults . CompiledAssembly ;
493517
494518 return true ;
495519 }
520+ #endregion
521+
522+ #region Errors
523+ private void ClearErrors ( )
524+ {
525+ LastException = null ;
526+ Error = false ;
527+ ErrorMessage = null ;
528+ }
529+
530+ private void SetErrors ( Exception ex )
531+ {
532+ Error = true ;
533+ LastException = ex . GetBaseException ( ) ;
534+ ErrorMessage = LastException . Message ;
496535
536+ if ( ThrowExceptions )
537+ throw LastException ;
538+ }
497539
498540 #endregion
499541
@@ -509,6 +551,8 @@ public bool CompileAssembly(string source)
509551 /// <returns></returns>
510552 public object InvokeMethod ( object instance , string method , params object [ ] parameters )
511553 {
554+ ClearErrors ( ) ;
555+
512556 // *** Try to run it
513557 try
514558 {
@@ -518,8 +562,7 @@ public object InvokeMethod(object instance, string method, params object[] param
518562 }
519563 catch ( Exception ex )
520564 {
521- Error = true ;
522- ErrorMessage = ex . GetBaseException ( ) . Message ;
565+ SetErrors ( ex ) ;
523566 }
524567
525568 return null ;
@@ -534,10 +577,7 @@ public object InvokeMethod(object instance, string method, params object[] param
534577 /// <returns>Instance of the class or null on error</returns>
535578 public object CreateInstance ( )
536579 {
537-
538- LastException = null ;
539- Error = false ;
540- ErrorMessage = null ;
580+ ClearErrors ( ) ;
541581
542582 if ( ObjectInstance != null )
543583 return ObjectInstance ;
@@ -551,15 +591,17 @@ public object CreateInstance()
551591 }
552592 catch ( Exception ex )
553593 {
554- Error = true ;
555- LastException = ex . GetBaseException ( ) ;
556- ErrorMessage = ex . Message ;
594+ SetErrors ( ex ) ;
557595 }
558596
559597 return null ;
560598 }
561599
600+
601+
562602 #endregion
603+
604+
563605 }
564606
565607 public enum ScriptCompilerModes
0 commit comments