本文整理匯總了C#中System.ComponentModel.Composition.ImportAttribute類的典型用法代碼示例。如果您正苦於以下問題:C# ImportAttribute類的具體用法?C# ImportAttribute怎麽用?C# ImportAttribute使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
ImportAttribute類屬於System.ComponentModel.Composition命名空間,在下文中一共展示了ImportAttribute類的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();
}
}