| 1 |
// |
| 2 |
// EventDefinition.cs |
| 3 |
// |
| 4 |
// Author: |
| 5 |
// Jb Evain ([email protected]) |
| 6 |
// |
| 7 |
// (C) 2005 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 |
namespace Mono.Cecil { |
| 30 |
|
| 31 |
internal 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; |
| 40 |
|
| 41 |
public EventAttributes Attributes { |
| 42 |
get { return m_attributes; } |
| 43 |
set { m_attributes = value; } |
| 44 |
} |
| 45 |
|
| 46 |
public MethodDefinition AddMethod { |
| 47 |
get { return m_addMeth; } |
| 48 |
set { m_addMeth = value; } |
| 49 |
} |
| 50 |
|
| 51 |
public MethodDefinition InvokeMethod { |
| 52 |
get { return m_invMeth; } |
| 53 |
set { m_invMeth = value; } |
| 54 |
} |
| 55 |
|
| 56 |
public MethodDefinition RemoveMethod { |
| 57 |
get { return m_remMeth; } |
| 58 |
set { m_remMeth = value; } |
| 59 |
} |
| 60 |
|
| 61 |
public bool HasCustomAttributes { |
| 62 |
get { return (m_customAttrs == null) ? false : (m_customAttrs.Count > 0); } |
| 63 |
} |
| 64 |
|
| 65 |
public CustomAttributeCollection CustomAttributes { |
| 66 |
get { |
| 67 |
if (m_customAttrs == null) |
| 68 |
m_customAttrs = new CustomAttributeCollection (this); |
| 69 |
|
| 70 |
return m_customAttrs; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
#region EventAttributes |
| 75 |
|
| 76 |
public bool IsSpecialName { |
| 77 |
get { return (m_attributes & EventAttributes.SpecialName) != 0; } |
| 78 |
set { |
| 79 |
if (value) |
| 80 |
m_attributes |= EventAttributes.SpecialName; |
| 81 |
else |
| 82 |
m_attributes &= ~EventAttributes.SpecialName; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
public bool IsRuntimeSpecialName { |
| 87 |
get { return (m_attributes & EventAttributes.RTSpecialName) != 0; } |
| 88 |
set { |
| 89 |
if (value) |
| 90 |
m_attributes |= EventAttributes.RTSpecialName; |
| 91 |
else |
| 92 |
m_attributes &= ~EventAttributes.RTSpecialName; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
#endregion |
| 97 |
|
| 98 |
public new TypeDefinition DeclaringType { |
| 99 |
get { return (TypeDefinition) base.DeclaringType; } |
| 100 |
set { base.DeclaringType = value; } |
| 101 |
} |
| 102 |
|
| 103 |
public EventDefinition (string name, TypeReference eventType, |
| 104 |
EventAttributes attrs) : base (name, eventType) |
| 105 |
{ |
| 106 |
m_attributes = attrs; |
| 107 |
} |
| 108 |
|
| 109 |
public override EventDefinition Resolve () |
| 110 |
{ |
| 111 |
return this; |
| 112 |
} |
| 113 |
|
| 114 |
public static MethodDefinition CreateAddMethod (EventDefinition evt) |
| 115 |
{ |
| 116 |
MethodDefinition add = new MethodDefinition ( |
| 117 |
string.Concat ("add_", evt.Name), (MethodAttributes) 0, evt.EventType); |
| 118 |
evt.AddMethod = add; |
| 119 |
return add; |
| 120 |
} |
| 121 |
|
| 122 |
public static MethodDefinition CreateRemoveMethod (EventDefinition evt) |
| 123 |
{ |
| 124 |
MethodDefinition remove = new MethodDefinition ( |
| 125 |
string.Concat ("remove_", evt.Name), (MethodAttributes) 0, evt.EventType); |
| 126 |
evt.RemoveMethod = remove; |
| 127 |
return remove; |
| 128 |
} |
| 129 |
|
| 130 |
public static MethodDefinition CreateInvokeMethod (EventDefinition evt) |
| 131 |
{ |
| 132 |
MethodDefinition raise = new MethodDefinition ( |
| 133 |
string.Concat ("raise_", evt.Name), (MethodAttributes) 0, evt.EventType); |
| 134 |
evt.InvokeMethod = raise; |
| 135 |
return raise; |
| 136 |
} |
| 137 |
|
| 138 |
public EventDefinition Clone () |
| 139 |
{ |
| 140 |
return Clone (this, new ImportContext (NullReferenceImporter.Instance, this.DeclaringType)); |
| 141 |
} |
| 142 |
|
| 143 |
internal static EventDefinition Clone (EventDefinition evt, ImportContext context) |
| 144 |
{ |
| 145 |
EventDefinition ne = new EventDefinition ( |
| 146 |
evt.Name, |
| 147 |
context.Import (evt.EventType), |
| 148 |
evt.Attributes); |
| 149 |
|
| 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]; |
| 158 |
} |
| 159 |
|
| 160 |
foreach (CustomAttribute ca in evt.CustomAttributes) |
| 161 |
ne.CustomAttributes.Add (CustomAttribute.Clone (ca, context)); |
| 162 |
|
| 163 |
return ne; |
| 164 |
} |
| 165 |
|
| 166 |
public override void Accept (IReflectionVisitor visitor) |
| 167 |
{ |
| 168 |
visitor.VisitEventDefinition (this); |
| 169 |
|
| 170 |
this.CustomAttributes.Accept (visitor); |
| 171 |
} |
| 172 |
} |
| 173 |
} |