[Import ( "ButtonCaption" )]
public String ButtonCaption
{
get { return theButton.Content.ToString (); }
set {theButton.Content = value ; }
} * This source code was highlighted with Source Code Highlighter .
public class ExampleStringProvider
{
[Export ( "ButtonCaption" )]
public String providedCaption
{
get { return "MEF Hello World !!" ; }
}
} * This source code was highlighted with Source Code Highlighter .
public MyHelloWorld ()
{
Initializecomponent ();
CompositionContainer container =
new CompositionContainer ();
container.AddComponent <MefHelloWorld.MyHelloWorld> ( this );
container.AddComponent ( new
ExampleStringProvider ());
container.Bind ();
} * This source code was highlighted with Source Code Highlighter .
This code registers the interconnected components in a container (composition container) and, by calling the Bind method, binds the data from the extension to the existing property. Alternatively, the class of the ExampleStringProvider extension can be replaced by another class that provides other functionality, for example, which instead of text displays the date:
public class DateStringProvider
{
[Export ( "ButtonCaption" )]
public String providedCaption
{
get { return DateTime .Today.ToString (); }
}
} * This source code was highlighted with Source Code Highlighter .
As can be seen from a simple example, MEF implements a mechanism to bind data during the execution of a code through a reflection mechanism. The idea lay on the surface, and I am glad that Microsoft took care to create a convenient and powerful library that implements the mechanism for expanding the functionality of the code.
Source: https://habr.com/ru/post/26853/