Created
September 6, 2012 10:37
-
-
Save r-moeritz/3654643 to your computer and use it in GitHub Desktop.
Sample RequireJS type modules in C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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