private void FillEventInfoAttribute(EventInfo realEventInfo, IList attributes) {
string eventName = realEventInfo.Name;
BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly;
Type currentReflectType = realEventInfo.ReflectedType;
Debug.Assert(currentReflectType != null, "currentReflectType cannot be null");
int depth = 0;
// First, calculate the depth of the object hierarchy. We do this so we can do a single
// object create for an array of attributes.
//
while(currentReflectType != typeof(object)) {
depth++;
currentReflectType = currentReflectType.BaseType;
}
if (depth > 0) {
// Now build up an array in reverse order
//
currentReflectType = realEventInfo.ReflectedType;
object[][] attributeStack = new object[depth][];
while(currentReflectType != typeof(object)) {
// Fill in our member info so we can get at the custom attributes.
//
MemberInfo memberInfo = currentReflectType.GetEvent(eventName, bindingFlags);
// Get custom attributes for the member info.
//
if (memberInfo != null) {
attributeStack[--depth] = DebugTypeDescriptor.GetCustomAttributes(memberInfo);
}
// Ready for the next loop iteration.
//
currentReflectType = currentReflectType.BaseType;
}
// Now trawl the attribute stack so that we add attributes
// from base class to most derived.
//
foreach(object[] attributeArray in attributeStack) {
if (attributeArray != null) {
foreach(object attr in attributeArray) {
if (attr is Attribute) {
attributes.Add(attr);
}
}
}
}
}
}