~ubuntu-branches/ubuntu/trusty/mono-addins/trusty-proposed

« back to all changes in this revision

Viewing changes to Mono.Addins.CecilReflector/Mono.Cecil/Mono.Cecil/EventDefinition.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-04-25 11:11:33 UTC
  • mfrom: (4.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20110425111133-t05u5p7o5fxx70fu
Tags: 0.6-2
Upload to Unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
// Author:
5
5
//   Jb Evain (jbevain@gmail.com)
6
6
//
7
 
// (C) 2005 Jb Evain
 
7
// Copyright (c) 2008 - 2010 Jb Evain
8
8
//
9
9
// Permission is hereby granted, free of charge, to any person obtaining
10
10
// a copy of this software and associated documentation files (the
26
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
27
//
28
28
 
 
29
using Mono.Collections.Generic;
 
30
 
29
31
namespace Mono.Cecil {
30
32
 
31
 
        public sealed class EventDefinition : EventReference, IMemberDefinition, ICustomAttributeProvider {
32
 
 
33
 
                EventAttributes m_attributes;
34
 
 
35
 
                CustomAttributeCollection m_customAttrs;
36
 
 
37
 
                MethodDefinition m_addMeth;
38
 
                MethodDefinition m_invMeth;
39
 
                MethodDefinition m_remMeth;
 
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;
40
43
 
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; }
44
47
                }
45
48
 
46
49
                public MethodDefinition AddMethod {
47
 
                        get { return m_addMeth; }
48
 
                        set { m_addMeth = value; }
 
50
                        get {
 
51
                                if (add_method != null)
 
52
                                        return add_method;
 
53
 
 
54
                                InitializeMethods ();
 
55
                                return add_method;
 
56
                        }
 
57
                        set { add_method = value; }
49
58
                }
50
59
 
51
60
                public MethodDefinition InvokeMethod {
52
 
                        get { return m_invMeth; }
53
 
                        set { m_invMeth = value; }
 
61
                        get {
 
62
                                if (invoke_method != null)
 
63
                                        return invoke_method;
 
64
 
 
65
                                InitializeMethods ();
 
66
                                return invoke_method;
 
67
                        }
 
68
                        set { invoke_method = value; }
54
69
                }
55
70
 
56
71
                public MethodDefinition RemoveMethod {
57
 
                        get { return m_remMeth; }
58
 
                        set { m_remMeth = value; }
59
 
                }
60
 
 
61
 
                public CustomAttributeCollection CustomAttributes {
62
 
                        get {
63
 
                                if (m_customAttrs == null)
64
 
                                        m_customAttrs = new CustomAttributeCollection (this);
65
 
 
66
 
                                return m_customAttrs;
67
 
                        }
 
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)); }
68
117
                }
69
118
 
70
119
                #region EventAttributes
71
120
 
72
121
                public bool IsSpecialName {
73
 
                        get { return (m_attributes & EventAttributes.SpecialName) != 0; }
74
 
                        set {
75
 
                                if (value)
76
 
                                        m_attributes |= EventAttributes.SpecialName;
77
 
                                else
78
 
                                        m_attributes &= ~EventAttributes.SpecialName;
79
 
                        }
 
122
                        get { return attributes.GetAttributes ((ushort) EventAttributes.SpecialName); }
 
123
                        set { attributes = attributes.SetAttributes ((ushort) EventAttributes.SpecialName, value); }
80
124
                }
81
125
 
82
126
                public bool IsRuntimeSpecialName {
83
 
                        get { return (m_attributes & EventAttributes.RTSpecialName) != 0; }
84
 
                        set {
85
 
                                if (value)
86
 
                                        m_attributes |= EventAttributes.RTSpecialName;
87
 
                                else
88
 
                                        m_attributes &= ~EventAttributes.RTSpecialName;
89
 
                        }
 
127
                        get { return attributes.GetAttributes ((ushort) FieldAttributes.RTSpecialName); }
 
128
                        set { attributes = attributes.SetAttributes ((ushort) FieldAttributes.RTSpecialName, value); }
90
129
                }
91
130
 
92
131
                #endregion
93
132
 
94
 
                public EventDefinition (string name, TypeReference eventType,
95
 
                        EventAttributes attrs) : base (name, eventType)
96
 
                {
97
 
                        m_attributes = attrs;
98
 
                }
99
 
 
100
 
                public static MethodDefinition CreateAddMethod (EventDefinition evt)
101
 
                {
102
 
                        MethodDefinition add = new MethodDefinition (
103
 
                                string.Concat ("add_", evt.Name), (MethodAttributes) 0, evt.EventType);
104
 
                        evt.AddMethod = add;
105
 
                        return add;
106
 
                }
107
 
 
108
 
                public static MethodDefinition CreateRemoveMethod (EventDefinition evt)
109
 
                {
110
 
                        MethodDefinition remove = new MethodDefinition (
111
 
                                string.Concat ("remove_", evt.Name), (MethodAttributes) 0, evt.EventType);
112
 
                        evt.RemoveMethod = remove;
113
 
                        return remove;
114
 
                }
115
 
 
116
 
                public static MethodDefinition CreateInvokeMethod (EventDefinition evt)
117
 
                {
118
 
                        MethodDefinition raise = new MethodDefinition (
119
 
                                string.Concat ("raise_", evt.Name), (MethodAttributes) 0, evt.EventType);
120
 
                        evt.InvokeMethod = raise;
121
 
                        return raise;
122
 
                }
123
 
 
124
 
                public EventDefinition Clone ()
125
 
                {
126
 
                        return Clone (this, new ImportContext (NullReferenceImporter.Instance, this.DeclaringType));
127
 
                }
128
 
 
129
 
                internal static EventDefinition Clone (EventDefinition evt, ImportContext context)
130
 
                {
131
 
                        EventDefinition ne = new EventDefinition (
132
 
                                evt.Name,
133
 
                                context.Import (evt.EventType),
134
 
                                evt.Attributes);
135
 
 
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];
144
 
                        }
145
 
 
146
 
                        foreach (CustomAttribute ca in evt.CustomAttributes)
147
 
                                ne.CustomAttributes.Add (CustomAttribute.Clone (ca, context));
148
 
 
149
 
                        return ne;
150
 
                }
151
 
 
152
 
                public override void Accept (IReflectionVisitor visitor)
153
 
                {
154
 
                        visitor.VisitEventDefinition (this);
155
 
 
156
 
                        this.CustomAttributes.Accept (visitor);
 
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));
157
161
                }
158
162
        }
159
163
}