自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

.NET 4.0的ICustomQueryInterface新特性

開發(fā) 后端
本文介紹了.NET 4.0的命名空間中的ICustomQueryInterface新特性。顧名思義,這個Interface的功能就是使得用戶可以自己控制QueryInterface這個COM最常用的函數(shù)的行為。

在.NET Framework v4.0發(fā)布的新功能中,在名字空間System.Runtime.InteropServices新增加了一個叫做ICustomQueryInterface的Interface, 顧名思義,這個Interface的功能就是使得用戶可以自己控制QueryInterface這個COM最常用的函數(shù)的行為。在v4.0以前,所有作用于托管組件上的QI行為,都是由CLR內(nèi)部的IUnkown:QueryInterface控制的,比如,如果你QI著名的IDispatch接口時,你得到的永遠都是CLR提供的那個IDispatch,諸如此類的還有IMarshal/IProvideClassInfo等一些常用的Interface。如果你非常希望用自己的IDispatch實現(xiàn)來替換clr提供的實現(xiàn),那么恭喜你,ICustomQueryInterface就是為你而生的!當然,ICustomQueryInterface所帶來的,不僅僅是簡單的Interface替換,它甚至可以使得Aggregate托管組件也成為現(xiàn)實,wow,如果你了解Aggregation的話,一定會因此而雀躍不已的。我會在另一篇文章中通過例程給大家做一個詳細的介紹。

讓我們來看看這個ICustomQueryInterface的定義吧:

  1: public interface ICustomQueryInterface
  2: {
  3:     CustomQueryInterfaceResult GetInterface([In]ref Guid iid, out IntPtr ppv);
  4: }
  5: 

是的,就是這么簡單,就一個GetInterface方法,再仔細看看它的方法參數(shù),是不是和c++里面的QueryInterface有點神似啊。哈哈,其實你可以把它理解成QueryInterface的托管實現(xiàn)也無妨啊!不過它還有個小小的功能,就是如果自己不想處理這個QI,就返回NotHandled, clr看到這個返回值,就會調(diào)用自己的QI實現(xiàn)來幫你處理這個請求,爽吧。

讓我們來看看有了這個Interface之后clr內(nèi)部關于QI的處理流程圖吧:

關于QI的處理流程圖
.NET 4.0新特性:ICustomQueryInterface

從這個圖上我們可以看到,除了不能處理對IUnknown的QI請求(要求別太高嘛),其他統(tǒng)統(tǒng)OK!

理論一大堆了,來實戰(zhàn)一下。

