15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

752 ❘ ChaPTer 28 mAnAGed extensibility frAmewOrk<br />

}<br />

}<br />

}<br />

foreach (var item in operations)<br />

{<br />

Console.WriteLine("Name: {0}, number oper<strong>and</strong>s: {1}", item.Name,<br />

item.NumberOper<strong>and</strong>s);<br />

operationsDict.Add(item.Name, item);<br />

}<br />

Console.WriteLine();<br />

string selectedOp = null;<br />

do<br />

{<br />

try<br />

{<br />

Console.Write("Operation ");<br />

selectedOp = Console.ReadLine();<br />

if (selectedOp.ToLower() == "exit" ||<br />

!operationsDict.ContainsKey(selectedOp))<br />

continue;<br />

var operation = operationsDict[selectedOp];<br />

double[] oper<strong>and</strong>s = new double[operation.NumberOper<strong>and</strong>s];<br />

for (int i = 0; i < operation.NumberOper<strong>and</strong>s; i++)<br />

{<br />

Console.Write("\t oper<strong>and</strong> {0} ", i + 1);<br />

string selectedOper<strong>and</strong> = Console.ReadLine();<br />

oper<strong>and</strong>s[i] = double.Parse(selectedOper<strong>and</strong>);<br />

}<br />

Console.WriteLine("calling calculator");<br />

double result = Calculator.Operate(operation, oper<strong>and</strong>s);<br />

Console.WriteLine("result: {0}", result);<br />

}<br />

catch (FormatException ex)<br />

{<br />

Console.WriteLine(ex.Message);<br />

Console.WriteLine();<br />

continue;<br />

}<br />

} while (selectedOp != "exit");<br />

The output of one sample run of the application is shown here:<br />

Name: +, number oper<strong>and</strong>s: 2<br />

Name: -, number oper<strong>and</strong>s: 2<br />

Name: /, number oper<strong>and</strong>s: 2<br />

Name: *, number oper<strong>and</strong>s: 2<br />

Operation +<br />

oper<strong>and</strong> 1 3<br />

oper<strong>and</strong> 2 5<br />

calling calculator<br />

result: 8<br />

Operation -<br />

oper<strong>and</strong> 1 7<br />

oper<strong>and</strong> 2 2<br />

calling calculator<br />

result: 5<br />

Operation exit<br />

Without recompiling the host application, it is possible to use a completely different add-in library. The<br />

assembly AdvCalculator defines a different implementation for the Calculator class to offer more<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!