EventDeclaration TransformAutomaticEvents(CustomEventDeclaration ev)
        {
            if (!ev.PrivateImplementationType.IsNull)
            {
                return(null);
            }
            if (!ev.Modifiers.HasFlag(Modifiers.Abstract))
            {
                if (!CheckAutomaticEventV4(ev) && !CheckAutomaticEventV2(ev) && !CheckAutomaticEventV4MCS(ev))
                {
                    return(null);
                }
            }
            RemoveCompilerGeneratedAttribute(ev.AddAccessor.Attributes, attributeTypesToRemoveFromAutoEvents);
            EventDeclaration ed = new EventDeclaration();

            ev.Attributes.MoveTo(ed.Attributes);
            foreach (var attr in ev.AddAccessor.Attributes)
            {
                attr.AttributeTarget = "method";
                ed.Attributes.Add(attr.Detach());
            }
            ed.ReturnType = ev.ReturnType.Detach();
            ed.Modifiers  = ev.Modifiers;
            ed.Variables.Add(new VariableInitializer(ev.Name));
            ed.CopyAnnotationsFrom(ev);

            IEvent eventDef = ev.GetSymbol() as IEvent;

            if (eventDef != null)
            {
                IField field = eventDef.DeclaringType.GetFields(f => f.Name == ev.Name, GetMemberOptions.IgnoreInheritedMembers).SingleOrDefault();
                if (field != null)
                {
                    ed.AddAnnotation(field);
                    var attributes = field.GetAttributes()
                                     .Where(a => !attributeTypesToRemoveFromAutoEvents.Contains(a.AttributeType.FullName))
                                     .Select(context.TypeSystemAstBuilder.ConvertAttribute).ToArray();
                    if (attributes.Length > 0)
                    {
                        var section = new AttributeSection {
                            AttributeTarget = "field"
                        };
                        section.Attributes.AddRange(attributes);
                        ed.Attributes.Add(section);
                    }
                }
            }

            ev.ReplaceWith(ed);
            return(ed);
        }
        EventDeclaration TransformAutomaticEvents(CustomEventDeclaration ev)
        {
            Match m1 = automaticEventPatternV4.Match(ev.AddAccessor);

            if (!CheckAutomaticEventV4Match(m1, ev, true))
            {
                return(null);
            }
            Match m2 = automaticEventPatternV4.Match(ev.RemoveAccessor);

            if (!CheckAutomaticEventV4Match(m2, ev, false))
            {
                return(null);
            }
            EventDeclaration ed = new EventDeclaration();

            ev.Attributes.MoveTo(ed.Attributes);
            ed.ReturnType = ev.ReturnType.Detach();
            ed.Modifiers  = ev.Modifiers;
            ed.Variables.Add(new VariableInitializer(ev.Name));
            ed.CopyAnnotationsFrom(ev);

            EventDefinition eventDef = ev.Annotation <EventDefinition>();

            if (eventDef != null)
            {
                FieldDefinition field = eventDef.DeclaringType.Fields.FirstOrDefault(f => f.Name == ev.Name);
                if (field != null)
                {
                    ed.AddAnnotation(field);
                    AstBuilder.ConvertAttributes(ed, field, AttributeTarget.Field);
                }
            }

            ev.ReplaceWith(ed);
            return(ed);
        }
Beispiel #3
0
				public override void VisitCustomEventDeclaration (CustomEventDeclaration eventDeclaration)
				{
					base.VisitCustomEventDeclaration (eventDeclaration);

					var fd = new FieldDeclaration {
						Name = eventDeclaration.Name,
						ReturnType = new SimpleType ("NEvent", eventDeclaration.ReturnType.Clone ()),
					};
					fd.Variables.Add (new VariableInitializer (
						eventDeclaration.Name,
						IsInterface (eventDeclaration.GetParent<TypeDeclaration> ()) ? null : new ObjectCreateExpression (new SimpleType ("NEvent", eventDeclaration.ReturnType.Clone ()))));


					eventDeclaration.ReplaceWith (fd);
				}