7a80aa06d822f27492d92930b1571bc1d88301e2
[mono.git] / mcs / class / Mono.Cecil / Mono.Cecil / EventDefinition.cs
1 //
2 // EventDefinition.cs
3 //
4 // Author:
5 //   Jb Evain (jbevain@gmail.com)
6 //
7 // Copyright (c) 2008 - 2010 Jb Evain
8 //
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:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
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.
27 //
28
29 using Mono.Collections.Generic;
30
31 namespace Mono.Cecil {
32
33         public sealed class EventDefinition : EventReference, IMemberDefinition {
34
35                 ushort attributes;
36
37                 Collection<CustomAttribute> custom_attributes;
38
39                 internal MethodDefinition add_method;
40                 internal MethodDefinition invoke_method;
41                 internal MethodDefinition remove_method;
42                 internal Collection<MethodDefinition> other_methods;
43
44                 public EventAttributes Attributes {
45                         get { return (EventAttributes) attributes; }
46                         set { attributes = (ushort) value; }
47                 }
48
49                 public MethodDefinition AddMethod {
50                         get {
51                                 if (add_method != null)
52                                         return add_method;
53
54                                 InitializeMethods ();
55                                 return add_method;
56                         }
57                         set { add_method = value; }
58                 }
59
60                 public MethodDefinition InvokeMethod {
61                         get {
62                                 if (invoke_method != null)
63                                         return invoke_method;
64
65                                 InitializeMethods ();
66                                 return invoke_method;
67                         }
68                         set { invoke_method = value; }
69                 }
70
71                 public MethodDefinition RemoveMethod {
72                         get {
73                                 if (remove_method != null)
74                                         return remove_method;
75
76                                 InitializeMethods ();
77                                 return remove_method;
78                         }
79                         set { remove_method = value; }
80                 }
81
82                 public bool HasOtherMethods {
83                         get {
84                                 if (other_methods != null)
85                                         return other_methods.Count > 0;
86
87                                 InitializeMethods ();
88                                 return !other_methods.IsNullOrEmpty ();
89                         }
90                 }
91
92                 public Collection<MethodDefinition> OtherMethods {
93                         get {
94                                 if (other_methods != null)
95                                         return other_methods;
96
97                                 InitializeMethods ();
98
99                                 if (other_methods != null)
100                                         return other_methods;
101
102                                 return other_methods = new Collection<MethodDefinition> ();
103                         }
104                 }
105
106                 public bool HasCustomAttributes {
107                         get {
108                                 if (custom_attributes != null)
109                                         return custom_attributes.Count > 0;
110
111                                 return this.GetHasCustomAttributes (Module);
112                         }
113                 }
114
115                 public Collection<CustomAttribute> CustomAttributes {
116                         get { return custom_attributes ?? (custom_attributes = this.GetCustomAttributes (Module)); }
117                 }
118
119                 #region EventAttributes
120
121                 public bool IsSpecialName {
122                         get { return attributes.GetAttributes ((ushort) EventAttributes.SpecialName); }
123                         set { attributes = attributes.SetAttributes ((ushort) EventAttributes.SpecialName, value); }
124                 }
125
126                 public bool IsRuntimeSpecialName {
127                         get { return attributes.GetAttributes ((ushort) FieldAttributes.RTSpecialName); }
128                         set { attributes = attributes.SetAttributes ((ushort) FieldAttributes.RTSpecialName, value); }
129                 }
130
131                 #endregion
132
133                 public new TypeDefinition DeclaringType {
134                         get { return (TypeDefinition) base.DeclaringType; }
135                         set { base.DeclaringType = value; }
136                 }
137
138                 public override bool IsDefinition {
139                         get { return true; }
140                 }
141
142                 public EventDefinition (string name, EventAttributes attributes, TypeReference eventType)
143                         : base (name, eventType)
144                 {
145                         this.attributes = (ushort) attributes;
146                         this.token = new MetadataToken (TokenType.Event);
147                 }
148
149                 void InitializeMethods ()
150                 {
151                         if (add_method != null
152                                 || invoke_method != null
153                                 || remove_method != null)
154                                 return;
155
156                         var module = this.Module;
157                         if (!module.HasImage ())
158                                 return;
159
160                         module.Read (this, (@event, reader) => reader.ReadMethods (@event));
161                 }
162         }
163 }