C# 正在捕获.NET中的SENS事件,没有Guid属性
我正在通过COM注册SENS事件,但我想我遗漏了一些东西。我调用文章中提到的SubscribeToEvents方法来编写,如下所示:C# 正在捕获.NET中的SENS事件,没有Guid属性,c#,.net,events,com,sens,C#,.net,Events,Com,Sens,我正在通过COM注册SENS事件,但我想我遗漏了一些东西。我调用文章中提到的SubscribeToEvents方法来编写,如下所示: EventSystemRegistrar.SubscribeToEvents("ManagedSENS EventSubscriber", "ManagedSENS.SensLogonInterop", subscriptionViewerID, this, typeof(SensLogon)); 这导致调用此方法: private static String
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类无关(至少据我所知):
也许我遗漏了什么?我是从这些课程中获得的还是什么?显示了SensLogon类,但它没有任何这些属性
有没有人做过类似的事情来注册COM事件,或者,也许可以看到我不正确地阅读了这篇文章?我认为您的代码是不安全的,因为您假设调用了
type.GetCustomAttributes(…)
在未检查的情况下工作…我会将其包装在尝试/捕获
块中,以查看发生了什么…并检查异常
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;
}
私有静态字符串GetInterfaceGuid(类型)
{
string sGuid=string.Empty;
试一试{
Object[]attributes=type.GetCustomAttributes(typeof(GuidAttribute),true);
if(attributes!=null&&attributes.Length>=1){
sGuid=String.Format(“{{{0}}},((GuidAttribute)attributes[0]).Value);
}否则{
//失败!
}
}捕获(System.Exception up){
呕吐;
}
返回sGuid;
}
ess.dll
是否已注册?您可能需要手动注册它?检查注册表中HKEY_CLASSES_ROOT下的类id,查看typelib id…如果它们不存在,则在当前文件夹中dll文件所在的位置发出此regsvr32 ess.dll
希望这有帮助,
顺致敬意,
汤姆。我想出来了。我正在将typeof(SensLogon)传递给eventsystemregister.SubscribeToEvents,而我本应该传递typeof(ISensLogon)(ISensLogon确实有一个GuidAttribute)。愚蠢的我。请不要让我们猜测异常。您确实读过吗?异常的类型并不重要,但它是一个IndexOutfrange异常,因为没有GuidAttribute类型的属性。缺少这些属性是问题所在,而不是导致的例外。我确实看过那篇文章,但它的范围似乎远远超出了我要做的,它要复杂得多。我不确定我现在正在做什么。下面是我写的两个类:如果这能澄清我可能遗漏的内容,Mike,你能重新发布你的snipit codepaste.net/x3xfsj,因为你发布的原始代码不起作用。我一直在关注你关注的同一篇文章[这个链接][1],我运气不太好。我真的很想有一个好的SENS dll,我可以为我的几个项目工作使用。如果您能提供任何帮助,我们将不胜感激。或者,如果您在最初发布本文后更新了代码,我很想看看新版本。[1] :给你,还有。我希望有帮助!谢谢我一定会抓住这一点和其他例外。
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;
}