| 1 |
// |
| 2 |
// CustomAttribute.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 |
using System.Collections; |
| 32 |
|
| 33 |
internal sealed class CustomAttribute : IRequireResolving, IAnnotationProvider, IReflectionVisitable { |
| 34 |
|
| 35 |
MethodReference m_ctor; |
| 36 |
IList m_parameters; |
| 37 |
IDictionary m_fields; |
| 38 |
IDictionary m_properties; |
| 39 |
IDictionary m_fieldTypes; |
| 40 |
IDictionary m_propTypes; |
| 41 |
IDictionary m_annotations; |
| 42 |
|
| 43 |
bool m_resolved; |
| 44 |
byte [] m_blob; |
| 45 |
|
| 46 |
public MethodReference Constructor { |
| 47 |
get { return m_ctor; } |
| 48 |
set { m_ctor = value; } |
| 49 |
} |
| 50 |
|
| 51 |
public IList ConstructorParameters { |
| 52 |
get { |
| 53 |
if (m_parameters == null) |
| 54 |
m_parameters = new ArrayList (); |
| 55 |
return m_parameters; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
public IDictionary Fields { |
| 60 |
get { |
| 61 |
if (m_fields == null) |
| 62 |
m_fields = new Hashtable (); |
| 63 |
|
| 64 |
return m_fields; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
public IDictionary Properties { |
| 69 |
get { |
| 70 |
if (m_properties == null) |
| 71 |
m_properties = new Hashtable (); |
| 72 |
|
| 73 |
return m_properties; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
internal IDictionary FieldTypes { |
| 78 |
get { |
| 79 |
if (m_fieldTypes == null) |
| 80 |
m_fieldTypes = new Hashtable (); |
| 81 |
|
| 82 |
return m_fieldTypes; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
internal IDictionary PropertyTypes { |
| 87 |
get { |
| 88 |
if (m_propTypes == null) |
| 89 |
m_propTypes = new Hashtable (); |
| 90 |
|
| 91 |
return m_propTypes; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
public bool Resolved { |
| 96 |
get { return m_resolved; } |
| 97 |
set { m_resolved = value; } |
| 98 |
} |
| 99 |
|
| 100 |
public byte [] Blob { |
| 101 |
get { return m_blob; } |
| 102 |
set { m_blob = value; } |
| 103 |
} |
| 104 |
|
| 105 |
IDictionary IAnnotationProvider.Annotations { |
| 106 |
get { |
| 107 |
if (m_annotations == null) |
| 108 |
m_annotations = new Hashtable (); |
| 109 |
return m_annotations; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
public CustomAttribute (MethodReference ctor) |
| 114 |
{ |
| 115 |
m_ctor = ctor; |
| 116 |
m_resolved = true; |
| 117 |
} |
| 118 |
|
| 119 |
public CustomAttribute (MethodReference ctor, byte [] blob) |
| 120 |
{ |
| 121 |
m_ctor = ctor; |
| 122 |
m_blob = blob; |
| 123 |
} |
| 124 |
|
| 125 |
public TypeReference GetFieldType (string fieldName) |
| 126 |
{ |
| 127 |
return (TypeReference) FieldTypes [fieldName]; |
| 128 |
} |
| 129 |
|
| 130 |
public TypeReference GetPropertyType (string propertyName) |
| 131 |
{ |
| 132 |
return (TypeReference) PropertyTypes [propertyName]; |
| 133 |
} |
| 134 |
|
| 135 |
public void SetFieldType (string fieldName, TypeReference type) |
| 136 |
{ |
| 137 |
FieldTypes [fieldName] = type; |
| 138 |
} |
| 139 |
|
| 140 |
public void SetPropertyType (string propertyName, TypeReference type) |
| 141 |
{ |
| 142 |
PropertyTypes [propertyName] = type; |
| 143 |
} |
| 144 |
|
| 145 |
public CustomAttribute Clone () |
| 146 |
{ |
| 147 |
return Clone (this, new ImportContext (NullReferenceImporter.Instance)); |
| 148 |
} |
| 149 |
|
| 150 |
static void Clone (IDictionary original, IDictionary target) |
| 151 |
{ |
| 152 |
target.Clear (); |
| 153 |
foreach (DictionaryEntry entry in original) |
| 154 |
target.Add (entry.Key, entry.Value); |
| 155 |
} |
| 156 |
|
| 157 |
internal static CustomAttribute Clone (CustomAttribute custattr, ImportContext context) |
| 158 |
{ |
| 159 |
CustomAttribute ca = new CustomAttribute (context.Import (custattr.Constructor)); |
| 160 |
custattr.CopyTo (ca); |
| 161 |
return ca; |
| 162 |
} |
| 163 |
|
| 164 |
void CopyTo (CustomAttribute target) |
| 165 |
{ |
| 166 |
target.Resolved = Resolved; |
| 167 |
if (!Resolved) { |
| 168 |
target.Blob = Blob; |
| 169 |
return; |
| 170 |
} |
| 171 |
|
| 172 |
foreach (object o in ConstructorParameters) |
| 173 |
target.ConstructorParameters.Add (o); |
| 174 |
Clone (Fields, target.Fields); |
| 175 |
Clone (FieldTypes, target.FieldTypes); |
| 176 |
Clone (Properties, target.Properties); |
| 177 |
Clone (PropertyTypes, target.PropertyTypes); |
| 178 |
} |
| 179 |
|
| 180 |
public bool Resolve () |
| 181 |
{ |
| 182 |
if (Resolved) |
| 183 |
return true; |
| 184 |
|
| 185 |
ReflectionReader r = m_ctor.DeclaringType.Module.Controller.Reader; |
| 186 |
CustomAttribute newCa = r.GetCustomAttribute (m_ctor, Blob, true); |
| 187 |
if (!newCa.Resolved) |
| 188 |
return false; |
| 189 |
|
| 190 |
newCa.CopyTo (this); |
| 191 |
return true; |
| 192 |
} |
| 193 |
|
| 194 |
public void Accept (IReflectionVisitor visitor) |
| 195 |
{ |
| 196 |
visitor.VisitCustomAttribute (this); |
| 197 |
} |
| 198 |
} |
| 199 |
} |