本文整理汇总了C#中System.ComponentModel.Composition.ExportAttribute类的典型用法代码示例。如果您正苦于以下问题:C# ExportAttribute类的具体用法?C# ExportAttribute怎么用?C# ExportAttribute使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ExportAttribute类属于System.ComponentModel.Composition命名空间,在下文中一共展示了ExportAttribute类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: typeof
//Default export infers type and contract name from the
//exported type. This is the preferred method.
[Export]
public class MyExport1
{
public String data = "Test Data 1.";
}
public class MyImporter1
{
[Import]
public MyExport1 importedMember { get; set; }
}
public interface MyInterface
{
}
//Specifying the contract type may be important if
//you want to export a type other then the base type,
//such as an interface.
[Export(typeof(MyInterface))]
public class MyExport2 : MyInterface
{
public String data = "Test Data 2.";
}
public class MyImporter2
{
//The import must match the contract type!
[Import(typeof(MyInterface))]
public MyExport2 importedMember { get; set; }
}
//Specifying a contract name should only be
//needed in rare caes. Usually, using metadata
//is a better approach.
[Export("MyContractName", typeof(MyInterface))]
public class MyExport3 : MyInterface
{
public String data = "Test Data 3.";
}
public class MyImporter3
{
//Both contract name and type must match!
[Import("MyContractName", typeof(MyInterface))]
public MyExport3 importedMember { get; set; }
}
class Program
{
static void Main(string[] args)
{
AggregateCatalog catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(typeof(MyExport1).Assembly));
CompositionContainer _container = new CompositionContainer(catalog);
MyImporter1 test1 = new MyImporter1();
MyImporter2 test2 = new MyImporter2();
MyImporter3 test3 = new MyImporter3();
_container.SatisfyImportsOnce(test1);
_container.SatisfyImportsOnce(test2);
_container.SatisfyImportsOnce(test3);
Console.WriteLine(test1.importedMember.data);
Console.WriteLine(test2.importedMember.data);
Console.WriteLine(test3.importedMember.data);
Console.ReadLine();
}
}