5 // Jb Evain (jbevain@gmail.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 namespace Mono.Cecil {
31 public sealed class EventDefinition : EventReference, IMemberDefinition, ICustomAttributeProvider {
33 EventAttributes m_attributes;
35 CustomAttributeCollection m_customAttrs;
37 MethodDefinition m_addMeth;
38 MethodDefinition m_invMeth;
39 MethodDefinition m_remMeth;
41 public EventAttributes Attributes {
42 get { return m_attributes; }
43 set { m_attributes = value; }
46 public MethodDefinition AddMethod {
47 get { return m_addMeth; }
48 set { m_addMeth = value; }
51 public MethodDefinition InvokeMethod {
52 get { return m_invMeth; }
53 set { m_invMeth = value; }
56 public MethodDefinition RemoveMethod {
57 get { return m_remMeth; }
58 set { m_remMeth = value; }
61 public bool HasCustomAttributes {
62 get { return (m_customAttrs == null) ? false : (m_customAttrs.Count > 0); }
65 public CustomAttributeCollection CustomAttributes {
67 if (m_customAttrs == null)
68 m_customAttrs = new CustomAttributeCollection (this);
74 #region EventAttributes
76 public bool IsSpecialName {
77 get { return (m_attributes & EventAttributes.SpecialName) != 0; }
80 m_attributes |= EventAttributes.SpecialName;
82 m_attributes &= ~EventAttributes.SpecialName;
86 public bool IsRuntimeSpecialName {
87 get { return (m_attributes & EventAttributes.RTSpecialName) != 0; }
90 m_attributes |= EventAttributes.RTSpecialName;
92 m_attributes &= ~EventAttributes.RTSpecialName;
98 public new TypeDefinition DeclaringType {
99 get { return (TypeDefinition) base.DeclaringType; }
100 set { base.DeclaringType = value; }
103 public EventDefinition (string name, TypeReference eventType,
104 EventAttributes attrs) : base (name, eventType)
106 m_attributes = attrs;
109 public override EventDefinition Resolve ()
114 public static MethodDefinition CreateAddMethod (EventDefinition evt)
116 MethodDefinition add = new MethodDefinition (
117 string.Concat ("add_", evt.Name), (MethodAttributes) 0, evt.EventType);
122 public static MethodDefinition CreateRemoveMethod (EventDefinition evt)
124 MethodDefinition remove = new MethodDefinition (
125 string.Concat ("remove_", evt.Name), (MethodAttributes) 0, evt.EventType);
126 evt.RemoveMethod = remove;
130 public static MethodDefinition CreateInvokeMethod (EventDefinition evt)
132 MethodDefinition raise = new MethodDefinition (
133 string.Concat ("raise_", evt.Name), (MethodAttributes) 0, evt.EventType);
134 evt.InvokeMethod = raise;
138 public EventDefinition Clone ()
140 return Clone (this, new ImportContext (NullReferenceImporter.Instance, this.DeclaringType));
143 internal static EventDefinition Clone (EventDefinition evt, ImportContext context)
145 EventDefinition ne = new EventDefinition (
147 context.Import (evt.EventType),
150 if (context.GenericContext.Type is TypeDefinition) {
151 TypeDefinition type = context.GenericContext.Type as TypeDefinition;
152 if (evt.AddMethod != null)
153 ne.AddMethod = type.Methods.GetMethod (evt.AddMethod.Name) [0];
154 if (evt.InvokeMethod != null)
155 ne.InvokeMethod = type.Methods.GetMethod (evt.InvokeMethod.Name) [0];
156 if (evt.RemoveMethod != null)
157 ne.RemoveMethod = type.Methods.GetMethod (evt.RemoveMethod.Name) [0];
160 foreach (CustomAttribute ca in evt.CustomAttributes)
161 ne.CustomAttributes.Add (CustomAttribute.Clone (ca, context));
166 public override void Accept (IReflectionVisitor visitor)
168 visitor.VisitEventDefinition (this);
170 this.CustomAttributes.Accept (visitor);