2010-02-15 72 views
0

我正在關注this article來通過COM註冊SENS事件,但我想我錯過了一些東西。我打電話SubscribeToEvents方法的文章說寫,像這樣:在.NET中捕捉SENS事件,沒有Guid屬性

EventSystemRegistrar.SubscribeToEvents("ManagedSENS EventSubscriber", "ManagedSENS.SensLogonInterop", subscriptionViewerID, this, typeof(SensLogon)); 

導致這種方法獲取調用:

private static String GetInterfaceGuid(Type type) 
{ 
    Object[] attributes = type.GetCustomAttributes(typeof(GuidAttribute), true); 

    return String.Format("{{{0}}}", ((GuidAttribute)attributes[0]).Value); 
} 

的問題是,該類型存在SensLogon他們班建議寫作,但它沒有任何屬性,所以該方法拋出一個異常。唯一的屬性,這是,事實上,GuidAttributes,他們說寫上這些課,什麼都沒有(至少據我可以告訴)做的SensLogon類:

[ComImport, Guid("4E14FBA2-2E22-11D1-9964-00C04FBBB345")] 
class EventSystem { } 
[ComImport, Guid("7542E960-79C7-11D1-88F9-0080C7D771BF")] 
class EventSubcription { } 
[ComImport, Guid("AB944620-79C6-11d1-88F9-0080C7D771BF")] 
class EventPublisher { } 
[ComImport, Guid("cdbec9c0-7a68-11d1-88f9-0080c7d771bf")] 
class EventClass { } 

也許我在這裏錯過了什麼?我是從這些課程或其他東西中派生出來的嗎?顯示SensLogon類,但它沒有任何這些屬性。

有沒有人做過類似的事情來註冊COM事件,或者可以,也許可以看到我在哪裏不正確地跟着文章?

+0

請不要讓我們猜測例外。 – 2010-02-15 23:12:59

+0

您確實在http://www.codeproject.com/KB/cs/subscriptionviewer.aspx上閱讀過? – t0mm13b 2010-02-15 23:32:32

+0

異常的類型並不重要,但它是一個IndexOutOfRange異常,因爲沒有GuidAttribute類型的屬性。缺乏這些屬性是問題,而不是例外。我確實看過那篇文章,但它的範圍似乎遠遠超出了我想要做的事情,它要複雜得多。我不知道該如何應用於我目前正在做的事情。這裏是我寫的兩個類:http://codepaste.net/x3xfsj和http://codepaste.net/khdefj,如果這清除了我可能會錯過的東西。 – 2010-02-16 00:18:56

回答

0

我想通了。我將typeof(SensLogon)傳入EventSystemRegistrar.SubscribeToEvents,當時我應該傳遞typeof(ISensLogon)(ISensLogon的確有一個GuidAttribute)。傻我。

1

我覺得你的代碼是不安全的,因爲你承擔調用type.GetCustomAttributes(...)工作沒有檢查....我會包裝這個在try/catch塊,看看發生了什麼......並檢查異常.. 。

 
private static String GetInterfaceGuid(Type type) 
{ 
    string sGuid = string.Empty; 
    try{ 
     Object[] attributes = type.GetCustomAttributes(typeof(GuidAttribute), true); 
     if (attributes != null && attributes.Length >= 1){ 
      sGuid = String.Format("{{{0}}}", ((GuidAttribute)attributes[0]).Value); 
     }else{ 
      // FAIL! 
     } 
    }catch(System.Exception up){ 
     throw up; 
    } 
    return sGuid; 
} 

ess.dll難道根本沒有註冊?您可能需要手動註冊它?檢查註冊表中的HKEY_CLASSES_ROOT下的類ID,看看typelib ID ...如果他們不在那裏,然後發出這個regsvr32 ess.dll哪裏 - 該DLL文件位於當前文件夾中。

希望這會有所幫助, 最好的問候, 湯姆。

+0

謝謝。我一定會抓住這個和其他例外。 – 2010-02-16 01:23:37