mscorlib(4.0.0.0) API with additions
SignatureHelper.cs
2 using System.Security;
3 using System.Text;
4 
6 {
8  [ClassInterface(ClassInterfaceType.None)]
9  [ComDefaultInterface(typeof(_SignatureHelper))]
10  [ComVisible(true)]
11  public sealed class SignatureHelper : _SignatureHelper
12  {
13  private const int NO_SIZE_IN_SIG = -1;
14 
15  private byte[] m_signature;
16 
17  private int m_currSig;
18 
19  private int m_sizeLoc;
20 
21  private ModuleBuilder m_module;
22 
23  private bool m_sigDone;
24 
25  private int m_argCount;
26 
27  internal int ArgumentCount => m_argCount;
28 
38  [SecuritySafeCritical]
39  public static SignatureHelper GetMethodSigHelper(Module mod, Type returnType, Type[] parameterTypes)
40  {
41  return GetMethodSigHelper(mod, CallingConventions.Standard, returnType, null, null, parameterTypes, null, null);
42  }
43 
44  [SecurityCritical]
45  internal static SignatureHelper GetMethodSigHelper(Module mod, CallingConventions callingConvention, Type returnType, int cGenericParam)
46  {
47  return GetMethodSigHelper(mod, callingConvention, cGenericParam, returnType, null, null, null, null, null);
48  }
49 
59  [SecuritySafeCritical]
60  public static SignatureHelper GetMethodSigHelper(Module mod, CallingConventions callingConvention, Type returnType)
61  {
62  return GetMethodSigHelper(mod, callingConvention, returnType, null, null, null, null, null);
63  }
64 
65  internal static SignatureHelper GetMethodSpecSigHelper(Module scope, Type[] inst)
66  {
67  SignatureHelper signatureHelper = new SignatureHelper(scope, MdSigCallingConvention.GenericInst);
68  signatureHelper.AddData(inst.Length);
69  foreach (Type clsArgument in inst)
70  {
71  signatureHelper.AddArgument(clsArgument);
72  }
73  return signatureHelper;
74  }
75 
76  [SecurityCritical]
77  internal static SignatureHelper GetMethodSigHelper(Module scope, CallingConventions callingConvention, Type returnType, Type[] requiredReturnTypeCustomModifiers, Type[] optionalReturnTypeCustomModifiers, Type[] parameterTypes, Type[][] requiredParameterTypeCustomModifiers, Type[][] optionalParameterTypeCustomModifiers)
78  {
79  return GetMethodSigHelper(scope, callingConvention, 0, returnType, requiredReturnTypeCustomModifiers, optionalReturnTypeCustomModifiers, parameterTypes, requiredParameterTypeCustomModifiers, optionalParameterTypeCustomModifiers);
80  }
81 
82  [SecurityCritical]
83  internal static SignatureHelper GetMethodSigHelper(Module scope, CallingConventions callingConvention, int cGenericParam, Type returnType, Type[] requiredReturnTypeCustomModifiers, Type[] optionalReturnTypeCustomModifiers, Type[] parameterTypes, Type[][] requiredParameterTypeCustomModifiers, Type[][] optionalParameterTypeCustomModifiers)
84  {
85  if (returnType == null)
86  {
87  returnType = typeof(void);
88  }
89  MdSigCallingConvention mdSigCallingConvention = MdSigCallingConvention.Default;
90  if ((callingConvention & CallingConventions.VarArgs) == CallingConventions.VarArgs)
91  {
92  mdSigCallingConvention = MdSigCallingConvention.Vararg;
93  }
94  if (cGenericParam > 0)
95  {
96  mdSigCallingConvention |= MdSigCallingConvention.Generic;
97  }
98  if ((callingConvention & CallingConventions.HasThis) == CallingConventions.HasThis)
99  {
100  mdSigCallingConvention |= MdSigCallingConvention.HasThis;
101  }
102  SignatureHelper signatureHelper = new SignatureHelper(scope, mdSigCallingConvention, cGenericParam, returnType, requiredReturnTypeCustomModifiers, optionalReturnTypeCustomModifiers);
103  signatureHelper.AddArguments(parameterTypes, requiredParameterTypeCustomModifiers, optionalParameterTypeCustomModifiers);
104  return signatureHelper;
105  }
106 
117  [SecuritySafeCritical]
118  public static SignatureHelper GetMethodSigHelper(Module mod, CallingConvention unmanagedCallConv, Type returnType)
119  {
120  if (returnType == null)
121  {
122  returnType = typeof(void);
123  }
124  MdSigCallingConvention callingConvention;
125  switch (unmanagedCallConv)
126  {
127  case CallingConvention.Cdecl:
128  callingConvention = MdSigCallingConvention.C;
129  break;
130  case CallingConvention.Winapi:
131  case CallingConvention.StdCall:
132  callingConvention = MdSigCallingConvention.StdCall;
133  break;
134  case CallingConvention.ThisCall:
135  callingConvention = MdSigCallingConvention.ThisCall;
136  break;
137  case CallingConvention.FastCall:
138  callingConvention = MdSigCallingConvention.FastCall;
139  break;
140  default:
141  throw new ArgumentException(Environment.GetResourceString("Argument_UnknownUnmanagedCallConv"), "unmanagedCallConv");
142  }
143  return new SignatureHelper(mod, callingConvention, returnType, null, null);
144  }
145 
149  {
150  return GetLocalVarSigHelper(null);
151  }
152 
157  public static SignatureHelper GetMethodSigHelper(CallingConventions callingConvention, Type returnType)
158  {
159  return GetMethodSigHelper(null, callingConvention, returnType);
160  }
161 
168  public static SignatureHelper GetMethodSigHelper(CallingConvention unmanagedCallingConvention, Type returnType)
169  {
170  return GetMethodSigHelper(null, unmanagedCallingConvention, returnType);
171  }
172 
177  {
178  return new SignatureHelper(mod, MdSigCallingConvention.LocalSig);
179  }
180 
185  {
186  return new SignatureHelper(mod, MdSigCallingConvention.Field);
187  }
188 
198  public static SignatureHelper GetPropertySigHelper(Module mod, Type returnType, Type[] parameterTypes)
199  {
200  return GetPropertySigHelper(mod, returnType, null, null, parameterTypes, null, null);
201  }
202 
216  public static SignatureHelper GetPropertySigHelper(Module mod, Type returnType, Type[] requiredReturnTypeCustomModifiers, Type[] optionalReturnTypeCustomModifiers, Type[] parameterTypes, Type[][] requiredParameterTypeCustomModifiers, Type[][] optionalParameterTypeCustomModifiers)
217  {
218  return GetPropertySigHelper(mod, (CallingConventions)0, returnType, requiredReturnTypeCustomModifiers, optionalReturnTypeCustomModifiers, parameterTypes, requiredParameterTypeCustomModifiers, optionalParameterTypeCustomModifiers);
219  }
220 
235  [SecuritySafeCritical]
236  public static SignatureHelper GetPropertySigHelper(Module mod, CallingConventions callingConvention, Type returnType, Type[] requiredReturnTypeCustomModifiers, Type[] optionalReturnTypeCustomModifiers, Type[] parameterTypes, Type[][] requiredParameterTypeCustomModifiers, Type[][] optionalParameterTypeCustomModifiers)
237  {
238  if (returnType == null)
239  {
240  returnType = typeof(void);
241  }
242  MdSigCallingConvention mdSigCallingConvention = MdSigCallingConvention.Property;
243  if ((callingConvention & CallingConventions.HasThis) == CallingConventions.HasThis)
244  {
245  mdSigCallingConvention |= MdSigCallingConvention.HasThis;
246  }
247  SignatureHelper signatureHelper = new SignatureHelper(mod, mdSigCallingConvention, returnType, requiredReturnTypeCustomModifiers, optionalReturnTypeCustomModifiers);
248  signatureHelper.AddArguments(parameterTypes, requiredParameterTypeCustomModifiers, optionalParameterTypeCustomModifiers);
249  return signatureHelper;
250  }
251 
252  [SecurityCritical]
253  internal static SignatureHelper GetTypeSigToken(Module mod, Type type)
254  {
255  if (mod == null)
256  {
257  throw new ArgumentNullException("module");
258  }
259  if (type == null)
260  {
261  throw new ArgumentNullException("type");
262  }
263  return new SignatureHelper(mod, type);
264  }
265 
266  private SignatureHelper(Module mod, MdSigCallingConvention callingConvention)
267  {
268  Init(mod, callingConvention);
269  }
270 
271  [SecurityCritical]
272  private SignatureHelper(Module mod, MdSigCallingConvention callingConvention, int cGenericParameters, Type returnType, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
273  {
274  Init(mod, callingConvention, cGenericParameters);
275  if (callingConvention == MdSigCallingConvention.Field)
276  {
277  throw new ArgumentException(Environment.GetResourceString("Argument_BadFieldSig"));
278  }
279  AddOneArgTypeHelper(returnType, requiredCustomModifiers, optionalCustomModifiers);
280  }
281 
282  [SecurityCritical]
283  private SignatureHelper(Module mod, MdSigCallingConvention callingConvention, Type returnType, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
284  : this(mod, callingConvention, 0, returnType, requiredCustomModifiers, optionalCustomModifiers)
285  {
286  }
287 
288  [SecurityCritical]
289  private SignatureHelper(Module mod, Type type)
290  {
291  Init(mod);
292  AddOneArgTypeHelper(type);
293  }
294 
295  private void Init(Module mod)
296  {
297  m_signature = new byte[32];
298  m_currSig = 0;
299  m_module = (mod as ModuleBuilder);
300  m_argCount = 0;
301  m_sigDone = false;
302  m_sizeLoc = -1;
303  if (m_module == null && mod != null)
304  {
305  throw new ArgumentException(Environment.GetResourceString("NotSupported_MustBeModuleBuilder"));
306  }
307  }
308 
309  private void Init(Module mod, MdSigCallingConvention callingConvention)
310  {
311  Init(mod, callingConvention, 0);
312  }
313 
314  private void Init(Module mod, MdSigCallingConvention callingConvention, int cGenericParam)
315  {
316  Init(mod);
317  AddData((int)callingConvention);
318  if (callingConvention == MdSigCallingConvention.Field || callingConvention == MdSigCallingConvention.GenericInst)
319  {
320  m_sizeLoc = -1;
321  return;
322  }
323  if (cGenericParam > 0)
324  {
325  AddData(cGenericParam);
326  }
327  m_sizeLoc = m_currSig++;
328  }
329 
330  [SecurityCritical]
331  private void AddOneArgTypeHelper(Type argument, bool pinned)
332  {
333  if (pinned)
334  {
335  AddElementType(CorElementType.Pinned);
336  }
337  AddOneArgTypeHelper(argument);
338  }
339 
340  [SecurityCritical]
341  private void AddOneArgTypeHelper(Type clsArgument, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
342  {
343  if (optionalCustomModifiers != null)
344  {
345  foreach (Type type in optionalCustomModifiers)
346  {
347  if (type == null)
348  {
349  throw new ArgumentNullException("optionalCustomModifiers");
350  }
351  if (type.HasElementType)
352  {
353  throw new ArgumentException(Environment.GetResourceString("Argument_ArraysInvalid"), "optionalCustomModifiers");
354  }
355  if (type.ContainsGenericParameters)
356  {
357  throw new ArgumentException(Environment.GetResourceString("Argument_GenericsInvalid"), "optionalCustomModifiers");
358  }
359  AddElementType(CorElementType.CModOpt);
360  int token = m_module.GetTypeToken(type).Token;
361  AddToken(token);
362  }
363  }
364  if (requiredCustomModifiers != null)
365  {
366  foreach (Type type2 in requiredCustomModifiers)
367  {
368  if (type2 == null)
369  {
370  throw new ArgumentNullException("requiredCustomModifiers");
371  }
372  if (type2.HasElementType)
373  {
374  throw new ArgumentException(Environment.GetResourceString("Argument_ArraysInvalid"), "requiredCustomModifiers");
375  }
376  if (type2.ContainsGenericParameters)
377  {
378  throw new ArgumentException(Environment.GetResourceString("Argument_GenericsInvalid"), "requiredCustomModifiers");
379  }
380  AddElementType(CorElementType.CModReqd);
381  int token2 = m_module.GetTypeToken(type2).Token;
382  AddToken(token2);
383  }
384  }
385  AddOneArgTypeHelper(clsArgument);
386  }
387 
388  [SecurityCritical]
389  private void AddOneArgTypeHelper(Type clsArgument)
390  {
391  AddOneArgTypeHelperWorker(clsArgument, lastWasGenericInst: false);
392  }
393 
394  [SecurityCritical]
395  private void AddOneArgTypeHelperWorker(Type clsArgument, bool lastWasGenericInst)
396  {
397  if (clsArgument.IsGenericParameter)
398  {
399  if (clsArgument.DeclaringMethod != null)
400  {
401  AddElementType(CorElementType.MVar);
402  }
403  else
404  {
405  AddElementType(CorElementType.Var);
406  }
407  AddData(clsArgument.GenericParameterPosition);
408  return;
409  }
410  if (clsArgument.IsGenericType && (!clsArgument.IsGenericTypeDefinition || !lastWasGenericInst))
411  {
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)
418  {
419  AddOneArgTypeHelper(clsArgument2);
420  }
421  return;
422  }
423  if (clsArgument is TypeBuilder)
424  {
425  TypeBuilder typeBuilder = (TypeBuilder)clsArgument;
426  TypeToken clsToken = (!typeBuilder.Module.Equals(m_module)) ? m_module.GetTypeToken(clsArgument) : typeBuilder.TypeToken;
427  if (clsArgument.IsValueType)
428  {
429  InternalAddTypeToken(clsToken, CorElementType.ValueType);
430  }
431  else
432  {
433  InternalAddTypeToken(clsToken, CorElementType.Class);
434  }
435  return;
436  }
437  if (clsArgument is EnumBuilder)
438  {
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)
442  {
443  InternalAddTypeToken(clsToken2, CorElementType.ValueType);
444  }
445  else
446  {
447  InternalAddTypeToken(clsToken2, CorElementType.Class);
448  }
449  return;
450  }
451  if (clsArgument.IsByRef)
452  {
453  AddElementType(CorElementType.ByRef);
454  clsArgument = clsArgument.GetElementType();
455  AddOneArgTypeHelper(clsArgument);
456  return;
457  }
458  if (clsArgument.IsPointer)
459  {
460  AddElementType(CorElementType.Ptr);
461  AddOneArgTypeHelper(clsArgument.GetElementType());
462  return;
463  }
464  if (clsArgument.IsArray)
465  {
466  if (clsArgument.IsSzArray)
467  {
468  AddElementType(CorElementType.SzArray);
469  AddOneArgTypeHelper(clsArgument.GetElementType());
470  return;
471  }
472  AddElementType(CorElementType.Array);
473  AddOneArgTypeHelper(clsArgument.GetElementType());
474  int arrayRank = clsArgument.GetArrayRank();
475  AddData(arrayRank);
476  AddData(0);
477  AddData(arrayRank);
478  for (int j = 0; j < arrayRank; j++)
479  {
480  AddData(0);
481  }
482  return;
483  }
484  CorElementType corElementType = CorElementType.Max;
485  if (clsArgument is RuntimeType)
486  {
487  corElementType = RuntimeTypeHandle.GetCorElementType((RuntimeType)clsArgument);
488  if (corElementType == CorElementType.Class)
489  {
490  if (clsArgument == typeof(object))
491  {
492  corElementType = CorElementType.Object;
493  }
494  else if (clsArgument == typeof(string))
495  {
496  corElementType = CorElementType.String;
497  }
498  }
499  }
500  if (IsSimpleType(corElementType))
501  {
502  AddElementType(corElementType);
503  }
504  else if (m_module == null)
505  {
506  InternalAddRuntimeType(clsArgument);
507  }
508  else if (clsArgument.IsValueType)
509  {
510  InternalAddTypeToken(m_module.GetTypeToken(clsArgument), CorElementType.ValueType);
511  }
512  else
513  {
514  InternalAddTypeToken(m_module.GetTypeToken(clsArgument), CorElementType.Class);
515  }
516  }
517 
518  private void AddData(int data)
519  {
520  if (m_currSig + 4 > m_signature.Length)
521  {
522  m_signature = ExpandArray(m_signature);
523  }
524  if (data <= 127)
525  {
526  m_signature[m_currSig++] = (byte)(data & 0xFF);
527  return;
528  }
529  if (data <= 16383)
530  {
531  m_signature[m_currSig++] = (byte)((data >> 8) | 0x80);
532  m_signature[m_currSig++] = (byte)(data & 0xFF);
533  return;
534  }
535  if (data <= 536870911)
536  {
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);
541  return;
542  }
543  throw new ArgumentException(Environment.GetResourceString("Argument_LargeInteger"));
544  }
545 
546  private void AddData(uint data)
547  {
548  if (m_currSig + 4 > m_signature.Length)
549  {
550  m_signature = ExpandArray(m_signature);
551  }
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);
556  }
557 
558  private void AddData(ulong data)
559  {
560  if (m_currSig + 8 > m_signature.Length)
561  {
562  m_signature = ExpandArray(m_signature);
563  }
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);
572  }
573 
574  private void AddElementType(CorElementType cvt)
575  {
576  if (m_currSig + 1 > m_signature.Length)
577  {
578  m_signature = ExpandArray(m_signature);
579  }
580  m_signature[m_currSig++] = (byte)cvt;
581  }
582 
583  private void AddToken(int token)
584  {
585  int num = token & 0xFFFFFF;
586  MetadataTokenType metadataTokenType = (MetadataTokenType)(token & -16777216);
587  if (num > 67108863)
588  {
589  throw new ArgumentException(Environment.GetResourceString("Argument_LargeInteger"));
590  }
591  num <<= 2;
592  switch (metadataTokenType)
593  {
594  case MetadataTokenType.TypeRef:
595  num |= 1;
596  break;
597  case MetadataTokenType.TypeSpec:
598  num |= 2;
599  break;
600  }
601  AddData(num);
602  }
603 
604  private void InternalAddTypeToken(TypeToken clsToken, CorElementType CorType)
605  {
606  AddElementType(CorType);
607  AddToken(clsToken.Token);
608  }
609 
610  [SecurityCritical]
611  private unsafe void InternalAddRuntimeType(Type type)
612  {
613  AddElementType(CorElementType.Internal);
614  IntPtr value = type.GetTypeHandleInternal().Value;
615  if (m_currSig + sizeof(void*) > m_signature.Length)
616  {
617  m_signature = ExpandArray(m_signature);
618  }
619  byte* ptr = (byte*)(&value);
620  for (int i = 0; i < sizeof(void*); i++)
621  {
622  m_signature[m_currSig++] = ptr[i];
623  }
624  }
625 
626  private byte[] ExpandArray(byte[] inArray)
627  {
628  return ExpandArray(inArray, inArray.Length * 2);
629  }
630 
631  private byte[] ExpandArray(byte[] inArray, int requiredLength)
632  {
633  if (requiredLength < inArray.Length)
634  {
635  requiredLength = inArray.Length * 2;
636  }
637  byte[] array = new byte[requiredLength];
638  Array.Copy(inArray, array, inArray.Length);
639  return array;
640  }
641 
642  private void IncrementArgCounts()
643  {
644  if (m_sizeLoc != -1)
645  {
646  m_argCount++;
647  }
648  }
649 
650  private void SetNumberOfSignatureElements(bool forceCopy)
651  {
652  int currSig = m_currSig;
653  if (m_sizeLoc != -1)
654  {
655  if (m_argCount < 128 && !forceCopy)
656  {
657  m_signature[m_sizeLoc] = (byte)m_argCount;
658  return;
659  }
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));
664  m_signature = array;
665  m_currSig = m_sizeLoc;
666  AddData(m_argCount);
667  m_currSig = currSig + (num - 1);
668  }
669  }
670 
671  internal static bool IsSimpleType(CorElementType type)
672  {
673  if ((int)type <= 14)
674  {
675  return true;
676  }
677  if (type == CorElementType.TypedByRef || type == CorElementType.I || type == CorElementType.U || type == CorElementType.Object)
678  {
679  return true;
680  }
681  return false;
682  }
683 
684  internal byte[] InternalGetSignature(out int length)
685  {
686  if (!m_sigDone)
687  {
688  m_sigDone = true;
689  SetNumberOfSignatureElements(forceCopy: false);
690  }
691  length = m_currSig;
692  return m_signature;
693  }
694 
695  internal byte[] InternalGetSignatureArray()
696  {
697  int argCount = m_argCount;
698  int currSig = m_currSig;
699  int num = currSig;
700  num = ((argCount < 127) ? (num + 1) : ((argCount >= 16383) ? (num + 4) : (num + 2)));
701  byte[] array = new byte[num];
702  int num2 = 0;
703  array[num2++] = m_signature[0];
704  if (argCount <= 127)
705  {
706  array[num2++] = (byte)(argCount & 0xFF);
707  }
708  else if (argCount <= 16383)
709  {
710  array[num2++] = (byte)((argCount >> 8) | 0x80);
711  array[num2++] = (byte)(argCount & 0xFF);
712  }
713  else
714  {
715  if (argCount > 536870911)
716  {
717  throw new ArgumentException(Environment.GetResourceString("Argument_LargeInteger"));
718  }
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);
723  }
724  Array.Copy(m_signature, 2, array, num2, currSig - 2);
725  array[num - 1] = 0;
726  return array;
727  }
728 
734  public void AddArgument(Type clsArgument)
735  {
736  AddArgument(clsArgument, null, null);
737  }
738 
745  [SecuritySafeCritical]
746  public void AddArgument(Type argument, bool pinned)
747  {
748  if (argument == null)
749  {
750  throw new ArgumentNullException("argument");
751  }
752  IncrementArgCounts();
753  AddOneArgTypeHelper(argument, pinned);
754  }
755 
762  public void AddArguments(Type[] arguments, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
763  {
764  if (requiredCustomModifiers != null && (arguments == null || requiredCustomModifiers.Length != arguments.Length))
765  {
766  throw new ArgumentException(Environment.GetResourceString("Argument_MismatchedArrays", "requiredCustomModifiers", "arguments"));
767  }
768  if (optionalCustomModifiers != null && (arguments == null || optionalCustomModifiers.Length != arguments.Length))
769  {
770  throw new ArgumentException(Environment.GetResourceString("Argument_MismatchedArrays", "optionalCustomModifiers", "arguments"));
771  }
772  if (arguments != null)
773  {
774  for (int i = 0; i < arguments.Length; i++)
775  {
776  AddArgument(arguments[i], (requiredCustomModifiers == null) ? null : requiredCustomModifiers[i], (optionalCustomModifiers == null) ? null : optionalCustomModifiers[i]);
777  }
778  }
779  }
780 
788  [SecuritySafeCritical]
789  public void AddArgument(Type argument, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
790  {
791  if (m_sigDone)
792  {
793  throw new ArgumentException(Environment.GetResourceString("Argument_SigIsFinalized"));
794  }
795  if (argument == null)
796  {
797  throw new ArgumentNullException("argument");
798  }
799  IncrementArgCounts();
800  AddOneArgTypeHelper(argument, requiredCustomModifiers, optionalCustomModifiers);
801  }
802 
804  public void AddSentinel()
805  {
806  AddElementType(CorElementType.Sentinel);
807  }
808 
813  public override bool Equals(object obj)
814  {
815  if (!(obj is SignatureHelper))
816  {
817  return false;
818  }
819  SignatureHelper signatureHelper = (SignatureHelper)obj;
820  if (!signatureHelper.m_module.Equals(m_module) || signatureHelper.m_currSig != m_currSig || signatureHelper.m_sizeLoc != m_sizeLoc || signatureHelper.m_sigDone != m_sigDone)
821  {
822  return false;
823  }
824  for (int i = 0; i < m_currSig; i++)
825  {
826  if (m_signature[i] != signatureHelper.m_signature[i])
827  {
828  return false;
829  }
830  }
831  return true;
832  }
833 
836  public override int GetHashCode()
837  {
838  int num = m_module.GetHashCode() + m_currSig + m_sizeLoc;
839  if (m_sigDone)
840  {
841  num++;
842  }
843  for (int i = 0; i < m_currSig; i++)
844  {
845  num += m_signature[i].GetHashCode();
846  }
847  return num;
848  }
849 
852  public byte[] GetSignature()
853  {
854  return GetSignature(appendEndOfSig: false);
855  }
856 
857  internal byte[] GetSignature(bool appendEndOfSig)
858  {
859  if (!m_sigDone)
860  {
861  if (appendEndOfSig)
862  {
863  AddElementType(CorElementType.End);
864  }
865  SetNumberOfSignatureElements(forceCopy: true);
866  m_sigDone = true;
867  }
868  if (m_signature.Length > m_currSig)
869  {
870  byte[] array = new byte[m_currSig];
871  Array.Copy(m_signature, array, m_currSig);
872  m_signature = array;
873  }
874  return m_signature;
875  }
876 
879  public override string ToString()
880  {
881  StringBuilder stringBuilder = new StringBuilder();
882  stringBuilder.Append("Length: " + m_currSig + Environment.NewLine);
883  if (m_sizeLoc != -1)
884  {
885  stringBuilder.Append("Arguments: " + m_signature[m_sizeLoc] + Environment.NewLine);
886  }
887  else
888  {
889  stringBuilder.Append("Field Signature" + Environment.NewLine);
890  }
891  stringBuilder.Append("Signature: " + Environment.NewLine);
892  for (int i = 0; i <= m_currSig; i++)
893  {
894  stringBuilder.Append(m_signature[i] + " ");
895  }
896  stringBuilder.Append(Environment.NewLine);
897  return stringBuilder.ToString();
898  }
899 
903  void _SignatureHelper.GetTypeInfoCount(out uint pcTInfo)
904  {
905  throw new NotImplementedException();
906  }
907 
913  void _SignatureHelper.GetTypeInfo(uint iTInfo, uint lcid, IntPtr ppTInfo)
914  {
915  throw new NotImplementedException();
916  }
917 
925  void _SignatureHelper.GetIDsOfNames([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
926  {
927  throw new NotImplementedException();
928  }
929 
940  void _SignatureHelper.Invoke(uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
941  {
942  throw new NotImplementedException();
943  }
944  }
945 }
void AddArgument(Type clsArgument)
Adds an argument to the signature.
Performs reflection on a module.
Definition: Module.cs:17
static string NewLine
Gets the newline string defined for this environment.
Definition: Environment.cs:449
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 ...
Definition: __Canon.cs:3
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....
Definition: Environment.cs:21
Represents a globally unique identifier (GUID).To browse the .NET Framework source code for this type...
Definition: Guid.cs:14
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.
Definition: IntPtr.cs:14
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...
Definition: Array.cs:17
Represents type declarations: class types, interface types, array types, value types,...
Definition: Type.cs:18
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 ...
Definition: Array.cs:1275
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.
Definition: TypeToken.cs:17
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.