WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

System_CodeDom

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Execute CSharp in Memory

 Keywords: C# Microsoft.CSharp.CSharpCodeProvider System.CodeDom.Compiler.CompilerParameters CompileAssemblyFromSource GenerateInMemory Generate In Memory

;***************************************************************************
;**  Run C# in memory using WinBatch - Use Namespace.Class defined in CSharpeSource
;**
;** Purpose:  Run C# in memory using WinBatch - Using Namespace.Class defined in CSharpeSource
;** Inputs:
;** Outputs: Results in message
;** Reference:
;**       REQUIRES WinBatch 2013A or newer
;**
;** Developer: Deana Falk 2014.04.14
;***************************************************************************
If Version( )< '2013A'
   Pause('Notice', 'Need 2013A or Newer Version of WinBatch')
   Exit
EndIf

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Load assemblies into the WinBatch process.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; mscorlib assembly is automatically loaded by WinBatch when the CLR is loaded.
; ObjectClrOption ('use','mscorlib, version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')

ObjectClrOption("version", "v2.0.50727")
ObjectClrOption("use","System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")


;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Compiles the c# code in Memory
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
objCSharp = ObjectClrNew('Microsoft.CSharp.CSharpCodeProvider')
objParams = ObjectClrNew('System.CodeDom.Compiler.CompilerParameters')
objParams.GenerateInMemory = ObjectType( "VT_BOOL", 1 ) ;TRUE

objParams.ReferencedAssemblies.Add("System.dll")
objParams.ReferencedAssemblies.Add("System.XML.dll")

;Make sure the cSharp code includes both namepspace and class that can be later used by ObjectCLRNew
cSharpSource = ``
cSharpSource = cSharpSource :`using System;`:@LF
cSharpSource = cSharpSource :`using System.Xml;`:@LF
cSharpSource = cSharpSource :`namespace MyNamespace {`:@LF
cSharpSource = cSharpSource :` public class MyClass {`:@LF
cSharpSource = cSharpSource :`  public string MySelectNode() {`:@LF
cSharpSource = cSharpSource :`    // Create the XmlDocument.`:@LF
cSharpSource = cSharpSource :`   string str = @"<Names><Name type=""M"">John</Name> <Name type=""F"">Susan</Name><Name type=""M"">David</Name></Names>";`:@LF
cSharpSource = cSharpSource :`   XmlDocument xml = new XmlDocument();` :@LF
cSharpSource = cSharpSource :`    xml.LoadXml(str);` :@LF
cSharpSource = cSharpSource :`  XmlNodeList xnList = xml.SelectNodes("/Names/Name[@type='M']");`:@LF
cSharpSource = cSharpSource :`  string Results = "";`:@LF
cSharpSource = cSharpSource :`  foreach (XmlNode xn in xnList)`:@LF
cSharpSource = cSharpSource :`  {`:@LF
cSharpSource = cSharpSource :`    Results = Results + " " + xn.InnerText;`:@LF
cSharpSource = cSharpSource :`  }`:@LF
cSharpSource = cSharpSource :`  return Results;`:@LF
cSharpSource = cSharpSource :`  }`:@LF
cSharpSource = cSharpSource :` }`:@LF
cSharpSource = cSharpSource :`}`
objResult = objCSharp.CompileAssemblyFromSource(objParams,cSharpSource)

;Compiler Output
If objResult.Output.Count > 0
   strOutput = ''
   For x = 0 To objResult.Output.Count-1
      If strOutput == "" Then strOutput = objResult.Output.Item(x)
      Else strOutput = strOutput:@LF:objResult.Output.Item(x)
   Next
   Pause('Compiler Output',strOutput)
EndIf
; Compiler Errors
If objResult.Errors.Count > 0
    strErrors = ''
    ForEach ce In objResult.Errors
         ;Pause("Error", ce.ToString())
         If strErrors == "" Then strErrors = ce.ToString()
         Else strErrors = strErrors:@LF:ce.ToString()
    Next
    Pause('Compiler Errors',strErrors)
    Exit
EndIf

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Create a class implemented by a managed assembly.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Use Namespace.Class defined in CSharpeSource
oSample = ObjectClrNew( 'MyNamespace.MyClass' )
Results = oSample.MySelectNode()
Pause('Results',Results)

Exit

Article ID:   W17807
Filename:   Execute CSharp in Memory.txt
File Created: 2014:04:18:10:22:44
Last Updated: 2014:04:18:10:22:44