Import and export contracts [MEF] section 02 of the MEF

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/gjysk/article/details/44746875

First, the presentation outlined
in this demonstration shows how to specify exported to Export protocol name and type, as well as the protocol name and type how to specify the import of Import, only to ensure that export and import of protocol names and types match up, in order to inject successful, otherwise the injection will failure.
Download (screen recording) : http://yunpan.cn/cZIxLTJgGhpvp  access code 3b86
online play : http://v.youku.com/v_show/id_XOTIzNTU0ODUy.html
Tips : If your screen video and download the code does not work, you can station message, or send an email to [email protected]

friends are welcome to add QQ group interested in studying .NET related technologies: 18,507,443


Second, the key code

contains the names and types of exported agreement:

[Export("SQL", typeof(ILogger))]
public class SqlLogger : ILogger
{
    public void WriteLog(string message)
    {
        Console.WriteLine("SQL Logger => {0}", message);
    }
}

Export wherein the first parameter is the name of the protocol, the second parameter is a type of export. Introducing the corresponding code is as follows:

[Import("SQL", typeof(ILogger))]
public ILogger SqlLogger { get; set; }

Export Export contains only the name of the protocol:
[Export("Oracal")]
public class OracalLogger : ILogger
{
    public void WriteLog(string message)
    {
        Console.WriteLine("Oracal Logger => {0}", message);
    }
}
Deriving the above-described protocol does not specify a type of export, and therefore in order to derive as a modified class type, i.e. OracalLogger. Introducing the corresponding code:
[Import("Oracal")]
public OracalLogger OracalLogger { get; set; }

Where the type attribute is OracalLogger, rather than ILogger. If the type is ILogger, the import will fail.


Export Export contains only type:
[Export(typeof(ILogger))]
public class TxtLogger : ILogger
{
    public void WriteLog(string message)
    {
        Console.WriteLine("Text Logger => {0}", message);
    }
}
Export to export the above-mentioned agreement does not specify the protocol name, only specify the type of export. Introducing the corresponding code:
[Import(typeof(ILogger))]
public ILogger TextLogger { get; set; }

It contains neither export type, Export Export Agreement does not contain the name of:
[Export]
public class XmlLogger : ILogger
{
    public void WriteLog(string message)
    {
        Console.WriteLine("Xml Logger => {0}", message);
    }
}
The export protocol default protocol called above code, the type of the same class and the modified, i.e. XmlLogger, matching import code should look like this:
[Import]
public XmlLogger XmlLogger { get; set; }

Always make sure the type of property types and characteristics of modified export import properties XmlLogger consistent, otherwise the import fails.

Third, related resources
1, MSDN official information: http://msdn.microsoft.com/zh-cn/library/dd460648(v=vs.110).aspx



Guess you like

Origin blog.csdn.net/gjysk/article/details/44746875