///IMethodInterceptor對方法進行截獲并加入預處理和后處理。///publicinterfaceIMethodInterceptor{voidPreProcess(InterceptedMethodmethod);vo" />

黄色网页视频 I 影音先锋日日狠狠久久 I 秋霞午夜毛片 I 秋霞一二三区 I 国产成人片无码视频 I 国产 精品 自在自线 I av免费观看网站 I 日本精品久久久久中文字幕5 I 91看视频 I 看全色黄大色黄女片18 I 精品不卡一区 I 亚洲最新精品 I 欧美 激情 在线 I 人妻少妇精品久久 I 国产99视频精品免费专区 I 欧美影院 I 欧美精品在欧美一区二区少妇 I av大片网站 I 国产精品黄色片 I 888久久 I 狠狠干最新 I 看看黄色一级片 I 黄色精品久久 I 三级av在线 I 69色综合 I 国产日韩欧美91 I 亚洲精品偷拍 I 激情小说亚洲图片 I 久久国产视频精品 I 国产综合精品一区二区三区 I 色婷婷国产 I 最新成人av在线 I 国产私拍精品 I 日韩成人影音 I 日日夜夜天天综合

使用動態(tài)代理,提高工作效率

系統(tǒng) 2149 0
動態(tài)代理的一個最主要的應用場合就是實現(xiàn)AOP - 截獲方法調(diào)用,加入自己的預處理、后處理或Around處理。
我在ESBasic.Emit中實現(xiàn)了對這些截獲的支持。
首先,介紹兩個截獲者:
/// <summary>
/// IMethodInterceptor對方法進行截獲并加入預處理和后處理。
/// </summary>
public interface IMethodInterceptor
{
void PreProcess( InterceptedMethod method);

void PostProcess( InterceptedMethod method, object returnVal);
}
IMethodInterceptor 很容易理解,用于為截獲插入預處理、后處理。這個非常容易理解。
/// <summary>
/// IAroundInterceptor對方法進行Around截獲處理。注意,必須要觸發(fā)目標方法的調(diào)用。
/// </summary>
public interface IAroundInterceptor
{
object AroundCall( InterceptedMethod method);
}
Around處理由IAroundInterceptor完成。什么是Around處理了?比如,我們想捕獲目標方法的異常,需要使用TryCatch將目標方法Around起來,這就是Around處理的一個例子。
在上面的接口方法中都有一個參數(shù)InterceptedMethod,它封裝了被截獲的目標方法的基本信息。
public sealed class InterceptedMethod
{
#region Ctor
public InterceptedMethod(){}
public InterceptedMethod( object _target,MethodInfo_method, object []paras)
{
this .target = _target;
this .method = _method;
this .arguments = paras;
}
#endregion

#region Method
private MethodInfomethod;
/// <summary>
/// Method被截獲的目標方法
/// </summary>
public MethodInfo Method
{
get { return method;}
set {method = value;}
}
#endregion

#region Target
private object target;
/// <summary>
/// Target被截獲的方法需要在哪個對象上調(diào)用。
/// </summary>
public object Target
{
get { return target;}
set {target = value;}
}
#endregion

#region Arguments
private object []arguments;
/// <summary>
/// Arguments調(diào)用被截獲的方法的參數(shù)
/// </summary>
public object []Arguments
{
get { return arguments;}
set {arguments = value;}
}
#endregion

#region Invoke
/// <summary>
/// Invoke執(zhí)行目標方法
/// </summary>
public object Invoke()
{
return this .method.Invoke( this .target, this .arguments);
}
#endregion
}

好,如何使用ESBasic.Emit來創(chuàng)建動態(tài)代理了?很簡單,你只需要調(diào)用
public static TInterfaceCreateAopProxy < TInterface > ( object origin, IMethodInterceptor methodInterceptor, IAroundInterceptor aroundInterceptor)
如果不需要某種截獲處理,對應的參數(shù)傳入null即可。
舉個例子,我們需要使用動態(tài)代理截獲IComputer所有方法調(diào)用拋出的異常,并記錄日志。IComputer定義如下:
public interface IComputer
{
int Add( int a, int b);
}

public class Computer: IComputer
{
public int Add( int a, int b)
{
throw new Exception( " TestException " );
return a + b;
}
}
我可以使用Around截獲者來截獲異常,所以我實現(xiàn)一個自己的IAroundInterceptor:
public class ExceprionAroundInterceptor : IAroundInterceptor
{
#region IAroundInterceptor成員
public object AroundCall( InterceptedMethod method)
{
try
{
return method.Invoke();
}
catch (Exceptionee)
{
ee
= ee;
// ..logException

throw ;
}
}
#endregion
}
現(xiàn)在,我們可以這樣獲得針對IComputer的動態(tài)代理的引用:
IComputer proxy = ESBasic.Emit.Application. DynamicProxyFactory .CreateAopProxy < IComputer > ( new Computer (), null , new ExceprionAroundInterceptor ());
proxy.Add(
1 , 2 );
這樣就ok了,Add方法拋出的異常會被 ExceprionAroundInterceptor 截獲。

最后,提醒一下,如果你讓Computer不從IComputer繼承,運行結果也一樣。你應該已經(jīng)看到 DynamicProxyFactory .CreateAopProxy方法的第一個參數(shù)是object類型,而不是泛型T,這說明只要對應的object包含了和目標接口一樣的方法(簽名完全一致)就可以 -- 這就是所謂的“換臉”能力,可以在 這里 看到更詳細的說明。
由于動態(tài)代理是使用Emit發(fā)射實現(xiàn),所以效率上來說,和你手寫的代碼是沒有區(qū)別的。

下載 ESBasic.Emit.dll。













使用動態(tài)代理,提高工作效率


更多文章、技術交流、商務合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論