13 private const int NO_SIZE_IN_SIG = -1;
15 private byte[] m_signature;
17 private int m_currSig;
19 private int m_sizeLoc;
23 private bool m_sigDone;
25 private int m_argCount;
27 internal int ArgumentCount => m_argCount;
38 [SecuritySafeCritical]
47 return GetMethodSigHelper(mod, callingConvention, cGenericParam, returnType,
null,
null,
null,
null,
null);
59 [SecuritySafeCritical]
62 return GetMethodSigHelper(mod, callingConvention, returnType,
null,
null,
null,
null,
null);
68 signatureHelper.AddData(inst.Length);
69 foreach (
Type clsArgument
in inst)
73 return signatureHelper;
77 internal static SignatureHelper
GetMethodSigHelper(Module scope,
CallingConventions callingConvention,
Type returnType,
Type[] requiredReturnTypeCustomModifiers,
Type[] optionalReturnTypeCustomModifiers,
Type[] parameterTypes,
Type[][] requiredParameterTypeCustomModifiers,
Type[][] optionalParameterTypeCustomModifiers)
79 return GetMethodSigHelper(scope, callingConvention, 0, returnType, requiredReturnTypeCustomModifiers, optionalReturnTypeCustomModifiers, parameterTypes, requiredParameterTypeCustomModifiers, optionalParameterTypeCustomModifiers);
83 internal static SignatureHelper
GetMethodSigHelper(Module scope,
CallingConventions callingConvention,
int cGenericParam, Type returnType, Type[] requiredReturnTypeCustomModifiers, Type[] optionalReturnTypeCustomModifiers, Type[] parameterTypes, Type[][] requiredParameterTypeCustomModifiers, Type[][] optionalParameterTypeCustomModifiers)
85 if (returnType ==
null)
87 returnType = typeof(
void);
89 MdSigCallingConvention mdSigCallingConvention = MdSigCallingConvention.Default;
92 mdSigCallingConvention = MdSigCallingConvention.Vararg;
94 if (cGenericParam > 0)
96 mdSigCallingConvention |= MdSigCallingConvention.Generic;
100 mdSigCallingConvention |= MdSigCallingConvention.HasThis;
102 SignatureHelper signatureHelper =
new SignatureHelper(scope, mdSigCallingConvention, cGenericParam, returnType, requiredReturnTypeCustomModifiers, optionalReturnTypeCustomModifiers);
103 signatureHelper.AddArguments(parameterTypes, requiredParameterTypeCustomModifiers, optionalParameterTypeCustomModifiers);
104 return signatureHelper;
117 [SecuritySafeCritical]
120 if (returnType ==
null)
122 returnType = typeof(
void);
124 MdSigCallingConvention callingConvention;
125 switch (unmanagedCallConv)
128 callingConvention = MdSigCallingConvention.C;
132 callingConvention = MdSigCallingConvention.StdCall;
135 callingConvention = MdSigCallingConvention.ThisCall;
138 callingConvention = MdSigCallingConvention.FastCall;
143 return new SignatureHelper(mod, callingConvention, returnType,
null,
null);
218 return GetPropertySigHelper(mod, (
CallingConventions)0, returnType, requiredReturnTypeCustomModifiers, optionalReturnTypeCustomModifiers, parameterTypes, requiredParameterTypeCustomModifiers, optionalParameterTypeCustomModifiers);
235 [SecuritySafeCritical]
238 if (returnType ==
null)
240 returnType = typeof(
void);
242 MdSigCallingConvention mdSigCallingConvention = MdSigCallingConvention.Property;
245 mdSigCallingConvention |= MdSigCallingConvention.HasThis;
247 SignatureHelper signatureHelper =
new SignatureHelper(mod, mdSigCallingConvention, returnType, requiredReturnTypeCustomModifiers, optionalReturnTypeCustomModifiers);
248 signatureHelper.
AddArguments(parameterTypes, requiredParameterTypeCustomModifiers, optionalParameterTypeCustomModifiers);
249 return signatureHelper;
263 return new SignatureHelper(mod, type);
266 private SignatureHelper(Module mod, MdSigCallingConvention callingConvention)
268 Init(mod, callingConvention);
272 private SignatureHelper(Module mod, MdSigCallingConvention callingConvention,
int cGenericParameters, Type returnType, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
274 Init(mod, callingConvention, cGenericParameters);
275 if (callingConvention == MdSigCallingConvention.Field)
277 throw new ArgumentException(Environment.GetResourceString(
"Argument_BadFieldSig"));
279 AddOneArgTypeHelper(returnType, requiredCustomModifiers, optionalCustomModifiers);
283 private SignatureHelper(Module mod, MdSigCallingConvention callingConvention, Type returnType, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
284 : this(mod, callingConvention, 0, returnType, requiredCustomModifiers, optionalCustomModifiers)
289 private SignatureHelper(Module mod, Type type)
292 AddOneArgTypeHelper(type);
295 private void Init(Module mod)
297 m_signature =
new byte[32];
299 m_module = (mod as ModuleBuilder);
303 if (m_module ==
null && mod !=
null)
305 throw new ArgumentException(Environment.GetResourceString(
"NotSupported_MustBeModuleBuilder"));
309 private void Init(Module mod, MdSigCallingConvention callingConvention)
311 Init(mod, callingConvention, 0);
314 private void Init(Module mod, MdSigCallingConvention callingConvention,
int cGenericParam)
317 AddData((
int)callingConvention);
318 if (callingConvention == MdSigCallingConvention.Field || callingConvention == MdSigCallingConvention.GenericInst)
323 if (cGenericParam > 0)
325 AddData(cGenericParam);
327 m_sizeLoc = m_currSig++;
331 private void AddOneArgTypeHelper(Type argument,
bool pinned)
335 AddElementType(CorElementType.Pinned);
337 AddOneArgTypeHelper(argument);
341 private void AddOneArgTypeHelper(Type clsArgument, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
343 if (optionalCustomModifiers !=
null)
345 foreach (Type type
in optionalCustomModifiers)
349 throw new ArgumentNullException(
"optionalCustomModifiers");
351 if (type.HasElementType)
353 throw new ArgumentException(Environment.GetResourceString(
"Argument_ArraysInvalid"),
"optionalCustomModifiers");
355 if (type.ContainsGenericParameters)
357 throw new ArgumentException(Environment.GetResourceString(
"Argument_GenericsInvalid"),
"optionalCustomModifiers");
359 AddElementType(CorElementType.CModOpt);
364 if (requiredCustomModifiers !=
null)
366 foreach (Type type2
in requiredCustomModifiers)
370 throw new ArgumentNullException(
"requiredCustomModifiers");
372 if (type2.HasElementType)
374 throw new ArgumentException(Environment.GetResourceString(
"Argument_ArraysInvalid"),
"requiredCustomModifiers");
376 if (type2.ContainsGenericParameters)
378 throw new ArgumentException(Environment.GetResourceString(
"Argument_GenericsInvalid"),
"requiredCustomModifiers");
380 AddElementType(CorElementType.CModReqd);
385 AddOneArgTypeHelper(clsArgument);
389 private void AddOneArgTypeHelper(Type clsArgument)
391 AddOneArgTypeHelperWorker(clsArgument, lastWasGenericInst:
false);
395 private void AddOneArgTypeHelperWorker(Type clsArgument,
bool lastWasGenericInst)
397 if (clsArgument.IsGenericParameter)
399 if (clsArgument.DeclaringMethod !=
null)
401 AddElementType(CorElementType.MVar);
405 AddElementType(CorElementType.Var);
407 AddData(clsArgument.GenericParameterPosition);
410 if (clsArgument.IsGenericType && (!clsArgument.IsGenericTypeDefinition || !lastWasGenericInst))
412 AddElementType(CorElementType.GenericInst);
413 AddOneArgTypeHelperWorker(clsArgument.GetGenericTypeDefinition(), lastWasGenericInst:
true);
414 Type[] genericArguments = clsArgument.GetGenericArguments();
415 AddData(genericArguments.Length);
416 Type[] array = genericArguments;
417 foreach (Type clsArgument2
in array)
419 AddOneArgTypeHelper(clsArgument2);
423 if (clsArgument is TypeBuilder)
425 TypeBuilder typeBuilder = (TypeBuilder)clsArgument;
426 TypeToken clsToken = (!typeBuilder.Module.Equals(m_module)) ? m_module.
GetTypeToken(clsArgument) : typeBuilder.TypeToken;
427 if (clsArgument.IsValueType)
429 InternalAddTypeToken(clsToken, CorElementType.ValueType);
433 InternalAddTypeToken(clsToken, CorElementType.Class);
437 if (clsArgument is EnumBuilder)
439 TypeBuilder typeBuilder2 = ((EnumBuilder)clsArgument).m_typeBuilder;
440 TypeToken clsToken2 = (!typeBuilder2.Module.Equals(m_module)) ? m_module.
GetTypeToken(clsArgument) : typeBuilder2.TypeToken;
441 if (clsArgument.IsValueType)
443 InternalAddTypeToken(clsToken2, CorElementType.ValueType);
447 InternalAddTypeToken(clsToken2, CorElementType.Class);
451 if (clsArgument.IsByRef)
453 AddElementType(CorElementType.ByRef);
454 clsArgument = clsArgument.GetElementType();
455 AddOneArgTypeHelper(clsArgument);
458 if (clsArgument.IsPointer)
460 AddElementType(CorElementType.Ptr);
461 AddOneArgTypeHelper(clsArgument.GetElementType());
464 if (clsArgument.IsArray)
466 if (clsArgument.IsSzArray)
468 AddElementType(CorElementType.SzArray);
469 AddOneArgTypeHelper(clsArgument.GetElementType());
472 AddElementType(CorElementType.Array);
473 AddOneArgTypeHelper(clsArgument.GetElementType());
474 int arrayRank = clsArgument.GetArrayRank();
478 for (
int j = 0; j < arrayRank; j++)
484 CorElementType corElementType = CorElementType.Max;
485 if (clsArgument is RuntimeType)
487 corElementType = RuntimeTypeHandle.GetCorElementType((RuntimeType)clsArgument);
488 if (corElementType == CorElementType.Class)
490 if (clsArgument == typeof(
object))
492 corElementType = CorElementType.Object;
494 else if (clsArgument == typeof(
string))
496 corElementType = CorElementType.String;
500 if (IsSimpleType(corElementType))
502 AddElementType(corElementType);
504 else if (m_module ==
null)
506 InternalAddRuntimeType(clsArgument);
508 else if (clsArgument.IsValueType)
510 InternalAddTypeToken(m_module.
GetTypeToken(clsArgument), CorElementType.ValueType);
514 InternalAddTypeToken(m_module.
GetTypeToken(clsArgument), CorElementType.Class);
518 private void AddData(
int data)
520 if (m_currSig + 4 > m_signature.Length)
522 m_signature = ExpandArray(m_signature);
526 m_signature[m_currSig++] = (byte)(data & 0xFF);
531 m_signature[m_currSig++] = (byte)((data >> 8) | 0x80);
532 m_signature[m_currSig++] = (byte)(data & 0xFF);
535 if (data <= 536870911)
537 m_signature[m_currSig++] = (byte)((data >> 24) | 0xC0);
538 m_signature[m_currSig++] = (byte)((data >> 16) & 0xFF);
539 m_signature[m_currSig++] = (byte)((data >> 8) & 0xFF);
540 m_signature[m_currSig++] = (byte)(data & 0xFF);
543 throw new ArgumentException(Environment.GetResourceString(
"Argument_LargeInteger"));
546 private void AddData(uint data)
548 if (m_currSig + 4 > m_signature.Length)
550 m_signature = ExpandArray(m_signature);
552 m_signature[m_currSig++] = (byte)(data & 0xFF);
553 m_signature[m_currSig++] = (byte)((data >> 8) & 0xFF);
554 m_signature[m_currSig++] = (byte)((data >> 16) & 0xFF);
555 m_signature[m_currSig++] = (byte)((data >> 24) & 0xFF);
558 private void AddData(ulong data)
560 if (m_currSig + 8 > m_signature.Length)
562 m_signature = ExpandArray(m_signature);
564 m_signature[m_currSig++] = (byte)(data & 0xFF);
565 m_signature[m_currSig++] = (byte)((data >> 8) & 0xFF);
566 m_signature[m_currSig++] = (byte)((data >> 16) & 0xFF);
567 m_signature[m_currSig++] = (byte)((data >> 24) & 0xFF);
568 m_signature[m_currSig++] = (byte)((data >> 32) & 0xFF);
569 m_signature[m_currSig++] = (byte)((data >> 40) & 0xFF);
570 m_signature[m_currSig++] = (byte)((data >> 48) & 0xFF);
571 m_signature[m_currSig++] = (byte)((data >> 56) & 0xFF);
574 private void AddElementType(CorElementType cvt)
576 if (m_currSig + 1 > m_signature.Length)
578 m_signature = ExpandArray(m_signature);
580 m_signature[m_currSig++] = (byte)cvt;
583 private void AddToken(
int token)
585 int num = token & 0xFFFFFF;
586 MetadataTokenType metadataTokenType = (MetadataTokenType)(token & -16777216);
589 throw new ArgumentException(Environment.GetResourceString(
"Argument_LargeInteger"));
592 switch (metadataTokenType)
594 case MetadataTokenType.TypeRef:
597 case MetadataTokenType.TypeSpec:
604 private void InternalAddTypeToken(TypeToken clsToken, CorElementType CorType)
606 AddElementType(CorType);
607 AddToken(clsToken.Token);
611 private unsafe
void InternalAddRuntimeType(Type type)
613 AddElementType(CorElementType.Internal);
614 IntPtr value = type.GetTypeHandleInternal().Value;
615 if (m_currSig +
sizeof(
void*) > m_signature.Length)
617 m_signature = ExpandArray(m_signature);
619 byte* ptr = (
byte*)(&value);
620 for (
int i = 0; i <
sizeof(
void*); i++)
622 m_signature[m_currSig++] = ptr[i];
626 private byte[] ExpandArray(
byte[] inArray)
628 return ExpandArray(inArray, inArray.Length * 2);
631 private byte[] ExpandArray(
byte[] inArray,
int requiredLength)
633 if (requiredLength < inArray.Length)
635 requiredLength = inArray.Length * 2;
637 byte[] array =
new byte[requiredLength];
638 Array.Copy(inArray, array, inArray.Length);
642 private void IncrementArgCounts()
650 private void SetNumberOfSignatureElements(
bool forceCopy)
652 int currSig = m_currSig;
655 if (m_argCount < 128 && !forceCopy)
657 m_signature[m_sizeLoc] = (byte)m_argCount;
660 int num = (m_argCount < 128) ? 1 : ((m_argCount >= 16384) ? 4 : 2);
661 byte[] array =
new byte[m_currSig + num - 1];
662 array[0] = m_signature[0];
663 Array.Copy(m_signature, m_sizeLoc + 1, array, m_sizeLoc + num, currSig - (m_sizeLoc + 1));
665 m_currSig = m_sizeLoc;
667 m_currSig = currSig + (num - 1);
671 internal static bool IsSimpleType(CorElementType type)
677 if (type == CorElementType.TypedByRef || type == CorElementType.I || type == CorElementType.U || type == CorElementType.Object)
684 internal byte[] InternalGetSignature(out
int length)
689 SetNumberOfSignatureElements(forceCopy:
false);
695 internal byte[] InternalGetSignatureArray()
697 int argCount = m_argCount;
698 int currSig = m_currSig;
700 num = ((argCount < 127) ? (num + 1) : ((argCount >= 16383) ? (num + 4) : (num + 2)));
701 byte[] array =
new byte[num];
703 array[num2++] = m_signature[0];
706 array[num2++] = (byte)(argCount & 0xFF);
708 else if (argCount <= 16383)
710 array[num2++] = (byte)((argCount >> 8) | 0x80);
711 array[num2++] = (byte)(argCount & 0xFF);
715 if (argCount > 536870911)
717 throw new ArgumentException(Environment.GetResourceString(
"Argument_LargeInteger"));
719 array[num2++] = (byte)((argCount >> 24) | 0xC0);
720 array[num2++] = (byte)((argCount >> 16) & 0xFF);
721 array[num2++] = (byte)((argCount >> 8) & 0xFF);
722 array[num2++] = (byte)(argCount & 0xFF);
724 Array.Copy(m_signature, 2, array, num2, currSig - 2);
745 [SecuritySafeCritical]
748 if (argument ==
null)
752 IncrementArgCounts();
753 AddOneArgTypeHelper(argument, pinned);
764 if (requiredCustomModifiers !=
null && (arguments ==
null || requiredCustomModifiers.Length != arguments.Length))
768 if (optionalCustomModifiers !=
null && (arguments ==
null || optionalCustomModifiers.Length != arguments.Length))
772 if (arguments !=
null)
774 for (
int i = 0; i < arguments.Length; i++)
776 AddArgument(arguments[i], (requiredCustomModifiers ==
null) ?
null : requiredCustomModifiers[i], (optionalCustomModifiers ==
null) ?
null : optionalCustomModifiers[i]);
788 [SecuritySafeCritical]
795 if (argument ==
null)
799 IncrementArgCounts();
800 AddOneArgTypeHelper(argument, requiredCustomModifiers, optionalCustomModifiers);
806 AddElementType(CorElementType.Sentinel);
820 if (!signatureHelper.m_module.
Equals(m_module) || signatureHelper.m_currSig != m_currSig || signatureHelper.m_sizeLoc != m_sizeLoc || signatureHelper.m_sigDone != m_sigDone)
824 for (
int i = 0; i < m_currSig; i++)
826 if (m_signature[i] != signatureHelper.m_signature[i])
838 int num = m_module.
GetHashCode() + m_currSig + m_sizeLoc;
843 for (
int i = 0; i < m_currSig; i++)
845 num += m_signature[i].GetHashCode();
863 AddElementType(CorElementType.End);
865 SetNumberOfSignatureElements(forceCopy:
true);
868 if (m_signature.Length > m_currSig)
870 byte[] array =
new byte[m_currSig];
871 Array.
Copy(m_signature, array, m_currSig);
892 for (
int i = 0; i <= m_currSig; i++)
894 stringBuilder.
Append(m_signature[i] +
" ");
940 void _SignatureHelper.
Invoke(uint dispIdMember, [In] ref Guid riid, uint lcid,
short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
942 throw new NotImplementedException();
void AddArgument(Type clsArgument)
Adds an argument to the signature.
Performs reflection on a module.
static string NewLine
Gets the newline string defined for this environment.
Exposes the T:System.Reflection.Emit.SignatureHelper class to unmanaged code.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
static SignatureHelper GetFieldSigHelper(Module mod)
Returns a signature helper for a field.
unsafe override string ToString()
Converts the value of this instance to a T:System.String.
byte [] GetSignature()
Adds the end token to the signature and marks the signature as finished, so no further tokens can be ...
static SignatureHelper GetPropertySigHelper(Module mod, Type returnType, Type[] parameterTypes)
Returns a signature helper for a property, given the dynamic module that contains the property,...
CallingConvention
Specifies the calling convention required to call methods implemented in unmanaged code.
Provides methods for building signatures.
static SignatureHelper GetMethodSigHelper(CallingConventions callingConvention, Type returnType)
Returns a signature helper for a method given the method's calling convention and return type.
void AddArgument(Type argument, bool pinned)
Adds an argument of the specified type to the signature, specifying whether the argument is pinned.
override bool Equals(object obj)
Returns a value that indicates whether this instance is equal to the specified object.
static SignatureHelper GetPropertySigHelper(Module mod, Type returnType, Type[] requiredReturnTypeCustomModifiers, Type[] optionalReturnTypeCustomModifiers, Type[] parameterTypes, Type[][] requiredParameterTypeCustomModifiers, Type[][] optionalParameterTypeCustomModifiers)
Returns a signature helper for a property, given the dynamic module that contains the property,...
void GetIDsOfNames([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
Maps a set of names to a corresponding set of dispatch identifiers.
override bool Equals(object obj)
Checks if this instance is equal to the given object.
static SignatureHelper GetMethodSigHelper(Module mod, CallingConventions callingConvention, Type returnType)
Returns a signature helper for a method given the method's module, calling convention,...
void GetTypeInfoCount(out uint pcTInfo)
Retrieves the number of type information interfaces that an object provides (either 0 or 1).
override int GetHashCode()
Returns the hash code for this instance.
CallingConventions
Defines the valid calling conventions for a method.
Provides information about, and means to manipulate, the current environment and platform....
Represents a globally unique identifier (GUID).To browse the .NET Framework source code for this type...
static SignatureHelper GetPropertySigHelper(Module mod, CallingConventions callingConvention, Type returnType, Type[] requiredReturnTypeCustomModifiers, Type[] optionalReturnTypeCustomModifiers, Type[] parameterTypes, Type[][] requiredParameterTypeCustomModifiers, Type[][] optionalParameterTypeCustomModifiers)
Returns a signature helper for a property, given the dynamic module that contains the property,...
static SignatureHelper GetMethodSigHelper(Module mod, CallingConvention unmanagedCallConv, Type returnType)
Returns a signature helper for a method given the method's module, unmanaged calling convention,...
StringBuilder Append(char value, int repeatCount)
Appends a specified number of copies of the string representation of a Unicode character to this inst...
Defines and represents a module in a dynamic assembly.
A platform-specific type that is used to represent a pointer or a handle.
void GetTypeInfo(uint iTInfo, uint lcid, IntPtr ppTInfo)
Retrieves the type information for an object, which can be used to get the type information for an in...
void AddSentinel()
Marks the end of a vararg fixed part. This is only used if the caller is creating a vararg signature ...
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Represents type declarations: class types, interface types, array types, value types,...
static SignatureHelper GetLocalVarSigHelper()
Returns a signature helper for a local variable.
Represents a mutable string of characters. This class cannot be inherited.To browse the ....
void AddArgument(Type argument, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
Adds an argument to the signature, with the specified custom modifiers.
override string ToString()
Returns a string representing the signature arguments.
The exception that is thrown when one of the arguments provided to a method is not valid.
static void Copy(Array sourceArray, Array destinationArray, int length)
Copies a range of elements from an T:System.Array starting at the first element and pastes them into ...
static SignatureHelper GetLocalVarSigHelper(Module mod)
Returns a signature helper for a local variable.
static SignatureHelper GetMethodSigHelper(Module mod, Type returnType, Type[] parameterTypes)
Returns a signature helper for a method with a standard calling convention, given the method's module...
ClassInterfaceType
Identifies the type of class interface that is generated for a class.
void Invoke(uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
Provides access to properties and methods exposed by an object.
int Token
Retrieves the metadata token for this class.
static SignatureHelper GetMethodSigHelper(CallingConvention unmanagedCallingConvention, Type returnType)
Returns a signature helper for a method given the method's unmanaged calling convention and return ty...
TypeToken GetTypeToken(Type type)
Returns the token used to identify the specified type within this module.
The exception that is thrown when a requested method or operation is not implemented.
override int GetHashCode()
Creates and returns a hash code for this instance.
void AddArguments(Type[] arguments, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
Adds a set of arguments to the signature, with the specified custom modifiers.