标签:style blog color io os 使用 ar for 数据
public class MyClass { [Import("MajorRevision")] public int MajorRevision { get; set; } } public class MyExportClass { [Export("MajorRevision")] //This one will match. public int MajorRevision = 4; [Export("MinorRevision")] public int MinorRevision = 16; }
public class MyClass { [Import("TheString")] public dynamic MyAddin { get; set; } } [Export("TheString", typeof(IMyAddin))] public class MyLogger : IMyAddin { } [Export("TheString")] public class MyToolbar { }
public class MyClass { [Import] public Lazy<IMyAddin> MyAddin { get; set; } } [Export(typeof(IMyAddin))] public class MyLogger : IMyAddin { }
public class MyClass { private IMyAddin _theAddin; public MyClass() { } //This constructor will be used. //An import with contract type IMyAddin is //declared automatically. [ImportingConstructor] public MyClass(IMyAddin MyAddin) { _theAddin = MyAddin; } }
[ImportingConstructor] public MyClass([Import(typeof(IMySubAddin))]IMyAddin MyAddin) { _theAddin = MyAddin; }
public class MyClass { [Import(AllowDefault = true)] public Plugin thePlugin { get; set; } //If no matching export is avaliable, //thePlugin will be set to null. }
public class MyClass { [ImportMany] public IEnumerable<IMyAddin> MyAddin { get; set; } }
[Export] public class DataOne { //This part will be discovered //as normal by the catalog. } [Export] public abstract class DataTwo { //This part will not be discovered //by the catalog. } [PartNotDiscoverable] [Export] public class DataThree { //This part will also not be discovered //by the catalog. }
//元数据视图 public interface IPluginMetadata { //元数据 string Name { get; } [DefaultValue(1)] int Version { get; } } [Export(typeof(IPlugin)), ExportMetadata("Name", "Logger"), ExportMetadata("Version", 4)] public class Logger : IPlugin { } [Export(typeof(IPlugin)), ExportMetadata("Name", "Disk Writer")] //Version is not required because of the DefaultValue public class DWriter : IPlugin { } public class Addin { [Import] public Lazy<IPlugin, IPluginMetadata> plugin; }
public class User { [ImportMany] public IEnumerable<Lazy<IPlugin, IPluginMetadata>> plugins; public IPlugin InstantiateLogger () { IPlugin logger = null; foreach (Lazy<IPlugin, IPluginMetadata> plugin in plugins) { if (plugin.Metadata.Name = "Logger") logger = plugin.Value; } return logger; } }
[InheritedExport(typeof(IPlugin)), ExportMetadata("Name", "Logger"), ExportMetadata("Version", 4)] public class Logger : IPlugin { //Exports with contract type IPlugin and //metadata "Name" and "Version". } public class SuperLogger : Logger { //Exports with contract type IPlugin and //metadata "Name" and "Version", exactly the same //as the Logger class. } [InheritedExport(typeof(IPlugin)), ExportMetadata("Status", "Green")] public class MegaLogger : Logger { //Exports with contract type IPlugin and //metadata "Status" only. Re-declaring //the attribute replaces all metadata. }
[MetadataAttribute] [AttributeUsage(AttributeTargets.Class, AllowMultiple=false)] public class MyAttribute : ExportAttribute { public MyAttribute(string myMetadata) : base(typeof(IMyAddin)) { MyMetadata = myMetadata; } public string MyMetadata { get; private set; } } //隐式指定contract type和元数据 [MyAttribute("theData")] public MyAddin myAddin { get; set; }
标签:style blog color io os 使用 ar for 数据
原文地址:http://www.cnblogs.com/phenixyu/p/3968332.html