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.

e x p o r t s ❘ 755<br />

The hosting application can use the dynamic type to import a contract with the name Wrox.ProCSharp<br />

.MEF.SampleContract :<br />

[Import("Wrox.ProCSharp.MEF.SampleContract")]<br />

public dynamic Foo { get; set; }<br />

With the dynamic keyword, the Foo property can now be used to access the Bar() method directly. The call<br />

to this method is resolved during runtime:<br />

string s = Foo.Bar();<br />

The dynamic type is explained in Chapter 12, “Dynamic Language Extensions.”<br />

Contract names <strong>and</strong> interfaces can also be used in conjunction to defi ne that the contract is used only if<br />

both the interface <strong>and</strong> the contract name are the same. This way, you can use the same interface for different<br />

contracts.<br />

eXPorTs<br />

In the previous example, you saw the part SimpleCalculator ,<br />

which exports the type Calculator with all its methods <strong>and</strong><br />

properties. The following example contains the SimpleCalculator<br />

as well, with the same implementation that was shown previously.<br />

Another part that is used here is a WPF User Control library named<br />

TemperatureConversion that defi nes a user interface, as shown<br />

in Figure 28 - 2. This control provides conversion between Celsius,<br />

Fahrenheit, <strong>and</strong> Kelvin scales. With the fi rst <strong>and</strong> second combo box,<br />

the conversion source <strong>and</strong> target can be selected. The Calculate<br />

button starts the calculation to do the conversion.<br />

The user control has a simple implementation for temperature<br />

conversion. The enumeration TempConversionType defi nes the<br />

different conversions that are possible with that control. The<br />

enumeration values are shown in the two combo boxes by setting figure 28-2<br />

the DataContext property of the user control in the constructor.<br />

The method ToCelsiusFrom() converts the argument t from its original value to Celsius. The temperature<br />

source type is defi ned with the second argument TempConversionType . The method FromCelsiusTo()<br />

converts a Celsius value to the selected temperature scale. The method OnCalculate() is the h<strong>and</strong>ler of<br />

the Button.Click event <strong>and</strong> invokes the ToCelsiusFrom() <strong>and</strong> FromCelsiusTo() methods to do the<br />

conversion according to the user ’ s selected conversion type.<br />

using System;<br />

using System.Windows;<br />

using System.Windows.Controls;<br />

namespace Wrox.ProCSharp.MEF<br />

{<br />

public enum TempConversionType<br />

{<br />

Celsius,<br />

Fahrenheit,<br />

Kelvin<br />

}<br />

public partial class TemperatureConversion : UserControl<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!