1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
|
//
// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//
// System.Reflection.Emit/CustomAttributeBuilder.cs
//
// Author:
// Paolo Molaro (lupus@ximian.com)
//
// (C) 2001 Ximian, Inc. http://www.ximian.com
//
#if MONO_FEATURE_SRE
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace System.Reflection.Emit {
#if !MOBILE
[ComVisible (true)]
[ComDefaultInterface (typeof (_CustomAttributeBuilder))]
[ClassInterface (ClassInterfaceType.None)]
partial class CustomAttributeBuilder : _CustomAttributeBuilder
{
void _CustomAttributeBuilder.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
{
throw new NotImplementedException ();
}
void _CustomAttributeBuilder.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
{
throw new NotImplementedException ();
}
void _CustomAttributeBuilder.GetTypeInfoCount (out uint pcTInfo)
{
throw new NotImplementedException ();
}
void _CustomAttributeBuilder.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
{
throw new NotImplementedException ();
}
}
#endif
[StructLayout (LayoutKind.Sequential)]
public partial class CustomAttributeBuilder {
ConstructorInfo ctor;
byte[] data;
object [] args;
PropertyInfo [] namedProperties;
object [] propertyValues;
FieldInfo [] namedFields;
object [] fieldValues;
internal ConstructorInfo Ctor {
get {return ctor;}
}
internal byte[] Data {
get {return data;}
}
[MethodImplAttribute(MethodImplOptions.InternalCall)]
static extern byte[] GetBlob(Assembly asmb, ConstructorInfo con, object[] constructorArgs, PropertyInfo[] namedProperties, object[] propertyValues, FieldInfo[] namedFields, object[] fieldValues);
internal object Invoke ()
{
object result = ctor.Invoke (args);
for (int i=0; i < namedFields.Length; i++)
namedFields [i].SetValue (result, fieldValues [i]);
for (int i=0; i < namedProperties.Length; i++)
namedProperties [i].SetValue (result, propertyValues [i]);
return result;
}
internal CustomAttributeBuilder( ConstructorInfo con, byte[] binaryAttribute) {
if (con == null)
throw new ArgumentNullException ("con");
if (binaryAttribute == null)
throw new ArgumentNullException ("binaryAttribute");
ctor = con;
data = (byte[])binaryAttribute.Clone ();
/* should we check that the user supplied data is correct? */
}
public CustomAttributeBuilder( ConstructorInfo con, object[] constructorArgs)
{
Initialize (con, constructorArgs, new PropertyInfo [0], new object [0],
new FieldInfo [0], new object [0]);
}
public CustomAttributeBuilder( ConstructorInfo con, object[] constructorArgs,
FieldInfo[] namedFields, object[] fieldValues)
{
Initialize (con, constructorArgs, new PropertyInfo [0], new object [0],
namedFields, fieldValues);
}
public CustomAttributeBuilder( ConstructorInfo con, object[] constructorArgs,
PropertyInfo[] namedProperties, object[] propertyValues)
{
Initialize (con, constructorArgs, namedProperties, propertyValues, new FieldInfo [0],
new object [0]);
}
public CustomAttributeBuilder( ConstructorInfo con, object[] constructorArgs,
PropertyInfo[] namedProperties, object[] propertyValues,
FieldInfo[] namedFields, object[] fieldValues)
{
Initialize (con, constructorArgs, namedProperties, propertyValues, namedFields, fieldValues);
}
private bool IsValidType (Type t)
{
/* FIXME: Add more checks */
if (t.IsArray && t.GetArrayRank () > 1)
return false;
if (t is TypeBuilder && t.IsEnum) {
// Check that the enum is properly constructed, the unmanaged code
// depends on this
Enum.GetUnderlyingType (t);
}
if (t.IsClass && !(t.IsArray || t == typeof (object) || t == typeof (Type) || t == typeof (string) || t.Assembly.GetName ().Name == "mscorlib"))
return false;
if (t.IsValueType && !(t.IsPrimitive || t.IsEnum || ((t.Assembly is AssemblyBuilder) && t.Assembly.GetName ().Name == "mscorlib")))
return false;
return true;
}
private bool IsValidParam (object o, Type paramType)
{
Type t = o.GetType ();
if (!IsValidType (t))
return false;
if (paramType == typeof (object)) {
if (t.IsArray && t.GetArrayRank () == 1)
return IsValidType (t.GetElementType ());
if (!t.IsPrimitive && !typeof (Type).IsAssignableFrom (t) && t != typeof (string) && !t.IsEnum)
return false;
}
return true;
}
static bool IsValidValue (Type type, object value) {
if (type.IsValueType && value == null)
return false;
if (type.IsArray && type.GetElementType ().IsValueType) {
foreach (var v in (Array)value) {
if (v == null)
return false;
}
}
return true;
}
private void Initialize (ConstructorInfo con, object [] constructorArgs,
PropertyInfo [] namedProperties, object [] propertyValues,
FieldInfo [] namedFields, object [] fieldValues)
{
ctor = con;
args = constructorArgs;
this.namedProperties = namedProperties;
this.propertyValues = propertyValues;
this.namedFields = namedFields;
this.fieldValues = fieldValues;
if (con == null)
throw new ArgumentNullException ("con");
if (constructorArgs == null)
throw new ArgumentNullException ("constructorArgs");
if (namedProperties == null)
throw new ArgumentNullException ("namedProperties");
if (propertyValues == null)
throw new ArgumentNullException ("propertyValues");
if (namedFields == null)
throw new ArgumentNullException ("namedFields");
if (fieldValues == null)
throw new ArgumentNullException ("fieldValues");
if (con.GetParametersCount () != constructorArgs.Length)
throw new ArgumentException ("Parameter count does not match " +
"passed in argument value count.");
if (namedProperties.Length != propertyValues.Length)
throw new ArgumentException ("Array lengths must be the same.",
"namedProperties, propertyValues");
if (namedFields.Length != fieldValues.Length)
throw new ArgumentException ("Array lengths must be the same.",
"namedFields, fieldValues");
if ((con.Attributes & MethodAttributes.Static) == MethodAttributes.Static ||
(con.Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Private)
throw new ArgumentException ("Cannot have private or static constructor.");
Type atype = ctor.DeclaringType;
int i;
i = 0;
foreach (FieldInfo fi in namedFields) {
Type t = fi.DeclaringType;
if ((atype != t) && (!t.IsSubclassOf (atype)) && (!atype.IsSubclassOf (t)))
throw new ArgumentException ("Field '" + fi.Name + "' does not belong to the same class as the constructor");
if (!IsValidType (fi.FieldType))
throw new ArgumentException ("Field '" + fi.Name + "' does not have a valid type.");
if (!IsValidValue (fi.FieldType, fieldValues [i]))
throw new ArgumentException ("Field " + fi.Name + " is not a valid value.");
// FIXME: Check enums and TypeBuilders as well
if (fieldValues [i] != null)
// IsEnum does not seem to work on TypeBuilders
if (!(fi.FieldType is TypeBuilder) && !fi.FieldType.IsEnum && !fi.FieldType.IsInstanceOfType (fieldValues [i])) {
//
// mcs allways uses object[] for array types and
// MS.NET allows this
//
if (!fi.FieldType.IsArray)
throw new ArgumentException ("Value of field '" + fi.Name + "' does not match field type: " + fi.FieldType);
}
i ++;
}
i = 0;
foreach (PropertyInfo pi in namedProperties) {
if (!pi.CanWrite)
throw new ArgumentException ("Property '" + pi.Name + "' does not have a setter.");
Type t = pi.DeclaringType;
if ((atype != t) && (!t.IsSubclassOf (atype)) && (!atype.IsSubclassOf (t)))
throw new ArgumentException ("Property '" + pi.Name + "' does not belong to the same class as the constructor");
if (!IsValidType (pi.PropertyType))
throw new ArgumentException ("Property '" + pi.Name + "' does not have a valid type.");
if (!IsValidValue (pi.PropertyType, propertyValues [i]))
throw new ArgumentException ("Property " + pi.Name + " is not a valid value.");
if (propertyValues [i] != null) {
if (!(pi.PropertyType is TypeBuilder) && !pi.PropertyType.IsEnum && !pi.PropertyType.IsInstanceOfType (propertyValues [i]))
if (!pi.PropertyType.IsArray)
throw new ArgumentException ("Value of property '" + pi.Name + "' does not match property type: " + pi.PropertyType + " -> " + propertyValues [i]);
}
i ++;
}
i = 0;
foreach (ParameterInfo pi in GetParameters (con)) {
if (pi != null) {
Type paramType = pi.ParameterType;
if (!IsValidType (paramType))
throw new ArgumentException ("Parameter " + i + " does not have a valid type.");
if (!IsValidValue (paramType, constructorArgs [i]))
throw new ArgumentException ("Parameter " + i + " is not a valid value.");
if (constructorArgs [i] != null) {
if (!(paramType is TypeBuilder) && !paramType.IsEnum && !paramType.IsInstanceOfType (constructorArgs [i]))
if (!paramType.IsArray)
throw new ArgumentException ("Value of argument " + i + " does not match parameter type: " + paramType + " -> " + constructorArgs [i]);
if (!IsValidParam (constructorArgs [i], paramType))
throw new ArgumentException ("Cannot emit a CustomAttribute with argument of type " + constructorArgs [i].GetType () + ".");
}
}
i ++;
}
data = GetBlob (atype.Assembly, con, constructorArgs, namedProperties, propertyValues, namedFields, fieldValues);
}
/* helper methods */
internal static int decode_len (byte[] data, int pos, out int rpos) {
int len = 0;
if ((data [pos] & 0x80) == 0) {
len = (int)(data [pos++] & 0x7f);
} else if ((data [pos] & 0x40) == 0) {
len = ((data [pos] & 0x3f) << 8) + data [pos + 1];
pos += 2;
} else {
len = ((data [pos] & 0x1f) << 24) + (data [pos + 1] << 16) + (data [pos + 2] << 8) + data [pos + 3];
pos += 4;
}
rpos = pos;
return len;
}
internal static string string_from_bytes (byte[] data, int pos, int len)
{
return System.Text.Encoding.UTF8.GetString(data, pos, len);
}
internal static string decode_string (byte [] data, int pos, out int rpos)
{
if (data [pos] == 0xff) {
rpos = pos + 1;
return null;
} else {
int len = decode_len (data, pos, out pos);
string s = string_from_bytes (data, pos, len);
pos += len;
rpos = pos;
return s;
}
}
internal string string_arg ()
{
int pos = 2;
return decode_string (data, pos, out pos);
}
internal static UnmanagedMarshal get_umarshal (CustomAttributeBuilder customBuilder, bool is_field) {
byte[] data = customBuilder.Data;
UnmanagedType subtype = (UnmanagedType)0x50; /* NATIVE_MAX */
int sizeConst = -1;
int sizeParamIndex = -1;
bool hasSize = false;
int value;
int utype; /* the (stupid) ctor takes a short or an enum ... */
string marshalTypeName = null;
Type marshalTypeRef = null;
string marshalCookie = String.Empty;
utype = (int)data [2];
utype |= ((int)data [3]) << 8;
string first_type_name = GetParameters (customBuilder.Ctor) [0].ParameterType.FullName;
int pos = 6;
if (first_type_name == "System.Int16")
pos = 4;
int nnamed = (int)data [pos++];
nnamed |= ((int)data [pos++]) << 8;
for (int i = 0; i < nnamed; ++i) {
int paramType; // What is this ?
/* Skip field/property signature */
int fieldPropSig = (int)data [pos ++];
/* Read type */
paramType = ((int)data [pos++]);
if (paramType == 0x55) {
/* enums, the value is preceeded by the type */
decode_string (data, pos, out pos);
}
string named_name = decode_string (data, pos, out pos);
switch (named_name) {
case "ArraySubType":
value = (int)data [pos++];
value |= ((int)data [pos++]) << 8;
value |= ((int)data [pos++]) << 16;
value |= ((int)data [pos++]) << 24;
subtype = (UnmanagedType)value;
break;
case "SizeConst":
value = (int)data [pos++];
value |= ((int)data [pos++]) << 8;
value |= ((int)data [pos++]) << 16;
value |= ((int)data [pos++]) << 24;
sizeConst = value;
hasSize = true;
break;
case "SafeArraySubType":
value = (int)data[pos++];
value |= ((int)data[pos++]) << 8;
value |= ((int)data[pos++]) << 16;
value |= ((int)data[pos++]) << 24;
subtype = (UnmanagedType)value;
break;
case "IidParameterIndex":
pos += 4;
break;
case "SafeArrayUserDefinedSubType":
decode_string (data, pos, out pos);
break;
case "SizeParamIndex":
value = (int)data [pos++];
value |= ((int)data [pos++]) << 8;
sizeParamIndex = value;
hasSize = true;
break;
case "MarshalType":
marshalTypeName = decode_string (data, pos, out pos);
break;
case "MarshalTypeRef":
marshalTypeName = decode_string (data, pos, out pos);
if (marshalTypeName != null)
marshalTypeRef = Type.GetType (marshalTypeName);
break;
case "MarshalCookie":
marshalCookie = decode_string (data, pos, out pos);
break;
default:
throw new Exception ("Unknown MarshalAsAttribute field: " + named_name);
}
}
switch ((UnmanagedType)utype) {
case UnmanagedType.LPArray:
if (hasSize)
return UnmanagedMarshal.DefineLPArrayInternal (subtype, sizeConst, sizeParamIndex);
else
return UnmanagedMarshal.DefineLPArray (subtype);
#if FEATURE_COMINTEROP
case UnmanagedType.SafeArray:
return UnmanagedMarshal.DefineSafeArray (subtype);
#endif
case UnmanagedType.ByValArray:
if (!is_field)
throw new ArgumentException ("Specified unmanaged type is only valid on fields");
return UnmanagedMarshal.DefineByValArray (sizeConst);
case UnmanagedType.ByValTStr:
return UnmanagedMarshal.DefineByValTStr (sizeConst);
#if FEATURE_COMINTEROP
case UnmanagedType.CustomMarshaler:
return UnmanagedMarshal.DefineCustom (marshalTypeRef, marshalCookie, marshalTypeName, Guid.Empty);
#endif
default:
return UnmanagedMarshal.DefineUnmanagedMarshal ((UnmanagedType)utype);
}
}
static Type elementTypeToType (int elementType) {
/* Partition II, section 23.1.16 */
switch (elementType) {
case 0x02:
return typeof (bool);
case 0x03:
return typeof (char);
case 0x04:
return typeof (sbyte);
case 0x05:
return typeof (byte);
case 0x06:
return typeof (short);
case 0x07:
return typeof (ushort);
case 0x08:
return typeof (int);
case 0x09:
return typeof (uint);
case 0x0a:
return typeof (long);
case 0x0b:
return typeof (ulong);
case 0x0c:
return typeof (float);
case 0x0d:
return typeof (double);
case 0x0e:
return typeof (string);
default:
throw new Exception ("Unknown element type '" + elementType + "'");
}
}
static object decode_cattr_value (Type t, byte[] data, int pos, out int rpos) {
switch (Type.GetTypeCode (t)) {
case TypeCode.String:
if (data [pos] == 0xff) {
rpos = pos + 1;
return null;
}
int len = decode_len (data, pos, out pos);
rpos = pos + len;
return string_from_bytes (data, pos, len);
case TypeCode.Int32:
rpos = pos + 4;
return data [pos] + (data [pos + 1] << 8) + (data [pos + 2] << 16) + (data [pos + 3] << 24);
case TypeCode.Boolean:
rpos = pos + 1;
return (data [pos] == 0) ? false : true;
case TypeCode.Object:
int subtype = data [pos];
pos += 1;
if (subtype >= 0x02 && subtype <= 0x0e)
return decode_cattr_value (elementTypeToType (subtype), data, pos, out rpos);
else
throw new Exception ("Subtype '" + subtype + "' of type object not yet handled in decode_cattr_value");
default:
throw new Exception ("FIXME: Type " + t + " not yet handled in decode_cattr_value.");
}
}
internal struct CustomAttributeInfo {
public ConstructorInfo ctor;
public object[] ctorArgs;
public string[] namedParamNames;
public object[] namedParamValues;
}
internal static CustomAttributeInfo decode_cattr (CustomAttributeBuilder customBuilder) {
byte[] data = customBuilder.Data;
ConstructorInfo ctor = customBuilder.Ctor;
int pos = 0;
CustomAttributeInfo info = new CustomAttributeInfo ();
// Prolog
if (data.Length < 2)
throw new Exception ("Custom attr length is only '" + data.Length + "'");
if ((data [0] != 0x1) || (data [1] != 0x00))
throw new Exception ("Prolog invalid");
pos = 2;
ParameterInfo [] pi = GetParameters (ctor);
info.ctor = ctor;
info.ctorArgs = new object [pi.Length];
for (int i = 0; i < pi.Length; ++i)
info.ctorArgs [i] = decode_cattr_value (pi [i].ParameterType, data, pos, out pos);
int num_named = data [pos] + (data [pos + 1] * 256);
pos += 2;
info.namedParamNames = new string [num_named];
info.namedParamValues = new object [num_named];
for (int i = 0; i < num_named; ++i) {
int named_type = data [pos++];
int data_type = data [pos++];
string enum_type_name = null;
if (data_type == 0x55) {
int len2 = decode_len (data, pos, out pos);
enum_type_name = string_from_bytes (data, pos, len2);
pos += len2;
}
int len = decode_len (data, pos, out pos);
string name = string_from_bytes (data, pos, len);
info.namedParamNames [i] = name;
pos += len;
if (named_type == 0x53) {
/* Field */
FieldInfo fi = ctor.DeclaringType.GetField (name, BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance);
if (fi == null)
throw new Exception ("Custom attribute type '" + ctor.DeclaringType + "' doesn't contain a field named '" + name + "'");
object val = decode_cattr_value (fi.FieldType, data, pos, out pos);
if (enum_type_name != null) {
Type enumType = Type.GetType (enum_type_name);
val = Enum.ToObject (enumType, val);
}
info.namedParamValues [i] = val;
}
else
// FIXME:
throw new Exception ("Unknown named type: " + named_type);
}
return info;
}
static ParameterInfo [] GetParameters (ConstructorInfo ctor)
{
ConstructorBuilder cb = ctor as ConstructorBuilder;
if (cb != null)
return cb.GetParametersInternal ();
return ctor.GetParametersInternal ();
}
}
}
#endif
|