Skip to content

Instantly share code, notes, and snippets.

@r-moeritz
Created September 6, 2012 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r-moeritz/3654643 to your computer and use it in GitHub Desktop.
Save r-moeritz/3654643 to your computer and use it in GitHub Desktop.
Sample RequireJS type modules in C#
namespace DuckTyping
{
using System;
class Aviary
{ // The Aviary "module"
public class __Duck__
{
public void Quack()
{
Console.WriteLine("Quack, I love the aviary!");
}
public void Feathers()
{
Console.WriteLine("My feathers shine because I can fly freely.");
}
}
public __Duck__ Duck()
{
return new __Duck__();
}
}
class Cage
{ // The Cage "module"
public class __Duck__
{
public void Quack()
{
Console.WriteLine("Quack, I'm trapped!");
}
public void Feathers()
{
Console.WriteLine("My feathers are torn and tattered from trying to get out of this blasted cage!");
}
}
public __Duck__ Duck()
{
return new __Duck__();
}
}
class Program
{
public static dynamic Require(string typeName)
{
var type = Type.GetType(typeName);
var ctor = type.GetConstructor(Type.EmptyTypes);
var obj = ctor.Invoke(null);
return obj;
}
public static void Main()
{
var aviary = Require("DuckTyping.Aviary");
var cage = Require("DuckTyping.Cage");
var a = aviary.Duck();
var c = cage.Duck();
a.Quack();
c.Quack();
a.Feathers();
c.Feathers();
Console.Read();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment