26
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
using Mono.Collections.Generic;
29
31
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;
33
public sealed class EventDefinition : EventReference, IMemberDefinition {
37
Collection<CustomAttribute> custom_attributes;
39
internal MethodDefinition add_method;
40
internal MethodDefinition invoke_method;
41
internal MethodDefinition remove_method;
42
internal Collection<MethodDefinition> other_methods;
41
44
public EventAttributes Attributes {
42
get { return m_attributes; }
43
set { m_attributes = value; }
45
get { return (EventAttributes) attributes; }
46
set { attributes = (ushort) value; }
46
49
public MethodDefinition AddMethod {
47
get { return m_addMeth; }
48
set { m_addMeth = value; }
51
if (add_method != null)
57
set { add_method = value; }
51
60
public MethodDefinition InvokeMethod {
52
get { return m_invMeth; }
53
set { m_invMeth = value; }
62
if (invoke_method != null)
68
set { invoke_method = value; }
56
71
public MethodDefinition RemoveMethod {
57
get { return m_remMeth; }
58
set { m_remMeth = value; }
61
public CustomAttributeCollection CustomAttributes {
63
if (m_customAttrs == null)
64
m_customAttrs = new CustomAttributeCollection (this);
73
if (remove_method != null)
79
set { remove_method = value; }
82
public bool HasOtherMethods {
84
if (other_methods != null)
85
return other_methods.Count > 0;
88
return !other_methods.IsNullOrEmpty ();
92
public Collection<MethodDefinition> OtherMethods {
94
if (other_methods != null)
99
if (other_methods != null)
100
return other_methods;
102
return other_methods = new Collection<MethodDefinition> ();
106
public bool HasCustomAttributes {
108
if (custom_attributes != null)
109
return custom_attributes.Count > 0;
111
return this.GetHasCustomAttributes (Module);
115
public Collection<CustomAttribute> CustomAttributes {
116
get { return custom_attributes ?? (custom_attributes = this.GetCustomAttributes (Module)); }
70
119
#region EventAttributes
72
121
public bool IsSpecialName {
73
get { return (m_attributes & EventAttributes.SpecialName) != 0; }
76
m_attributes |= EventAttributes.SpecialName;
78
m_attributes &= ~EventAttributes.SpecialName;
122
get { return attributes.GetAttributes ((ushort) EventAttributes.SpecialName); }
123
set { attributes = attributes.SetAttributes ((ushort) EventAttributes.SpecialName, value); }
82
126
public bool IsRuntimeSpecialName {
83
get { return (m_attributes & EventAttributes.RTSpecialName) != 0; }
86
m_attributes |= EventAttributes.RTSpecialName;
88
m_attributes &= ~EventAttributes.RTSpecialName;
127
get { return attributes.GetAttributes ((ushort) FieldAttributes.RTSpecialName); }
128
set { attributes = attributes.SetAttributes ((ushort) FieldAttributes.RTSpecialName, value); }
94
public EventDefinition (string name, TypeReference eventType,
95
EventAttributes attrs) : base (name, eventType)
100
public static MethodDefinition CreateAddMethod (EventDefinition evt)
102
MethodDefinition add = new MethodDefinition (
103
string.Concat ("add_", evt.Name), (MethodAttributes) 0, evt.EventType);
108
public static MethodDefinition CreateRemoveMethod (EventDefinition evt)
110
MethodDefinition remove = new MethodDefinition (
111
string.Concat ("remove_", evt.Name), (MethodAttributes) 0, evt.EventType);
112
evt.RemoveMethod = remove;
116
public static MethodDefinition CreateInvokeMethod (EventDefinition evt)
118
MethodDefinition raise = new MethodDefinition (
119
string.Concat ("raise_", evt.Name), (MethodAttributes) 0, evt.EventType);
120
evt.InvokeMethod = raise;
124
public EventDefinition Clone ()
126
return Clone (this, new ImportContext (NullReferenceImporter.Instance, this.DeclaringType));
129
internal static EventDefinition Clone (EventDefinition evt, ImportContext context)
131
EventDefinition ne = new EventDefinition (
133
context.Import (evt.EventType),
136
if (context.GenericContext.Type is TypeDefinition) {
137
TypeDefinition type = context.GenericContext.Type as TypeDefinition;
138
if (evt.AddMethod != null)
139
ne.AddMethod = type.Methods.GetMethod (evt.AddMethod.Name) [0];
140
if (evt.InvokeMethod != null)
141
ne.InvokeMethod = type.Methods.GetMethod (evt.InvokeMethod.Name) [0];
142
if (evt.RemoveMethod != null)
143
ne.RemoveMethod = type.Methods.GetMethod (evt.RemoveMethod.Name) [0];
146
foreach (CustomAttribute ca in evt.CustomAttributes)
147
ne.CustomAttributes.Add (CustomAttribute.Clone (ca, context));
152
public override void Accept (IReflectionVisitor visitor)
154
visitor.VisitEventDefinition (this);
156
this.CustomAttributes.Accept (visitor);
133
public new TypeDefinition DeclaringType {
134
get { return (TypeDefinition) base.DeclaringType; }
135
set { base.DeclaringType = value; }
138
public override bool IsDefinition {
142
public EventDefinition (string name, EventAttributes attributes, TypeReference eventType)
143
: base (name, eventType)
145
this.attributes = (ushort) attributes;
146
this.token = new MetadataToken (TokenType.Event);
149
void InitializeMethods ()
151
if (add_method != null
152
|| invoke_method != null
153
|| remove_method != null)
156
var module = this.Module;
157
if (!module.HasImage ())
160
module.Read (this, (@event, reader) => reader.ReadMethods (@event));