Package Exports
- codeflex
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (codeflex) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Codeflex
Dynamically compile VB.NET and C#.NET code from javascript using Node.JS and Electron.
Why use Codeflex?
Code flex is a really useful tool that allows us to run C#/VB code in a Node JS environment. Codeflex can be used to compile entire directories of source code and reference the instances of the source code as a dynamic link library (dll).
Code flex can also be used to compile source code written inline with javascript. Making it easy to switch between languages eliminating the need to re-write existing c# or vb code.
Requirements
- Windows based operating system
- .NET Framework 4.6.2
Compile .vb files from a directory
The code below will locate all .vb files in the specified directory (recursively). If the code compiles, the result will return a assemblyId in the callback function. This assemblyId is used to access this compiled assembly.
var codeflex = require("codeflex");
flex.compileDirectory("vb","./directory/containing/vb/files/",function(err, assemblyId){
if(err){
//Something went wrong.
console.log('Error compiling directory');
console.log(err);
return;
}
console.log("The code was compiled into an in-memory assembly. You can access this assembly using the assemblyId");
console.log(assemblyId);
});
Compile c# files from a directory
To compile source files in c# instead of vb. Use the code above and set the first parameter (language parameter) of flex.compileDirectory() to 'c#'
flex.compileDirectory("c#","./directory/containing/c/files/",function(err, assemblyId){
});Compiling source code as inline code
You don't have to compile code from source files, you can instead provide code to compile as a string and use it in the same way.
The code must be supplied in the form of a function with the VB or C# code commented out using /* code here */
var flex = require("codeflex");
flex.compileCode("vb",function(){
/*
public namespace MyNamespace
public class VBTestCode
public SayHello(PersonName as String) as String
return "Hello, " & PersonName & "."
end function
end class
end namespace
*/
},function(err,assemblyId){
if(err){
//Something went wrong
console.log(err);
return;
}
console.log(assemblyId);
});Create an instance of an object compiled from source code
Here is an example of how to compile code and create an instance of an object specified within that code. Once a instance is created, the instance can be referenced with it's instanceId. This is needed for calling methods in the instance. (Mentioned later in this article.)
var flex = require("codeflex");
flex.compileCode("vb",function(){
/*
public namespace MyNamespace
public class VBTestCode
public SayHello(PersonName as String) as String
return "Hello, " & PersonName & "."
end function
end class
end namespace
*/
},function(err,assemblyId){
if(err){
//Something went wrong
console.log(err);
return;
}
//The code was compiled and the compiler returned an assemblyId for us to reference it with.
flex.createInstance(assemblyId, "MyNamespace.VBTestCode",function(err, instanceId){
if(err){
//Something went wrong. could not create instance
console.log(err);
return;
}
//Instance created! We can now reference this instance using the returned instanceId.
console.log(instanceId);
});
});Compile code, create an instance, call a method
Now we have our assemblyId and instanceId, we can finally call a method within that instance directly from javascript.
- The first parameter of .executeMethod is the assemblyId.. we obtain this by compiling the code. (Read above)
- The second parameter is the instanceId.. we obtain this by creating an instance of an object from the compiled the code. (Read above)
- The third parameter is the list of variables to send to the method.
- The forth parameter is the call back function that raises when the method returns.
flex.executeMethod(assemblyId,instanceId,{"PersonName":"Phil"},function(err, result){
if(err){
//Something went wrong. The method invocation failed.
console.log(err);
return;
}
console.log("The method completed execution and returned the result:");
console.log(result);
});Putting it all together, compile, instance, execute
Here is a complete example of how to compile some code, create an instance of an object in the compiled code and execute one of it's methods.
This example uses inline vb code, you could also load the code from a directory of source files, either VB or C# files. (.vb / .cs). Use flex.compileDirectory instead of flex.compileCode
var flex = require("codeflex");
flex.compileCode("vb",function(){
/*
public namespace MyNamespace
public class VBTestCode
public SayHello() as String
return "Hello world."
end function
end class
end namespace
*/
},function(err,assemblyId){
if(err){
//Something went wrong
console.log(err);
return;
}
//The code was compiled and the compiler returned an assemblyId for us to reference it with.
flex.createInstance(assemblyId, "MyNamespace.VBTestCode",function(err, instanceId){
if(err){
//Something went wrong. could not create instance
console.log(err);
return;
}
//Instance created! We can now reference this instance using the returned instanceId.
console.log(instanceId);
flex.executeMethod(assemblyId,instanceId,{},function(err, result){
if(err){
//Something went wrong. The method invocation failed.
console.log(err);
return;
}
console.log("The method completed execution and returned the result:");
console.log(result);// The result should be: 'Hello, Phil.'
});
});
});