npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

codeflex

v1.0.25

Published

Compile and access .NET C# and VB code from Node.

Downloads

88

Readme

Codeflex

A framework for the NPM package FFI: https://www.npmjs.com/package/ffi

Dynamically compile VB.NET and C#.NET code from javascript using Node.JS, Compile entire directories of VB/C# code.

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

  1. Windows based operating system
  2. .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 flex = 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#'

var flex = require("codeflex");

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(){
/*
namespace MyNamespace

public class VBTestCode

    public function SayHello(Name as string) as String
        return "Hello, " & Name 
    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(){
/*
namespace MyNamespace

public class VBTestCode

    public function SayHello(Name as string) as String
        return "Hello, " & Name 
    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.

  1. The first parameter of .executeMethod is the assemblyId.. we obtain this by compiling the code. (Read above)
  2. The second parameter is the instanceId.. we obtain this by creating an instance of an object from the compiled the code. (Read above)
  3. The third parameter is the list of variables to send to the method.
  4. The forth parameter is the call back function that raises when the method returns.
  flex.executeMethod(assemblyId,instanceId,"SayHello",{"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(){
/*
namespace MyNamespace

public class VBTestCode

    public function SayHello(Name as string) as String
        return "Hello, " & Name 
    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,"SayHello",{"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);// The result should be: 'Hello, Phil'
        });
    });
});

Adding .NET Assembly References

You can include additional references to the .NET framework assemblies by adding @import at the top of the code to be compiled.

Visual Basic:

'@import(System.Drawing.dll)

C#

//@import(System.Drawing.dll)