追赶SENS事件在.NET中,没有属性的Guid
题
我以下这篇文章到注册经由COM SENS事件但我觉得我失去了一些东西。我打电话SubscribeToEvents方法的文章说写,像这样的:
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事件进行注册,还是可以的,也许,看到我已经按照文章不当?
解决方案 2
我计算出来。我是路过的typeof(SensLogon)到EventSystemRegistrar.SubscribeToEvents,当我应该已经通过的typeof(ISensLogon)(ISensLogon确实有GuidAttribute就可以了)。傻我。
其他提示
我觉得你的代码是不安全的,因为你承担了通话而不检查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的注册表,看看类型库ID ...如果它们不存在,然后发出此regsvr32 ess.dll
其中前所未有的dll文件位于当前文件夹中。
希望这有助于 最好的祝福, 汤姆。
不隶属于 StackOverflow