Created
January 2, 2013 23:31
-
-
Save gogsbread/4439369 to your computer and use it in GitHub Desktop.
Simple MEF based calculator. MEF hacks
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
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.ComponentModel.Composition; | |
using System.ComponentModel.Composition.Hosting; | |
namespace dotNetPlayGround | |
{ | |
public interface ICalculator | |
{ | |
string Calculate(string input); | |
} | |
public interface ILiteral | |
{ | |
int OP { get; } | |
} | |
public interface IOperation | |
{ | |
int Operate(int left, int right); | |
} | |
public interface IOperator | |
{ | |
char Symbol { get; } | |
} | |
[Export(typeof(IOperation))] | |
[ExportMetadata("Symbol", '+')] | |
public class Add:IOperation | |
{ | |
public int Operate(int left, int right) | |
{ | |
return left + right; | |
} | |
} | |
[Export(typeof(ICalculator))] | |
public class SimpleCalculator : ICalculator | |
{ | |
[ImportMany] | |
IEnumerable<Lazy<IOperation,IOperator>> _operations = null; | |
bool _debug = false; | |
public SimpleCalculator() | |
{ | |
} | |
public SimpleCalculator(bool debug):this() | |
{ | |
_debug = debug; | |
} | |
public string Calculate(string input) | |
{ | |
int left; | |
int right; | |
char op; | |
int opIndex = FindFirstNonDigit(input); | |
if (opIndex < 0) return "Could not parse"; | |
try | |
{ | |
//separate out the operands | |
left = int.Parse(input.Substring(0, opIndex)); | |
right = int.Parse(input.Substring(opIndex + 1)); | |
} | |
catch | |
{ | |
return "Could not parse command."; | |
} | |
op = input[opIndex]; | |
if (_debug) | |
Console.WriteLine("left:{0} right:{1} operator:{2}", left, right, op); | |
foreach (Lazy<IOperation, IOperator> operation in _operations) | |
{ | |
if (_debug) | |
Console.WriteLine("Here"); | |
if (operation.Metadata.Symbol.Equals(op)) | |
return operation.Value.Operate(left, right).ToString(); | |
} | |
return "Operation not found"; | |
} | |
private int FindFirstNonDigit(String s) | |
{ | |
for (int i = 0; i < s.Length; i++) | |
{ | |
if (!(Char.IsDigit(s[i]))) return i; | |
} | |
return -1; | |
} | |
} | |
class MEFHacks | |
{ | |
[Import(typeof(ICalculator))] | |
public ICalculator calculator; | |
private CompositionContainer _container; | |
private MEFHacks() | |
{ | |
AggregateCatalog catalog = new AggregateCatalog(); | |
catalog.Catalogs.Add(new AssemblyCatalog(typeof(MEFHacks).Assembly)); | |
_container = new CompositionContainer(catalog); | |
try | |
{ | |
_container.ComposeParts(this); | |
} | |
catch (CompositionException ex) | |
{ | |
Console.WriteLine(ex.ToString()); | |
} | |
} | |
public static void Main() | |
{ | |
MEFHacks framework = new MEFHacks(); | |
Console.Write("Enter math expression:"); | |
string expression = Console.ReadLine(); | |
Console.WriteLine(framework.calculator.Calculate(expression)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment