C#中怎样实现类似Python中的装饰器
再补充个Attribute+Reflection的例子 class Test { static void Main(String args) { var sc = new SomeClass(); sc.SomeAction(); Console.WriteLine(); sc.SomeCalculate(5, 8); Console.Read(); } } public class SomeClass : ContextBoundObject { public void SomeAction() { Console.WriteLine("do some Action"); } public Int32 SomeCalculate(Int32 a, Int32 b) { var sum = a + b; Console.WriteLine("do some Calculate:{0} plus {1} equals {2}", a, b, sum); return sum; } } public class DecoratorClassAttribute : ContextAttribute, IContributeObjectSink { public DecoratorClassAttribute() : base("Decorator") { } public IMessageSink GetObjectSink(MarshalByRefObject obj, IMessageSink nextSink) { return new Decorator(nextSink); } } public class DecoratorMethodAttribute : Attribute { public DecoratorMethodAttribute() { PrefixPrint = "function start"; SuffixPrint = "function end"; } public DecoratorMethodAttribute(String strPrefixPrint, String strSuffixPrint) { PrefixPrint = strPrefixPrint; SuffixPrint = strSuffixPrint; } public String PrefixPrint { get; set; } public String SuffixPrint { get; set; } } public class Decorator : IMessageSink { public Decorator(IMessageSink nextSink) { _NextSink = nextSink; } #region IMessageSink private IMessageSink _NextSink; public IMessageSink NextSink { get { return _NextSink; } } public IMessageCtrl AsyncProcessMessage(IMessage msg, IMessageSink replySink) { throw new NotImplementedException(); } public IMessage SyncProcessMessage(IMessage msg) { IMessage result = null; var call = msg as IMethodCallMessage; if (call == null || Attribute.GetCustomAttribute(call.MethodBase, typeof(DecoratorMethodAttribute)) == null) { result = NextSink.SyncProcessMessage(msg); } else { var attr = Attribute.GetCustomAttribute(call.MethodBase, typeof(DecoratorMethodAttribute)) as DecoratorMethodAttribute; Console.WriteLine(attr.PrefixPrint); result = NextSink.SyncProcessMessage(msg); Console.WriteLine(attr.SuffixPrint); } return result; } #endregion }执行结果:function startdo some Actionfunction endbegin the calculatedo some Calculate:5 plus 8 equals 13end the calculate————————————————————补充了个例子:class Test { static void Main(String args) { Console.WriteLine("test for Action"); ProxyClass.Action(() =\u0026gt; { Console.WriteLine("do some action"); }); Console.WriteLine(); Console.WriteLine("test foc Func"); var sum = ProxyClass.Func(() =\u0026gt; { Console.WriteLine("do some func"); return 5 + 8; }); Console.WriteLine("the result is:" + sum); Console.Read(); } } class ProxyClass { public static T Func\u0026lt;T\u0026gt;(Func\u0026lt;T\u0026gt; func) { Console.WriteLine("function start"); var result = func(); Console.WriteLine("function end"); return result; } public static void Action(Action action) { Console.WriteLine("function start"); action(); Console.WriteLine("function end"); } }
推荐阅读
- 聪明人养花,这3种“花”怎样也要养一盆,每年能省不少医药费
- 北京22家市属医院均开展安检基本实现重点区域安检措施全覆盖
- 长江流域渔民退捕“上岸”实现扩产新致富
- 实现“甜蜜计划”,这对中哈跨国夫妻好甜
- 北京地铁11号线西段三座车站提前实现主体结构封顶
- 互联网怎样解决“家政服务上门速度慢”的问题
- 怎样看待从1月8号起,QQ钱包开始提现收费
- 银行it人怎样转型
- 汽车|冬天怎样让车内温度快速升高?座椅加热的最佳使用方式二,外循环的作用总结
- 怎样进入通信行业
