Code Monkey home page Code Monkey logo

codebuilder's Introduction

CodeBuilder

A simple Class for building up C# code using functions...

License

MIT, Do No Evil...

Usage

The simplest 'Hello World' example is such:

var programBuilder = new ClassRenderer(namespaceRef: "HelloWorldNamespace", className: "HelloWorldProgram");
// Add the [directives]
programBuilder.AddDirective("System")
    .AddDirective("System.Linq");
        
// Add class interfaces if there are any
programBuilder.AddInterface("ISomeInterface");

// Add fields
programBuilder.AddField("String", "_greetings");


// Build an empty constructor
var constructorMethod = programBuilder.AddConstructor();
constructorMethod.AddLine("_greetings = \"Greetings!\";");

// Build another constructors
var greetingConstructor = programBuilder.AddConstructor( new []{"string greeting"} );
greetingConstructor.AddLine("_greetings = greeting;");

// Add a public method
// same for AddProtected, AddPrivate
var pubMethod = programBuilder.AddPublicMethod(
            "Greet", // name of the method
            "string", // return type of the method
            new[] {"string name"} // an string array of inputs

// build the internals of the public method
pubMethod.AddLine("return String.Format(\"{0}, Its nice to meet you, {1}\", _greetings, name);");
        
// Build the class
var programString = programBuilder.Emit();

This should produce a program that looks like this:

using System;
using System.Linq;

namespace HelloWorldNamespace {
    class HelloWorldProgram : ISomeInterface {
        private String _greetings;
        public HelloWorldProgram() {
            _greetings = "Hello";
        }

        public HelloWorldProgram(string greeting) {
            _greetings = greeting;
        }

        public string Greet(string name){
            return String.Format("{0}, Its nice to meet you, {1}", _greetings, name);
        }
    }
}

There are other API documented in the NUnit tests.

Author

Evin Grano

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.