1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
using System;
using System.Security.Permissions;
namespace System.Runtime.InteropServices {
[System.Security.SecuritySafeCritical]
public class ComAwareEventInfo : System.Reflection.EventInfo {
#region private fields
private System.Reflection.EventInfo _innerEventInfo;
#endregion
#region ctor
public ComAwareEventInfo(Type type, string eventName) {
_innerEventInfo = type.GetEvent(eventName);
}
#endregion
#region protected overrides
#if FEATURE_NETCORE
[System.Security.SecuritySafeCritical]
#endif//FEATURE_NETCORE
public override void AddEventHandler(object target, Delegate handler) {
if (Marshal.IsComObject(target)) {
// retrieve sourceIid and dispid
Guid sourceIid;
int dispid;
GetDataForComInvocation(_innerEventInfo, out sourceIid, out dispid);
#if FEATURE_CAS_POLICY
// now validate the caller can call into native and redirect to ComEventHelpers.Combine
SecurityPermission perm = new SecurityPermission(SecurityPermissionFlag.UnmanagedCode);
perm.Demand();
#endif//FEATURE_CAS_POLICY
System.Runtime.InteropServices.ComEventsHelper.Combine(target, sourceIid, dispid, handler);
} else {
// we are dealing with a managed object - just add the delegate through reflection
_innerEventInfo.AddEventHandler(target, handler);
}
}
#if FEATURE_NETCORE
[System.Security.SecuritySafeCritical]
#endif//FEATURE_NETCORE
public override void RemoveEventHandler(object target, Delegate handler) {
if (Marshal.IsComObject(target)) {
// retrieve sourceIid and dispid
Guid sourceIid;
int dispid;
GetDataForComInvocation(_innerEventInfo, out sourceIid, out dispid);
#if FEATURE_CAS_POLICY
// now validate the caller can call into native and redirect to ComEventHelpers.Combine
SecurityPermission perm = new SecurityPermission(SecurityPermissionFlag.UnmanagedCode);
perm.Demand();
#endif//FEATURE_CAS_POLICY
System.Runtime.InteropServices.ComEventsHelper.Remove(target, sourceIid, dispid, handler);
} else {
// we are dealing with a managed object - just add the delegate through relection
_innerEventInfo.RemoveEventHandler(target, handler);
}
}
#endregion
#region public overrides
public override System.Reflection.EventAttributes Attributes {
get { return _innerEventInfo.Attributes; }
}
public override System.Reflection.MethodInfo GetAddMethod(bool nonPublic) {
return _innerEventInfo.GetAddMethod(nonPublic);
}
public override System.Reflection.MethodInfo GetRaiseMethod(bool nonPublic) {
return _innerEventInfo.GetRaiseMethod(nonPublic);
}
public override System.Reflection.MethodInfo GetRemoveMethod(bool nonPublic) {
return _innerEventInfo.GetRemoveMethod(nonPublic);
}
public override Type DeclaringType {
get { return _innerEventInfo.DeclaringType; }
}
public override object[] GetCustomAttributes(Type attributeType, bool inherit) {
return _innerEventInfo.GetCustomAttributes(attributeType, inherit);
}
public override object[] GetCustomAttributes(bool inherit) {
return _innerEventInfo.GetCustomAttributes(inherit);
}
public override bool IsDefined(Type attributeType, bool inherit) {
return _innerEventInfo.IsDefined(attributeType, inherit);
}
public override string Name {
get { return _innerEventInfo.Name; }
}
public override Type ReflectedType {
get { return _innerEventInfo.ReflectedType; }
}
#endregion
#region private methods
private static void GetDataForComInvocation(System.Reflection.EventInfo eventInfo, out Guid sourceIid, out int dispid) {
object[] comEventInterfaces = eventInfo.DeclaringType.GetCustomAttributes(typeof(ComEventInterfaceAttribute), false);
if (comEventInterfaces == null || comEventInterfaces.Length == 0) {
//
throw new InvalidOperationException("event invocation for COM objects requires interface to be attributed with ComSourceInterfaceGuidAttribute");
}
if (comEventInterfaces.Length > 1) {
//
throw new System.Reflection.AmbiguousMatchException("more than one ComSourceInterfaceGuidAttribute found");
}
Type sourceItf = ((ComEventInterfaceAttribute)comEventInterfaces[0]).SourceInterface;
Guid guid = sourceItf.GUID;
System.Reflection.MethodInfo methodInfo = sourceItf.GetMethod(eventInfo.Name);
Attribute dispIdAttribute = Attribute.GetCustomAttribute(methodInfo, typeof(DispIdAttribute));
if (dispIdAttribute == null) {
//
throw new InvalidOperationException("event invocation for COM objects requires event to be attributed with DispIdAttribute");
}
sourceIid = guid;
dispid = ((DispIdAttribute)dispIdAttribute).Value;
}
#endregion
}
}
|