看看我們的托管組件的實現(xiàn)

  1: using System;
  2: using System.Runtime.InteropServices;
  3:
  4: namespace States
  5: {   
  6:     [Guid("00020400-0000-0000-C000-000000001147")]
  7:     [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  8:     public interface ICQ
  9:     {
 10:         int func();
 11:         void slot2();
 12:         void slot3();
 13:         void slot4();
 14:     }
 15:
 16:     [Guid("11120400-0000-0000-C000-000000001148")]
 17:     [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
 18:     public interface IA
 19:     {
 20:         int FuncA();
 21:     }
 22:
 23:     [Guid("22220400-0000-0000-C000-000000001149")]
 24:     [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
 25:     public interface IB
 26:     {
 27:         int FuncB();
 28:     }
 29:
 30:
 31:
 32:     [Guid("00020400-0000-0000-C000-000000001150")]
 33:     [ClassInterface(ClassInterfaceType.None)]
 34:     public class StatesComServer : ICustomQueryInterface, ICQ, IA, IB
 35:     {
 36:           public readonly Guid IID_IA = new Guid("11120400-0000-0000-C000-000000001148");
 37:        
 38:           public CustomQueryInterfaceResult GetInterface([In]ref Guid iid, out IntPtr intf)
 39:           {
 40:                 if (iid == WellKnownGuids.IID_IDispatch)
 41:                 {
 42:                     intf = Marshal.GetComInterfaceForObject(this, typeof(ICQ), CustomQueryInterfaceMode.Ignore);
 43:                     return CustomQueryInterfaceResult.Handled;
 44:                 }
 45:
 46:                 if (iid == IID_IA)
 47:                 {
 48:                     intf = IntPtr.Zero;
 49:                     return CustomQueryInterfaceResult.Failed;
 50:                 }
 51:
 52:                 intf = IntPtr.Zero;
 53:                 return CustomQueryInterfaceResult.NotHandled;
 54:           }
 55:
 56:           public int func()
 57:           {
 58:               Console.WriteLine("This is Interface ICQ, not the IDispatch!!!");
 59:                return 2008;
 60:           }
 61:
 62:           public int FuncA()
 63:           {
 64:               Console.WriteLine("This is Interface IA!!!");
 65:               return 3008;
 66:           }
 67:
 68:           public int FuncB()
 69:           {
 70:               Console.WriteLine("This is Interface IB!!!");
 71:               return 4008;
 72:           }
 73:
 74:
 75:         #region Empty Functions
 76:           public void slot2() { }
 77:           public void slot3() { }
 78:           public void slot4() { }
 79:         #endregion
 80:     }
 81:
 82: }
 83:

這里稍微做個解釋,GetInterface的返回值如果是CustomQueryInterfaceResult.Failed,意思是QI失敗。CustomQueryInterfaceResult.NotHandled意思是讓clr去處理這個請求,CustomQueryInterfaceResult.Handled是告訴clr,已經(jīng)處理好了,指針值保存在intf里面,直接返回給用戶就可以了。

再來看看我們的客戶端

  1. IDispatch * pDisp = NULL;  
  2. printf("Scenario 1: QI IDispatch interface, Expected the Custom IDispatch interface\n");  
  3. hresult = pUnknown->QueryInterface(IID_IDispatch, (void**)&pDisp);  
  4.  
  5. UINT count  = 0;  
  6. hresult = pDisp->GetTypeInfoCount(&count);  
  7. printf("Return value of GetTypeInfoCount is %d\n", count);    
  8.  
  9. IA * pA = NULL;  
  10. printf("Scenario 2: QI IA interface, Expected failed\n");  
  11. hresult = pUnknown->QueryInterface(IID_IA, (void**)&hresult);  
  12. if (FAILED(hresult))  
  13. {  
  14.   printf("Failed to QI IA with error code %x\n", count);    
  15. }  
  16. IB * pB = NULL;  
  17. printf("Scenario 3: QI IB interface interface, Expected the IB interface\n");  
  18. hresult = pUnknown->QueryInterface(IID_IB, (void**)&pB);  
  19. long i  = 0;  
  20. hresult = pB->FuncB(&i);  
  21.    

再來看看我們的輸出結果。

  1. Scenario 1: QI IDispatch interface, Expected the Custom IDispatch interface 
  2. This is Interface ICQ, not the IDispatch!!!  
  3. Return value of GetTypeInfoCount is 2008  
  4. Scenario 2: QI iA interface, Expected failed  
  5. Failed to QI IA with error code 7d8  
  6. Scenario 3: QI IB interface interface, Expected the IB interface 
  7. This is Interface IB!!!  
  8.  

以上就介紹了.NET 4.0的命名空間中的ICustomQueryInterface新特性。

【編輯推薦】

  1. C#數(shù)據(jù)庫查詢和操作大全
  2. 簡單介紹C#預處理
  3. 學習C#無詞尾符號經(jīng)驗談
  4. C#調(diào)用QuickTest自動化
  5. 詳解C#正規(guī)表達式
責任編輯:yangsai 來源: CSDN博客
相關推薦

2009-09-04 16:28:05

ASP.NET 4.0

2009-12-30 10:21:36

.NET 4.0

2010-01-05 09:26:13

.NET 4.0

2010-02-24 14:24:35

.NET 4.0

2009-08-18 09:37:42

ASP.NET 4.0

2009-07-06 11:00:56

.NET 4.0新特性.NET

2018-11-15 14:05:24

MongoDB數(shù)據(jù)庫事務

2009-08-19 16:51:14

C# 4.0 dyna

2009-05-26 09:28:22

C# 4.0dynamic動態(tài)類型

2009-07-30 13:17:44

STM.NET.NET 4 Beta

2012-01-09 16:00:56

2009-05-26 11:15:31

C# 4.0dynamicVisual Stud

2009-07-30 14:55:43

ASP.NET 2.0

2010-08-17 09:57:39

C#

2009-11-04 14:17:34

ADO.NET 2.0

2009-03-12 11:26:35

Data ServicADO.NET.NET

2009-11-03 09:37:33

VB.NET重載

2009-11-04 15:44:39

ADO.NET Sql

2011-01-15 23:07:59

2011-01-14 10:27:18

C#.netasp.net
點贊
收藏

51CTO技術棧公眾號