MEF(managed extensibility framework)-导出属性和方法

本文探讨了MVC框架中特性注入的概念,包括输出和输入特性的应用,以及如何通过元数据和自定义特性来增强组件的功能。文章详细介绍了如何在方法上应用特性以指定其行为,并展示了如何在消费端通过特性注入来获取这些信息。

// 输出
[Export("Add",typeof(Func<double,double,double>))]
public doble add(double x,double y){......}
// 输入
[Imort("Add",typeof(Func<double,double,double>))]
public Func<double,double,double> Add{get;set;}
// 输入-使用
reault = add( a,b);

输出元数据
[ExportMetadata("speed","fast")]
public double Add(double x,double y){.......}

输入元数据
[importMany("Add",typeof(Func<double,double,double>))]
public Lazy<Func<double,double,double>,IDictionary<string,object>>[] AddMethods { get;set;}
输入元数据-使用
if(addMethod.Metadata.ContainsKey("speed") &&
    (string)addMethod.Metadata["speed"] == "fast") 
        result = addMethod.Value(a,b);

自定义特性
public enum Speed{ Fast,Slow }
[MetadataAttribute]
[AttributeUsage(AttributeTargets.Method| attributeTargets.Class)]
public class SpeedExportAtribute: ExportAttribute{
    public SpeedExportAtribute(string contractName, Type contractType)
        :base(contractName,contractType){
    }
    public Speed speed{get;set;}
}
输出
[SpeedExport("Add",type(Func<double,double,double>),Speed=Speed.Fast)]
public double Add(double x,double y){.....}

public interface ISpeedCapbilities{
    Speed Speed{ get; }
}
输入
[ImportMany("Add", typeof(Func<double, double,double>))]
public Lazy<Func<double, double,double>,ISpeedCapabilities>[] AddMethods{ get; set;}
输入-使用
if(addMethod.Metadata.Speed == Speed.Fast)
    result = addMethod.Value( a,b);

英语

contract
vi. 收缩; 感染; 订约; vt. 感染; 订约; 使缩短; n. 合同; 婚约
satisfied
adj. 感到满意的; v. 使满意(satisfy的过去式)
container
n. 集装箱; 容器
composition
n. 作文,作曲; [材] 构成; 合成物managed
v. 管理;掌管;主管;当……的经理人;调教;经营管理(土地)
extensibility: n. 展开性;可延长性
framework
n. 框架,骨架; 结构,构架

managed
adj. 托管的; 与中央化计划及管制有关的; v. 管理; 负责; 设法完成(manage的过去分...

extensibility
n. 展开性; 可延长性

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值