溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C#中如何使用對(duì)象

發(fā)布時(shí)間:2021-07-20 10:53:32 來源:億速云 閱讀:134 作者:Leah 欄目:編程語(yǔ)言

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)C#中如何使用對(duì)象,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

一、HttpModule

這個(gè)對(duì)象我們經(jīng)常用來進(jìn)行統(tǒng)一的權(quán)限判斷、日志等處理。
例子代碼:

publicclassMyModule:IHttpModule  {  publicvoidInit(HttpApplicationapplication)  {  application.BeginRequest+=newEventHandler(application_BeginRequest);  }   voidapplication_BeginRequest(objectsender,EventArgse)  {  ((HttpApplication)sender).Response.Write("Copyright@Gspring<br/>");  }   publicvoidDispose()  {  }  }

在Init方法中可以注冊(cè)很多application的事件,我們的例子就是在開始請(qǐng)求的時(shí)候加入自己的代碼,將版權(quán)聲明加到頁(yè)面的頭部

二、HttpHandler

這個(gè)對(duì)象經(jīng)常用來加入特殊的后綴所對(duì)應(yīng)的處理程序,比如可以限制.doc的文件只能給某個(gè)權(quán)限的人訪問。
Asp.Net中的Page類就是一個(gè)IHttpHandler的實(shí)現(xiàn)
例子代碼:

publicclassMyHandler:IHttpHandler  {  publicvoidProcessRequest(HttpContextctx)  {  ctx.Response.Write("Copyright@Gspring<br/>");  }  publicboolIsReusable  {  get{returntrue;}  }  }

這個(gè)對(duì)象主要就是ProcessRequest方法,在這個(gè)方法中輸出版權(quán)信息,但同時(shí)也有一個(gè)問題:原來的頁(yè)面不會(huì)被處理,也就是說頁(yè)面中只有版權(quán)聲明了。那么所有的aspx頁(yè)面都不能正常運(yùn)行了

三、HttpHandlerFactory

這個(gè)對(duì)象也可以用來加入特殊的后綴所對(duì)應(yīng)的處理程序,它的功能比HttpHandler要更加強(qiáng)大,在系統(tǒng)的web.config中就是通過注冊(cè)HttpHandlerFactory來實(shí)現(xiàn)aspx頁(yè)面的訪問的。

HttpHandlerFactory是HttpHandler的工廠,通過它來生成不同的HttpHandler對(duì)象。

  1. publicclassMyHandlerFactory:IHttpHandlerFactory  

  2. {  

  3. publicIHttpHandlerGetHandler(HttpContextcontext,stringrequestType,
    stringurl,stringpathTranslated)  

  4. {  

  5. PageHandlerFactoryfactory=(PageHandlerFactory)Activator.
    CreateInstance(typeof(PageHandlerFactory),true);  

  6. IHttpHandlerhandler=factory.GetHandler
    (context,requestType,url,pathTranslated);  

  7.  

  8. //執(zhí)行一些其它操作  

  9. Execute(handler);  

  10.  

  11. returnhandler;  

  12. }  

  13.  

  14. privatevoidExecute(IHttpHandlerhandler)  

  15. {  

  16. if(handlerisPage)  

  17. {  

  18. //可以直接對(duì)Page對(duì)象進(jìn)行操作  

  19. ((Page)handler).PreLoad+=newEventHandler(MyHandlerFactory_PreLoad);  

  20. }  

  21. }  

  22.  

  23. voidMyHandlerFactory_PreLoad(objectsender,EventArgse)  

  24. {  

  25. ((Page)sender).Response.Write("Copyright@Gspring<br/>");  

  26. }  

  27.  

  28. publicvoidReleaseHandler(IHttpHandlerhandler)  

  29. {  

  30. }  

上述就是小編為大家分享的C#中如何使用對(duì)象了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI