protected override void PreFilterEvents(System.Collections.IDictionary events) { base.PreFilterEvents(events); object[] list = new string[events.Keys.Count]; int i = 0; foreach (object key in events.Keys) { list[i++] = key; } foreach (object key in list) { EventDescriptor old = (EventDescriptor)events[key]; if (old != null && !old.Attributes.Contains(new UnityFormAttribute())) { List <Attribute> copiedAtts = CopyAttributes(old, typeof(BrowsableAttribute)); copiedAtts.Add(BrowsableAttribute.No); events[key] = TypeDescriptor.CreateEvent(old.ComponentType, old, copiedAtts.ToArray()); } } }
public static AttributeCollection GetRawAttributes(EventInfo eventInfo) { if (eventInfo == null) { throw new ArgumentNullException("eventInfo"); } string name = eventInfo.Name; Type declaringType = eventInfo.DeclaringType.UnderlyingSystemType; Type memberType = eventInfo.EventHandlerType.UnderlyingSystemType; MemberInfoKey memberInfoKey = new MemberInfoKey(name, declaringType.TypeHandle, memberType.TypeHandle); Dictionary <MemberInfoKey, AttributeCollection> attributesCache = DomainTypeDescriptor.EventInfoAttributesCache; AttributeCollection attributeCollection; if (!attributesCache.TryGetValue(memberInfoKey, out attributeCollection)) { lock (attributesCache) { if (!attributesCache.TryGetValue(memberInfoKey, out attributeCollection)) { attributesCache[memberInfoKey] = attributeCollection = TypeDescriptor.CreateEvent(declaringType, name, memberType, null).Attributes; } } } return(attributeCollection); }
/// <summary> /// Allows a designer to filter the set of events the component it is designing will expose through the TypeDescriptor object. /// </summary> protected virtual void PostFilterEvents(IDictionary events) { // If this component is being privately inherited, we need to filter the events to make them read-only. if (events is null) { return; } InheritanceAttribute inheritanceAttribute = InheritanceAttribute; if (inheritanceAttribute is null || !inheritanceAttribute.Equals(InheritanceAttribute.InheritedReadOnly)) { return; } EventDescriptor[] values = new EventDescriptor[events.Values.Count]; events.Values.CopyTo(values, 0); for (int i = 0; i < values.Length; i++) { EventDescriptor evt = values[i]; if (evt != null) { events[evt.Name] = TypeDescriptor.CreateEvent(evt.ComponentType, evt, ReadOnlyAttribute.Yes); } } }
public void GetEventType() { var eventType = typeof(Action); var eventDescriptor = TypeDescriptor.CreateEvent(typeof(DescriptorTestComponent), nameof(DescriptorTestComponent.ActionEvent), eventType, null); Assert.Equal(eventType, eventDescriptor.EventType); }
GetProperties(ITypeDescriptorContext context, object component, System.Attribute[] attributes) { // Obtain an instance of the IEventBindingService. IEventBindingService eventPropertySvc = (IEventBindingService) sp.GetService(typeof(IEventBindingService)); // Return if an IEventBindingService could not be obtained. if (eventPropertySvc == null) { return(new PropertyDescriptorCollection(null)); } // Obtain the events on the component. EventDescriptorCollection events = TypeDescriptor.GetEvents(component, attributes); // Create an array of the events, where each event is assigned // a category matching its type. EventDescriptor[] newEvents = new EventDescriptor[events.Count]; for (int i = 0; i < events.Count; i++) { newEvents[i] = TypeDescriptor.CreateEvent(events[i].ComponentType, events[i], new CategoryAttribute(events[i].EventType.FullName)); } events = new EventDescriptorCollection(newEvents); // Return event properties for the event descriptors. return(eventPropertySvc.GetEventProperties(events)); }
public void GetComponentType() { var componentType = typeof(DescriptorTestComponent); EventDescriptor eventDescriptor = TypeDescriptor.CreateEvent(componentType, nameof(DescriptorTestComponent.ActionEvent), typeof(Action)); Assert.Equal(componentType, eventDescriptor.ComponentType); }
public void CopyConstructorAddsAttribute() { var oldEventDescriptor = TypeDescriptor.CreateEvent(typeof(DescriptorTestComponent), nameof(DescriptorTestComponent.ActionEvent), typeof(Action)); var expectedString = "EventDescriptorTests.CopyConstructorAddsAttribute"; var newAttribute = new DescriptorTestAttribute(expectedString); EventDescriptor newEventDescriptor = TypeDescriptor.CreateEvent(typeof(DescriptorTestComponent), oldEventDescriptor, new[] { newAttribute }); Assert.True(newEventDescriptor.Attributes.Contains(newAttribute)); }
public void RemoveAddedEventHandler() { var component = new DescriptorTestComponent(); Action eventHandler = () => Assert.True(false, "EventDescriptor failed to remove an event handler"); EventDescriptor eventDescriptor = TypeDescriptor.CreateEvent(component.GetType(), nameof(component.ActionEvent), typeof(Action)); eventDescriptor.AddEventHandler(component, eventHandler); eventDescriptor.RemoveEventHandler(component, eventHandler); // component.Event should now have no handler => raising it should throw a NullReferenceException Assert.Throws <NullReferenceException>(() => component.RaiseEvent()); }
public void RaiseAddedEventHandler() { var component = new DescriptorTestComponent(); var eventHandlerWasCalled = false; Action eventHandler = () => eventHandlerWasCalled = true; var eventInfo = typeof(DescriptorTestComponent).GetEvent(nameof(DescriptorTestComponent.ActionEvent)); EventDescriptor eventDescriptor = TypeDescriptor.CreateEvent(component.GetType(), nameof(component.ActionEvent), typeof(Action)); eventDescriptor.AddEventHandler(component, eventHandler); component.RaiseEvent(); Assert.True(eventHandlerWasCalled); }
protected virtual void PostFilterEvents(IDictionary events) { if (this.InheritanceAttribute.Equals(System.ComponentModel.InheritanceAttribute.InheritedReadOnly)) { EventDescriptor[] array = new EventDescriptor[events.Values.Count]; events.Values.CopyTo(array, 0); for (int i = 0; i < array.Length; i++) { EventDescriptor oldEventDescriptor = array[i]; events[oldEventDescriptor.Name] = TypeDescriptor.CreateEvent(oldEventDescriptor.ComponentType, oldEventDescriptor, new Attribute[] { ReadOnlyAttribute.Yes }); } } }
protected override void PreFilterEvents(IDictionary events) { base.PreFilterEvents(events); string[] strArray = new string[] { "BindingContextChanged", "ChangeUICues", "ClientSizeChanged", "EnabledChanged", "FontChanged", "ForeColorChanged", "GiveFeedback", "ImeModeChanged", "Move", "QueryAccessibilityHelp", "Validated", "Validating", "VisibleChanged" }; for (int i = 0; i < strArray.Length; i++) { EventDescriptor oldEventDescriptor = (EventDescriptor)events[strArray[i]]; if (oldEventDescriptor != null) { events[strArray[i]] = TypeDescriptor.CreateEvent(oldEventDescriptor.ComponentType, oldEventDescriptor, new Attribute[] { BrowsableAttribute.No }); } } }
void IDesignerFilter.PostFilterEvents(IDictionary events) { // If this component is being privately inherited, we need to filter the events to make them read-only. if (InheritanceAttribute.Equals(InheritanceAttribute.InheritedReadOnly)) { EventDescriptor[] values = new EventDescriptor[events.Values.Count]; events.Values.CopyTo(values, 0); for (int i = 0; i < values.Length; i++) { EventDescriptor evt = values[i]; events[evt.Name] = TypeDescriptor.CreateEvent(evt.ComponentType, evt, ReadOnlyAttribute.Yes); } } }
protected override void PreFilterEvents(IDictionary events) { // Debug.WriteLine("ObjectListViewDesigner.PreFilterEvents"); base.PreFilterEvents(events); this.designerFilter.PreFilterEvents(events); // Remove the events that don't make sense for an ObjectListView. // See PreFilterProperties() for why we do this dance rather than just remove the event. List <string> unwanted = new List <string>(new string[] { "AfterLabelEdit", "BeforeLabelEdit", "DrawColumnHeader", "DrawItem", "DrawSubItem", "RetrieveVirtualItem", "SearchForVirtualItem", "VirtualItemsSelectionRangeChanged" }); // If we are looking at a TreeListView, remove group related events // since TreeListViews can't show groups if (this.Control is TreeListView) { unwanted.AddRange(new string[] { "AboutToCreateGroups", "AfterCreatingGroups", "BeforeCreatingGroups", "GroupTaskClicked", "GroupExpandingCollapsing", "GroupStateChanged" }); } foreach (string unwantedEvent in unwanted) { EventDescriptor eventDesc = TypeDescriptor.CreateEvent( typeof(ObjectListView), (EventDescriptor)events[unwantedEvent], new BrowsableAttribute(false)); events[unwantedEvent] = eventDesc; } }
/// <summary> /// Filter /// </summary> /// <param name="events"></param> protected override void PreFilterEvents(IDictionary events) { base.PreFilterEvents(events); if (!this.ShouldCodeSerialize) { ICollection collection1 = events.Values; if ((collection1 != null) && (collection1.Count != 0)) { object[] objArray1 = new object[collection1.Count]; collection1.CopyTo(objArray1, 0); for (int num1 = 0; num1 < objArray1.Length; num1++) { EventDescriptor descriptor1 = (EventDescriptor)objArray1[num1]; descriptor1 = TypeDescriptor.CreateEvent(descriptor1.ComponentType, descriptor1, new Attribute[] { BrowsableAttribute.No }); events[descriptor1.Name] = descriptor1; } } } }
protected override void PreFilterEvents(IDictionary events) { base.PreFilterEvents(events); if (!this.ShouldCodeSerializeInternal) { ICollection values = events.Values; if ((values != null) && (values.Count != 0)) { object[] array = new object[values.Count]; values.CopyTo(array, 0); for (int i = 0; i < array.Length; i++) { EventDescriptor oldEventDescriptor = (EventDescriptor)array[i]; oldEventDescriptor = TypeDescriptor.CreateEvent(oldEventDescriptor.ComponentType, oldEventDescriptor, new Attribute[] { BrowsableAttribute.No }); events[oldEventDescriptor.Name] = oldEventDescriptor; } } } }
protected override void PreFilterEvents(IDictionary events) { base.PreFilterEvents(events); if (this.panel.Parent is ToolStripContainer) { string[] strArray = new string[] { "AutoSizeChanged", "BindingContextChanged", "CausesValidationChanged", "ChangeUICues", "DockChanged", "DragDrop", "DragEnter", "DragLeave", "DragOver", "EnabledChanged", "FontChanged", "ForeColorChanged", "GiveFeedback", "ImeModeChanged", "KeyDown", "KeyPress", "KeyUp", "LocationChanged", "MarginChanged", "MouseCaptureChanged", "Move", "QueryAccessibilityHelp", "QueryContinueDrag", "RegionChanged", "Scroll", "Validated", "Validating" }; for (int i = 0; i < strArray.Length; i++) { EventDescriptor oldEventDescriptor = (EventDescriptor)events[strArray[i]]; if (oldEventDescriptor != null) { events[strArray[i]] = TypeDescriptor.CreateEvent(oldEventDescriptor.ComponentType, oldEventDescriptor, new Attribute[] { BrowsableAttribute.No }); } } } }
/// <include file='doc\HtmlControlDesigner.uex' path='docs/doc[@for="HtmlControlDesigner.PreFilterEvents"]/*' /> protected override void PreFilterEvents(IDictionary events) { base.PreFilterEvents(events); if (ShouldCodeSerialize == false) { // hide all the events, if this control isn't going to be serialized to code behind, ICollection eventCollection = events.Values; if ((eventCollection != null) && (eventCollection.Count != 0)) { object[] eventDescriptors = new object[eventCollection.Count]; eventCollection.CopyTo(eventDescriptors, 0); for (int i = 0; i < eventDescriptors.Length; i++) { EventDescriptor eventDesc = (EventDescriptor)eventDescriptors[i]; eventDesc = TypeDescriptor.CreateEvent(eventDesc.ComponentType, eventDesc, BrowsableAttribute.No); events[eventDesc.Name] = eventDesc; } } } }
private void GetDelegateHookupsFromHandlesClauses(Hashtable handlers, CodeTypeDeclaration codeTypeDecl, CodeStatementCollection statements) { CodeDomLoader.StartMark(); // unfortunately, we have to essentially parse the code to find the objects // that we are instantiating to get their types and names // // then we get the default event for each type, and then we walk through our list // of handlers and add statement hookups for the default ones. // // why do we go through all this work? because we need to know which ones are set by // parse time or when we double click on the same control, we'll think we have to add another handler. // Hashtable objs = new Hashtable(); foreach (CodeTypeMember member in codeTypeDecl.Members) { CodeMemberField field = member as CodeMemberField; if (field != null) { objs[field.Name] = field.Type.BaseType; } } // and add our base type... // objs["MyBase"] = codeTypeDecl.BaseTypes[0].BaseType; // again by name because Visual Basic keeps switching back and forth... // objs[codeTypeDecl.Name] = codeTypeDecl.BaseTypes[0].BaseType; // now we have all the created objects, so we walk through the // handlers we've found and see which ones are default handlers for those objects. // ITypeResolutionService typeLoader = provider.TypeLoader; Type baseType = null; if (typeLoader != null) { // now walk through them, checking each to see if we're on a default event // foreach (DictionaryEntry de in handlers) { // get the handler and and the data for this field // string handler = (string)de.Key; int dot = handler.IndexOf('.'); string fieldName = handler.Substring(0, dot); object objInfo = objs[fieldName]; Type fieldType = null; if (objInfo == null) { // this means this thing isn't an IComponent...try to reflect against the base type // if (baseType == null) { baseType = typeLoader.GetType(codeTypeDecl.BaseTypes[0].BaseType, false); } if (baseType != null) { // because of the wonderful magicalness of VB, there isn't actually a field called "button1", it's a // property. so we get to walk all the fields, looking for one who has the neat // little Accessed through property attribute that matches the name of this member. // foreach (FieldInfo field in baseType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) { if (field.Name == fieldName) { fieldType = field.FieldType; break; } object[] fieldAttrs = field.GetCustomAttributes(typeof(AccessedThroughPropertyAttribute), false); if (fieldAttrs != null && fieldAttrs.Length > 0) { AccessedThroughPropertyAttribute propAttr = (AccessedThroughPropertyAttribute)fieldAttrs[0]; // does the property name on the attribute match what we're looking for? // if (propAttr.PropertyName == fieldName) { PropertyInfo fieldProp = baseType.GetProperty(propAttr.PropertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); // now go find the property and get its value // if (fieldProp != null) { MethodInfo getMethod = fieldProp.GetGetMethod(true); if (getMethod != null && !getMethod.IsPrivate) { fieldType = fieldProp.PropertyType; } break; } } } } if (fieldType != null) { objs[fieldName] = fieldType; objInfo = fieldType; } else { // flag failure of this type so we // don't check again. // objs[fieldName] = false; objInfo = false; continue; } } } if (objInfo is string) { // it's the type name, get the default handler from the TypeDescriptor // Type t = typeLoader.GetType((string)objInfo, false); if (t != null) { objs[fieldName] = t; fieldType = t; } } else if (objInfo is Type) { // just grab the handler // fieldType = (Type)objInfo; } else if (objInfo is bool) { // we've failed here before, just give up! // continue; } else { // errr, how'd we get here? // Debug.Fail("Why does the handler data have a '" + objInfo.GetType().FullName + "'?"); continue; } // now that we have a default event, see if the // handler we're currently on, say "button1.Click" matches // what the handles clause for this component would look like. // if (fieldType != null) { string eventName = handler.Substring(dot + 1); // Make sure this is a valid event for this type EventDescriptor eventDesc = TypeDescriptor.GetEvents(fieldType)[eventName]; // (bug 120608) if we got null, we may be hooking up a private interface member. Try to find it. // if (eventDesc == null) { foreach (Type interfaceType in fieldType.GetInterfaces()) { EventInfo eventInfo = interfaceType.GetEvent(eventName); if (eventInfo != null) { eventDesc = TypeDescriptor.CreateEvent(interfaceType, eventName, eventInfo.EventHandlerType); break; } } } Debug.Assert(eventDesc != null, "Handles clause '" + handler + "' found, but type '" + fieldType.FullName + "' does not have an event '" + eventName + "'"); if (eventDesc != null) { CodeMemberMethod method = de.Value as CodeMemberMethod; CodeStatement attachStatement = CreateEventAttachStatement(fieldName == "MyBase" ? null : fieldName, eventDesc, method.Name); attachStatement.UserData[CodeDomXmlProcessor.KeyXmlParsedStatement] = CodeDomXmlProcessor.KeyXmlParsedStatement; statements.Add(attachStatement); } } } } CodeDomLoader.EndMark("Handles clauses to delegate hookups"); }