Created
August 17, 2012 19:05
-
-
Save gabrielgreen/3381659 to your computer and use it in GitHub Desktop.
Mixin Demo
This file contains 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 MixinDemo | |
{ | |
public interface ILogger | |
{ | |
} | |
} |
This file contains 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
using System; | |
namespace MixinDemo.Logger1.Log | |
{ | |
public static class Mixin | |
{ | |
public static void Log(this ILogger logger, string message) | |
{ | |
Console.WriteLine("Logger1 Log: " + message); | |
} | |
} | |
} |
This file contains 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
using System; | |
namespace MixinDemo.Logger2.LogError | |
{ | |
public static class Mixin | |
{ | |
public static void LogError(this ILogger logger, string message) | |
{ | |
Console.WriteLine("Logger2 LogError: " + message); | |
} | |
} | |
} |
This file contains 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
using System; | |
namespace MixinDemo | |
{ | |
// Mix in the ILogger functionality required by the implementation of the Program class | |
using Logger1.Log; | |
using Logger2.LogError; | |
class Program | |
{ | |
ILogger logger = null; // OK to leave uninitialized since only extension methods will be called | |
static void Main(string[] args) | |
{ | |
new Program().Execute(); | |
} | |
private void Execute() | |
{ | |
try | |
{ | |
logger.Log("Hello World"); | |
(null as object).GetType(); // error | |
} | |
catch (Exception ex) | |
{ | |
logger.LogError(ex.Message); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment