3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 // <OWNER>Microsoft</OWNER>
10 using System
.Reflection
;
11 using System
.Globalization
;
12 using System
.Threading
;
13 using System
.Diagnostics
;
14 using System
.Security
.Permissions
;
15 using System
.Collections
;
16 using System
.Runtime
.CompilerServices
;
17 using System
.Security
;
19 using System
.Runtime
.InteropServices
;
20 using System
.Configuration
.Assemblies
;
21 using System
.Runtime
.Versioning
;
22 using System
.Diagnostics
.Contracts
;
24 namespace System
.Reflection
27 internal enum CorElementType
: byte
69 internal enum MdSigCallingConvention
: byte
71 CallConvMask
= 0x0f, // Calling convention is bottom 4 bits
83 GenericInst
= 0x0a, // generic method instantiation
85 Generic
= 0x10, // Generic method sig with explicit number of type arguments (precedes ordinary parameter count)
86 HasThis
= 0x20, // Top bit indicates a 'this' parameter
87 ExplicitThis
= 0x40, // This parameter is explicitly in the signature
93 internal enum PInvokeAttributes
99 CharSetNotSpec
= 0x0000,
100 CharSetAnsi
= 0x0002,
101 CharSetUnicode
= 0x0004,
102 CharSetAuto
= 0x0006,
105 BestFitUseAssem
= 0x0000,
106 BestFitEnabled
= 0x0010,
107 BestFitDisabled
= 0x0020,
108 BestFitMask
= 0x0030,
110 ThrowOnUnmappableCharUseAssem
= 0x0000,
111 ThrowOnUnmappableCharEnabled
= 0x1000,
112 ThrowOnUnmappableCharDisabled
= 0x2000,
113 ThrowOnUnmappableCharMask
= 0x3000,
115 SupportsLastError
= 0x0040,
117 CallConvMask
= 0x0700,
118 CallConvWinapi
= 0x0100,
119 CallConvCdecl
= 0x0200,
120 CallConvStdcall
= 0x0300,
121 CallConvThiscall
= 0x0400,
122 CallConvFastcall
= 0x0500,
130 internal enum MethodSemanticsAttributes
142 internal enum MetadataTokenType
145 TypeRef
= 0x01000000,
146 TypeDef
= 0x02000000,
147 FieldDef
= 0x04000000,
148 MethodDef
= 0x06000000,
149 ParamDef
= 0x08000000,
150 InterfaceImpl
= 0x09000000,
151 MemberRef
= 0x0a000000,
152 CustomAttribute
= 0x0c000000,
153 Permission
= 0x0e000000,
154 Signature
= 0x11000000,
156 Property
= 0x17000000,
157 ModuleRef
= 0x1a000000,
158 TypeSpec
= 0x1b000000,
159 Assembly
= 0x20000000,
160 AssemblyRef
= 0x23000000,
162 ExportedType
= 0x27000000,
163 ManifestResource
= 0x28000000,
164 GenericPar
= 0x2a000000,
165 MethodSpec
= 0x2b000000,
168 BaseType
= 0x72000000,
169 Invalid
= 0x7FFFFFFF,
173 internal struct ConstArray
175 public IntPtr Signature { get { return m_constArray; }
}
176 public int Length { get { return m_length; }
}
177 public byte this[int index
]
179 [System
.Security
.SecuritySafeCritical
] // auto-generated
182 if (index
< 0 || index
>= m_length
)
183 throw new IndexOutOfRangeException();
184 Contract
.EndContractBlock();
188 return ((byte*)m_constArray
.ToPointer())[index
];
193 // Keep the definition in sync with vm\ManagedMdImport.hpp
194 internal int m_length
;
195 internal IntPtr m_constArray
;
199 internal struct MetadataToken
201 #region Implicit Cast Operators
202 public static implicit operator int(MetadataToken token
) { return token.Value; }
203 public static implicit operator MetadataToken(int token
) { return new MetadataToken(token); }
206 #region Public Static Members
207 public static bool IsTokenOfType(int token
, params MetadataTokenType
[] types
)
209 for (int i
= 0; i
< types
.Length
; i
++)
211 if ((int)(token
& 0xFF000000) == (int)types
[i
])
218 public static bool IsNullToken(int token
)
220 return (token
& 0x00FFFFFF) == 0;
224 #region Public Data Members
229 public MetadataToken(int token
) { Value = token; }
232 #region Public Members
233 public bool IsGlobalTypeDefToken { get { return (Value == 0x02000001); }
}
234 public MetadataTokenType TokenType { get { return (MetadataTokenType)(Value & 0xFF000000); }
}
235 public bool IsTypeRef { get { return TokenType == MetadataTokenType.TypeRef; }
}
236 public bool IsTypeDef { get { return TokenType == MetadataTokenType.TypeDef; }
}
237 public bool IsFieldDef { get { return TokenType == MetadataTokenType.FieldDef; }
}
238 public bool IsMethodDef { get { return TokenType == MetadataTokenType.MethodDef; }
}
239 public bool IsMemberRef { get { return TokenType == MetadataTokenType.MemberRef; }
}
240 public bool IsEvent { get { return TokenType == MetadataTokenType.Event; }
}
241 public bool IsProperty { get { return TokenType == MetadataTokenType.Property; }
}
242 public bool IsParamDef { get { return TokenType == MetadataTokenType.ParamDef; }
}
243 public bool IsTypeSpec { get { return TokenType == MetadataTokenType.TypeSpec; }
}
244 public bool IsMethodSpec { get { return TokenType == MetadataTokenType.MethodSpec; }
}
245 public bool IsString { get { return TokenType == MetadataTokenType.String; }
}
246 public bool IsSignature { get { return TokenType == MetadataTokenType.Signature; }
}
247 public bool IsModule { get { return TokenType == MetadataTokenType.Module; }
}
248 public bool IsAssembly { get { return TokenType == MetadataTokenType.Assembly; }
}
249 public bool IsGenericPar { get { return TokenType == MetadataTokenType.GenericPar; }
}
252 #region Object Overrides
253 public override string ToString() { return String.Format(CultureInfo.InvariantCulture, "0x{0:x8}
", Value); }
257 internal unsafe struct MetadataEnumResult
259 // Keep the definition in sync with vm\ManagedMdImport.hpp
260 private int[] largeResult;
262 private fixed int smallResult[16];
272 public int this[int index]
274 [System.Security.SecurityCritical]
277 Contract.Requires(0 <= index && index < Length);
278 if (largeResult != null)
279 return largeResult[index];
281 fixed (int* p = smallResult)
287 internal struct MetadataImport
289 #region Private Data Members
290 private IntPtr m_metadataImport2;
291 private object m_keepalive;
294 #region Override methods from Object
295 internal static readonly MetadataImport EmptyImport = new MetadataImport((IntPtr)0, null);
297 public override int GetHashCode()
299 return ValueType.GetHashCodeOfPtr(m_metadataImport2);
302 public override bool Equals(object obj)
304 if(!(obj is MetadataImport))
306 return Equals((MetadataImport)obj);
309 private bool Equals(MetadataImport import)
311 return import.m_metadataImport2 == m_metadataImport2;
316 #region Static Members
317 [System.Security.SecurityCritical] // auto-generated
318 [ResourceExposure(ResourceScope.None)]
319 [MethodImplAttribute(MethodImplOptions.InternalCall)]
320 private static extern void _GetMarshalAs(IntPtr pNativeType, int cNativeType, out int unmanagedType, out int safeArraySubType, out string safeArrayUserDefinedSubType,
321 out int arraySubType, out int sizeParamIndex, out int sizeConst, out string marshalType, out string marshalCookie,
322 out int iidParamIndex);
323 [System.Security.SecurityCritical] // auto-generated
324 internal static void GetMarshalAs(ConstArray nativeType,
325 out UnmanagedType unmanagedType, out VarEnum safeArraySubType, out string safeArrayUserDefinedSubType,
326 out UnmanagedType arraySubType, out int sizeParamIndex, out int sizeConst, out string marshalType, out string marshalCookie,
327 out int iidParamIndex)
329 int _unmanagedType, _safeArraySubType, _arraySubType;
331 _GetMarshalAs(nativeType.Signature, (int)nativeType.Length,
332 out _unmanagedType, out _safeArraySubType, out safeArrayUserDefinedSubType,
333 out _arraySubType, out sizeParamIndex, out sizeConst, out marshalType, out marshalCookie,
335 unmanagedType = (UnmanagedType)_unmanagedType;
336 safeArraySubType = (VarEnum)_safeArraySubType;
337 arraySubType = (UnmanagedType)_arraySubType;
341 #region Internal Static Members
342 internal static void ThrowError(int hResult)
344 throw new MetadataException(hResult);
349 internal MetadataImport(IntPtr metadataImport2, object keepalive)
351 m_metadataImport2 = metadataImport2;
352 m_keepalive = keepalive;
357 [System.Security.SecurityCritical] // auto-generated
358 [ResourceExposure(ResourceScope.None)]
359 [MethodImplAttribute (MethodImplOptions.InternalCall)]
360 private unsafe static extern void _Enum(IntPtr scope, int type, int parent, out MetadataEnumResult result);
362 [System.Security.SecurityCritical] // auto-generated
363 public unsafe void Enum(MetadataTokenType type, int parent, out MetadataEnumResult result)
365 _Enum(m_metadataImport2, (int)type, parent, out result);
368 [System.Security.SecurityCritical] // auto-generated
369 public unsafe void EnumNestedTypes(int mdTypeDef, out MetadataEnumResult result)
371 Enum(MetadataTokenType.TypeDef, mdTypeDef, out result);
374 [System.Security.SecurityCritical] // auto-generated
375 public unsafe void EnumCustomAttributes(int mdToken, out MetadataEnumResult result)
377 Enum(MetadataTokenType.CustomAttribute, mdToken, out result);
380 [System.Security.SecurityCritical] // auto-generated
381 public unsafe void EnumParams(int mdMethodDef, out MetadataEnumResult result)
383 Enum(MetadataTokenType.ParamDef, mdMethodDef, out result);
386 [System.Security.SecurityCritical] // auto-generated
387 public unsafe void EnumFields(int mdTypeDef, out MetadataEnumResult result)
389 Enum(MetadataTokenType.FieldDef, mdTypeDef, out result);
392 [System.Security.SecurityCritical] // auto-generated
393 public unsafe void EnumProperties(int mdTypeDef, out MetadataEnumResult result)
395 Enum(MetadataTokenType.Property, mdTypeDef, out result);
398 [System.Security.SecurityCritical] // auto-generated
399 public unsafe void EnumEvents(int mdTypeDef, out MetadataEnumResult result)
401 Enum(MetadataTokenType.Event, mdTypeDef, out result);
404 [System.Security.SecurityCritical] // auto-generated
405 [ResourceExposure(ResourceScope.None)]
406 [MethodImplAttribute (MethodImplOptions.InternalCall)]
407 private static extern String _GetDefaultValue(IntPtr scope, int mdToken, out long value, out int length, out int corElementType);
408 [System.Security.SecurityCritical] // auto-generated
409 public String GetDefaultValue(int mdToken, out long value, out int length, out CorElementType corElementType)
413 stringVal = _GetDefaultValue(m_metadataImport2, mdToken, out value, out length, out _corElementType);
414 corElementType = (CorElementType)_corElementType;
418 [System.Security.SecurityCritical] // auto-generated
419 [ResourceExposure(ResourceScope.None)]
420 [MethodImplAttribute (MethodImplOptions.InternalCall)]
421 private static unsafe extern void _GetUserString(IntPtr scope, int mdToken, void** name, out int length);
422 [System.Security.SecurityCritical] // auto-generated
423 public unsafe String GetUserString(int mdToken)
427 _GetUserString(m_metadataImport2, mdToken, &name, out length);
432 char[] c = new char[length];
433 for (int i = 0; i < length; i ++)
436 c[i] = (char)Marshal.ReadInt16( (IntPtr) (((char*)name) + i) );
438 c[i] = ((char*)name)[i];
442 return new String(c);
445 [System.Security.SecurityCritical] // auto-generated
446 [ResourceExposure(ResourceScope.None)]
447 [MethodImplAttribute (MethodImplOptions.InternalCall)]
448 private static unsafe extern void _GetName(IntPtr scope, int mdToken, void** name);
449 [System.Security.SecurityCritical] // auto-generated
450 public unsafe Utf8String GetName(int mdToken)
453 _GetName(m_metadataImport2, mdToken, &name);
455 return new Utf8String(name);
458 [System.Security.SecurityCritical] // auto-generated
459 [ResourceExposure(ResourceScope.None)]
460 [MethodImplAttribute (MethodImplOptions.InternalCall)]
461 private static unsafe extern void _GetNamespace(IntPtr scope, int mdToken, void** namesp);
462 [System.Security.SecurityCritical] // auto-generated
463 public unsafe Utf8String GetNamespace(int mdToken)
466 _GetNamespace(m_metadataImport2, mdToken, &namesp);
468 return new Utf8String(namesp);
471 [System.Security.SecurityCritical] // auto-generated
472 [ResourceExposure(ResourceScope.None)]
473 [MethodImplAttribute (MethodImplOptions.InternalCall)]
474 private unsafe static extern void _GetEventProps(IntPtr scope, int mdToken, void** name, out int eventAttributes);
475 [System.Security.SecurityCritical] // auto-generated
476 public unsafe void GetEventProps(int mdToken, out void* name, out EventAttributes eventAttributes)
478 int _eventAttributes;
480 _GetEventProps(m_metadataImport2, mdToken, &_name, out _eventAttributes);
482 eventAttributes = (EventAttributes)_eventAttributes;
485 [System.Security.SecurityCritical] // auto-generated
486 [ResourceExposure(ResourceScope.None)]
487 [MethodImplAttribute (MethodImplOptions.InternalCall)]
488 private static extern void _GetFieldDefProps(IntPtr scope, int mdToken, out int fieldAttributes);
489 [System.Security.SecurityCritical] // auto-generated
490 public void GetFieldDefProps(int mdToken, out FieldAttributes fieldAttributes)
492 int _fieldAttributes;
493 _GetFieldDefProps(m_metadataImport2, mdToken, out _fieldAttributes);
494 fieldAttributes = (FieldAttributes)_fieldAttributes;
497 [System.Security.SecurityCritical] // auto-generated
498 [ResourceExposure(ResourceScope.None)]
499 [MethodImplAttribute (MethodImplOptions.InternalCall)]
500 private unsafe static extern void _GetPropertyProps(IntPtr scope,
501 int mdToken, void** name, out int propertyAttributes, out ConstArray signature);
502 [System.Security.SecurityCritical] // auto-generated
503 public unsafe void GetPropertyProps(int mdToken, out void* name, out PropertyAttributes propertyAttributes, out ConstArray signature)
505 int _propertyAttributes;
507 _GetPropertyProps(m_metadataImport2, mdToken, &_name, out _propertyAttributes, out signature);
509 propertyAttributes = (PropertyAttributes)_propertyAttributes;
512 [System.Security.SecurityCritical] // auto-generated
513 [ResourceExposure(ResourceScope.None)]
514 [MethodImplAttribute (MethodImplOptions.InternalCall)]
515 private static extern void _GetParentToken(IntPtr scope,
516 int mdToken, out int tkParent);
517 [System.Security.SecurityCritical] // auto-generated
518 public int GetParentToken(int tkToken)
521 _GetParentToken(m_metadataImport2, tkToken, out tkParent);
525 [System.Security.SecurityCritical] // auto-generated
526 [ResourceExposure(ResourceScope.None)]
527 [MethodImplAttribute(MethodImplOptions.InternalCall)]
528 private static extern void _GetParamDefProps(IntPtr scope,
529 int parameterToken, out int sequence, out int attributes);
530 [System.Security.SecurityCritical] // auto-generated
531 public void GetParamDefProps(int parameterToken, out int sequence, out ParameterAttributes attributes)
535 _GetParamDefProps(m_metadataImport2, parameterToken, out sequence, out _attributes);
537 attributes = (ParameterAttributes)_attributes;
540 [System.Security.SecurityCritical] // auto-generated
541 [ResourceExposure(ResourceScope.None)]
542 [MethodImplAttribute(MethodImplOptions.InternalCall)]
543 private static extern void _GetGenericParamProps(IntPtr scope,
544 int genericParameter,
547 [System.Security.SecurityCritical] // auto-generated
548 public void GetGenericParamProps(
549 int genericParameter,
550 out GenericParameterAttributes attributes)
553 _GetGenericParamProps(m_metadataImport2, genericParameter, out _attributes);
554 attributes = (GenericParameterAttributes)_attributes;
557 [System.Security.SecurityCritical] // auto-generated
558 [ResourceExposure(ResourceScope.None)]
559 [MethodImplAttribute(MethodImplOptions.InternalCall)]
560 private static extern void _GetScopeProps(IntPtr scope,
563 [System.Security.SecurityCritical] // auto-generated
564 public void GetScopeProps(
567 _GetScopeProps(m_metadataImport2, out mvid);
571 [System.Security.SecurityCritical] // auto-generated
572 public ConstArray GetMethodSignature(MetadataToken token)
574 if (token.IsMemberRef)
575 return GetMemberRefProps(token);
577 return GetSigOfMethodDef(token);
580 [System.Security.SecurityCritical] // auto-generated
581 [ResourceExposure(ResourceScope.None)]
582 [MethodImplAttribute(MethodImplOptions.InternalCall)]
583 private static extern void _GetSigOfMethodDef(IntPtr scope,
585 ref ConstArray signature);
587 [System.Security.SecurityCritical] // auto-generated
588 public ConstArray GetSigOfMethodDef(int methodToken)
590 ConstArray signature = new ConstArray();
592 _GetSigOfMethodDef(m_metadataImport2, methodToken, ref signature);
597 [System.Security.SecurityCritical] // auto-generated
598 [ResourceExposure(ResourceScope.None)]
599 [MethodImplAttribute(MethodImplOptions.InternalCall)]
600 private static extern void _GetSignatureFromToken(IntPtr scope,
602 ref ConstArray signature);
604 [System.Security.SecurityCritical] // auto-generated
605 public ConstArray GetSignatureFromToken(int token)
607 ConstArray signature = new ConstArray();
609 _GetSignatureFromToken(m_metadataImport2, token, ref signature);
614 [System.Security.SecurityCritical] // auto-generated
615 [ResourceExposure(ResourceScope.None)]
616 [MethodImplAttribute(MethodImplOptions.InternalCall)]
617 private static extern void _GetMemberRefProps(IntPtr scope,
619 out ConstArray signature);
621 [System.Security.SecurityCritical] // auto-generated
622 public ConstArray GetMemberRefProps(int memberTokenRef)
624 ConstArray signature = new ConstArray();
626 _GetMemberRefProps(m_metadataImport2, memberTokenRef, out signature);
631 [System.Security.SecurityCritical] // auto-generated
632 [ResourceExposure(ResourceScope.None)]
633 [MethodImplAttribute(MethodImplOptions.InternalCall)]
634 private static extern void _GetCustomAttributeProps(IntPtr scope,
635 int customAttributeToken,
636 out int constructorToken,
637 out ConstArray signature);
639 [System.Security.SecurityCritical] // auto-generated
640 public void GetCustomAttributeProps(
641 int customAttributeToken,
642 out int constructorToken,
643 out ConstArray signature)
645 _GetCustomAttributeProps(m_metadataImport2, customAttributeToken,
646 out constructorToken, out signature);
649 [System.Security.SecurityCritical] // auto-generated
650 [ResourceExposure(ResourceScope.None)]
651 [MethodImplAttribute(MethodImplOptions.InternalCall)]
652 private static extern void _GetClassLayout(IntPtr scope,
653 int typeTokenDef, out int packSize, out int classSize);
654 [System.Security.SecurityCritical] // auto-generated
655 public void GetClassLayout(
660 _GetClassLayout(m_metadataImport2, typeTokenDef, out packSize, out classSize);
663 [System.Security.SecurityCritical] // auto-generated
664 [ResourceExposure(ResourceScope.None)]
665 [MethodImplAttribute(MethodImplOptions.InternalCall)]
666 private static extern bool _GetFieldOffset(IntPtr scope,
667 int typeTokenDef, int fieldTokenDef, out int offset);
668 [System.Security.SecurityCritical] // auto-generated
669 public bool GetFieldOffset(
674 return _GetFieldOffset(m_metadataImport2, typeTokenDef, fieldTokenDef, out offset);
677 [System.Security.SecurityCritical] // auto-generated
678 [ResourceExposure(ResourceScope.None)]
679 [MethodImplAttribute(MethodImplOptions.InternalCall)]
680 private static extern void _GetSigOfFieldDef(IntPtr scope,
682 ref ConstArray fieldMarshal);
684 [System.Security.SecurityCritical] // auto-generated
685 public ConstArray GetSigOfFieldDef(int fieldToken)
687 ConstArray fieldMarshal = new ConstArray();
689 _GetSigOfFieldDef(m_metadataImport2, fieldToken, ref fieldMarshal);
694 [System.Security.SecurityCritical] // auto-generated
695 [ResourceExposure(ResourceScope.None)]
696 [MethodImplAttribute(MethodImplOptions.InternalCall)]
697 private static extern void _GetFieldMarshal(IntPtr scope,
699 ref ConstArray fieldMarshal);
701 [System.Security.SecurityCritical] // auto-generated
702 public ConstArray GetFieldMarshal(int fieldToken)
704 ConstArray fieldMarshal = new ConstArray();
706 _GetFieldMarshal(m_metadataImport2, fieldToken, ref fieldMarshal);
711 [System.Security.SecurityCritical] // auto-generated
712 [ResourceExposure(ResourceScope.None)]
713 [MethodImplAttribute(MethodImplOptions.InternalCall)]
714 private unsafe static extern void _GetPInvokeMap(IntPtr scope,
720 [System.Security.SecurityCritical] // auto-generated
721 public unsafe void GetPInvokeMap(
723 out PInvokeAttributes attributes,
724 out String importName,
725 out String importDll)
728 void* _importName, _importDll;
729 _GetPInvokeMap(m_metadataImport2, token, out _attributes, &_importName, &_importDll);
730 importName = new Utf8String(_importName).ToString();
731 importDll = new Utf8String(_importDll).ToString();
733 attributes = (PInvokeAttributes)_attributes;
736 [System.Security.SecurityCritical] // auto-generated
737 [ResourceExposure(ResourceScope.None)]
738 [MethodImplAttribute(MethodImplOptions.InternalCall)]
739 private static extern bool _IsValidToken(IntPtr scope, int token);
740 [System.Security.SecurityCritical] // auto-generated
741 public bool IsValidToken(int token)
743 return _IsValidToken(m_metadataImport2, token);
749 internal class MetadataException : Exception
752 internal MetadataException(int hr) { m_hr = hr; }
754 public override string ToString()
756 return String.Format(CultureInfo.CurrentCulture, "MetadataException HResult
= {0:x}
.", m_hr);