mscorlib(4.0.0.0) API with additions
DynamicMethod.cs
5 using System.Security;
7 using System.Threading;
8 
10 {
12  [ComVisible(true)]
13  public sealed class DynamicMethod : MethodInfo
14  {
15  internal class RTDynamicMethod : MethodInfo
16  {
17  private class EmptyCAHolder : ICustomAttributeProvider
18  {
19  internal EmptyCAHolder()
20  {
21  }
22 
23  object[] ICustomAttributeProvider.GetCustomAttributes(Type attributeType, bool inherit)
24  {
25  return EmptyArray<object>.Value;
26  }
27 
28  object[] ICustomAttributeProvider.GetCustomAttributes(bool inherit)
29  {
30  return EmptyArray<object>.Value;
31  }
32 
33  bool ICustomAttributeProvider.IsDefined(Type attributeType, bool inherit)
34  {
35  return false;
36  }
37  }
38 
39  internal DynamicMethod m_owner;
40 
41  private ParameterInfo[] m_parameters;
42 
43  private string m_name;
44 
45  private MethodAttributes m_attributes;
46 
47  private CallingConventions m_callingConvention;
48 
49  public override string Name => m_name;
50 
51  public override Type DeclaringType => null;
52 
53  public override Type ReflectedType => null;
54 
55  public override Module Module => m_owner.m_module;
56 
57  public override RuntimeMethodHandle MethodHandle
58  {
59  get
60  {
61  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NotAllowedInDynamicMethod"));
62  }
63  }
64 
65  public override MethodAttributes Attributes => m_attributes;
66 
67  public override CallingConventions CallingConvention => m_callingConvention;
68 
69  public override bool IsSecurityCritical => m_owner.IsSecurityCritical;
70 
71  public override bool IsSecuritySafeCritical => m_owner.IsSecuritySafeCritical;
72 
73  public override bool IsSecurityTransparent => m_owner.IsSecurityTransparent;
74 
75  public override Type ReturnType => m_owner.m_returnType;
76 
77  public override ParameterInfo ReturnParameter => null;
78 
79  public override ICustomAttributeProvider ReturnTypeCustomAttributes => GetEmptyCAHolder();
80 
81  private RTDynamicMethod()
82  {
83  }
84 
85  internal RTDynamicMethod(DynamicMethod owner, string name, MethodAttributes attributes, CallingConventions callingConvention)
86  {
87  m_owner = owner;
88  m_name = name;
89  m_attributes = attributes;
90  m_callingConvention = callingConvention;
91  }
92 
93  public override string ToString()
94  {
95  return ReturnType.FormatTypeName() + " " + FormatNameAndSig();
96  }
97 
98  public override MethodInfo GetBaseDefinition()
99  {
100  return this;
101  }
102 
103  public override ParameterInfo[] GetParameters()
104  {
105  ParameterInfo[] array = LoadParameters();
106  ParameterInfo[] array2 = new ParameterInfo[array.Length];
107  Array.Copy(array, array2, array.Length);
108  return array2;
109  }
110 
112  {
113  return MethodImplAttributes.NoInlining;
114  }
115 
116  public override object Invoke(object obj, BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture)
117  {
118  throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeMethodInfo"), "this");
119  }
120 
121  public override object[] GetCustomAttributes(Type attributeType, bool inherit)
122  {
123  if (attributeType == null)
124  {
125  throw new ArgumentNullException("attributeType");
126  }
127  if (attributeType.IsAssignableFrom(typeof(MethodImplAttribute)))
128  {
129  return new object[1]
130  {
132  };
133  }
134  return EmptyArray<object>.Value;
135  }
136 
137  public override object[] GetCustomAttributes(bool inherit)
138  {
139  return new object[1]
140  {
142  };
143  }
144 
145  public override bool IsDefined(Type attributeType, bool inherit)
146  {
147  if (attributeType == null)
148  {
149  throw new ArgumentNullException("attributeType");
150  }
151  if (attributeType.IsAssignableFrom(typeof(MethodImplAttribute)))
152  {
153  return true;
154  }
155  return false;
156  }
157 
158  internal ParameterInfo[] LoadParameters()
159  {
160  if (m_parameters == null)
161  {
162  Type[] parameterTypes = m_owner.m_parameterTypes;
163  ParameterInfo[] array = new ParameterInfo[parameterTypes.Length];
164  for (int i = 0; i < parameterTypes.Length; i++)
165  {
166  array[i] = new RuntimeParameterInfo(this, null, parameterTypes[i], i);
167  }
168  if (m_parameters == null)
169  {
170  m_parameters = array;
171  }
172  }
173  return m_parameters;
174  }
175 
176  private ICustomAttributeProvider GetEmptyCAHolder()
177  {
178  return new EmptyCAHolder();
179  }
180  }
181 
182  private RuntimeType[] m_parameterTypes;
183 
184  internal IRuntimeMethodInfo m_methodHandle;
185 
186  private RuntimeType m_returnType;
187 
188  private DynamicILGenerator m_ilGenerator;
189 
190  private DynamicILInfo m_DynamicILInfo;
191 
192  private bool m_fInitLocals;
193 
194  private RuntimeModule m_module;
195 
196  internal bool m_skipVisibility;
197 
198  internal RuntimeType m_typeOwner;
199 
200  private RTDynamicMethod m_dynMethod;
201 
202  internal DynamicResolver m_resolver;
203 
204  private bool m_profileAPICheck;
205 
206  private RuntimeAssembly m_creatorAssembly;
207 
208  internal bool m_restrictedSkipVisibility;
209 
210  internal CompressedStack m_creationContext;
211 
212  private static volatile InternalModuleBuilder s_anonymouslyHostedDynamicMethodsModule;
213 
214  private static readonly object s_anonymouslyHostedDynamicMethodsModuleLock = new object();
215 
216  internal bool ProfileAPICheck
217  {
218  get
219  {
220  return m_profileAPICheck;
221  }
222  [FriendAccessAllowed]
223  set
224  {
225  m_profileAPICheck = value;
226  }
227  }
228 
231  public override string Name => m_dynMethod.Name;
232 
235  public override Type DeclaringType => m_dynMethod.DeclaringType;
236 
239  public override Type ReflectedType => m_dynMethod.ReflectedType;
240 
243  public override Module Module => m_dynMethod.Module;
244 
248  public override RuntimeMethodHandle MethodHandle
249  {
250  get
251  {
252  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NotAllowedInDynamicMethod"));
253  }
254  }
255 
258  public override MethodAttributes Attributes => m_dynMethod.Attributes;
259 
262  public override CallingConventions CallingConvention => m_dynMethod.CallingConvention;
263 
268  public override bool IsSecurityCritical
269  {
270  [SecuritySafeCritical]
271  get
272  {
273  if (m_methodHandle != null)
274  {
275  return RuntimeMethodHandle.IsSecurityCritical(m_methodHandle);
276  }
277  if (m_typeOwner != null)
278  {
279  RuntimeAssembly runtimeAssembly = m_typeOwner.Assembly as RuntimeAssembly;
280  return runtimeAssembly.IsAllSecurityCritical();
281  }
282  RuntimeAssembly runtimeAssembly2 = m_module.Assembly as RuntimeAssembly;
283  return runtimeAssembly2.IsAllSecurityCritical();
284  }
285  }
286 
291  public override bool IsSecuritySafeCritical
292  {
293  [SecuritySafeCritical]
294  get
295  {
296  if (m_methodHandle != null)
297  {
298  return RuntimeMethodHandle.IsSecuritySafeCritical(m_methodHandle);
299  }
300  if (m_typeOwner != null)
301  {
302  RuntimeAssembly runtimeAssembly = m_typeOwner.Assembly as RuntimeAssembly;
303  return runtimeAssembly.IsAllPublicAreaSecuritySafeCritical();
304  }
305  RuntimeAssembly runtimeAssembly2 = m_module.Assembly as RuntimeAssembly;
306  return runtimeAssembly2.IsAllSecuritySafeCritical();
307  }
308  }
309 
314  public override bool IsSecurityTransparent
315  {
316  [SecuritySafeCritical]
317  get
318  {
319  if (m_methodHandle != null)
320  {
321  return RuntimeMethodHandle.IsSecurityTransparent(m_methodHandle);
322  }
323  if (m_typeOwner != null)
324  {
325  RuntimeAssembly runtimeAssembly = m_typeOwner.Assembly as RuntimeAssembly;
326  return !runtimeAssembly.IsAllSecurityCritical();
327  }
328  RuntimeAssembly runtimeAssembly2 = m_module.Assembly as RuntimeAssembly;
329  return !runtimeAssembly2.IsAllSecurityCritical();
330  }
331  }
332 
335  public override Type ReturnType => m_dynMethod.ReturnType;
336 
339  public override ParameterInfo ReturnParameter => m_dynMethod.ReturnParameter;
340 
343  public override ICustomAttributeProvider ReturnTypeCustomAttributes => m_dynMethod.ReturnTypeCustomAttributes;
344 
348  public bool InitLocals
349  {
350  get
351  {
352  return m_fInitLocals;
353  }
354  set
355  {
356  m_fInitLocals = value;
357  }
358  }
359 
360  private DynamicMethod()
361  {
362  }
363 
373  [MethodImpl(MethodImplOptions.NoInlining)]
374  [SecuritySafeCritical]
375  public DynamicMethod(string name, Type returnType, Type[] parameterTypes)
376  {
377  StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
378  Init(name, MethodAttributes.FamANDAssem | MethodAttributes.Family | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, null, null, skipVisibility: false, transparentMethod: true, ref stackMark);
379  }
380 
392  [MethodImpl(MethodImplOptions.NoInlining)]
393  [SecuritySafeCritical]
394  public DynamicMethod(string name, Type returnType, Type[] parameterTypes, bool restrictedSkipVisibility)
395  {
396  StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
397  Init(name, MethodAttributes.FamANDAssem | MethodAttributes.Family | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, null, null, restrictedSkipVisibility, transparentMethod: true, ref stackMark);
398  }
399 
412  [MethodImpl(MethodImplOptions.NoInlining)]
413  [SecuritySafeCritical]
414  public DynamicMethod(string name, Type returnType, Type[] parameterTypes, Module m)
415  {
416  StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
417  PerformSecurityCheck(m, ref stackMark, skipVisibility: false);
418  Init(name, MethodAttributes.FamANDAssem | MethodAttributes.Family | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, null, m, skipVisibility: false, transparentMethod: false, ref stackMark);
419  }
420 
435  [MethodImpl(MethodImplOptions.NoInlining)]
436  [SecuritySafeCritical]
437  public DynamicMethod(string name, Type returnType, Type[] parameterTypes, Module m, bool skipVisibility)
438  {
439  StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
440  PerformSecurityCheck(m, ref stackMark, skipVisibility);
441  Init(name, MethodAttributes.FamANDAssem | MethodAttributes.Family | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, null, m, skipVisibility, transparentMethod: false, ref stackMark);
442  }
443 
462  [MethodImpl(MethodImplOptions.NoInlining)]
463  [SecuritySafeCritical]
464  public DynamicMethod(string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, Module m, bool skipVisibility)
465  {
466  StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
467  PerformSecurityCheck(m, ref stackMark, skipVisibility);
468  Init(name, attributes, callingConvention, returnType, parameterTypes, null, m, skipVisibility, transparentMethod: false, ref stackMark);
469  }
470 
483  [MethodImpl(MethodImplOptions.NoInlining)]
484  [SecuritySafeCritical]
485  public DynamicMethod(string name, Type returnType, Type[] parameterTypes, Type owner)
486  {
487  StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
488  PerformSecurityCheck(owner, ref stackMark, skipVisibility: false);
489  Init(name, MethodAttributes.FamANDAssem | MethodAttributes.Family | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, owner, null, skipVisibility: false, transparentMethod: false, ref stackMark);
490  }
491 
506  [MethodImpl(MethodImplOptions.NoInlining)]
507  [SecuritySafeCritical]
508  public DynamicMethod(string name, Type returnType, Type[] parameterTypes, Type owner, bool skipVisibility)
509  {
510  StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
511  PerformSecurityCheck(owner, ref stackMark, skipVisibility);
512  Init(name, MethodAttributes.FamANDAssem | MethodAttributes.Family | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, owner, null, skipVisibility, transparentMethod: false, ref stackMark);
513  }
514 
533  [MethodImpl(MethodImplOptions.NoInlining)]
534  [SecuritySafeCritical]
535  public DynamicMethod(string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, Type owner, bool skipVisibility)
536  {
537  StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
538  PerformSecurityCheck(owner, ref stackMark, skipVisibility);
539  Init(name, attributes, callingConvention, returnType, parameterTypes, owner, null, skipVisibility, transparentMethod: false, ref stackMark);
540  }
541 
542  private static void CheckConsistency(MethodAttributes attributes, CallingConventions callingConvention)
543  {
544  if ((attributes & ~MethodAttributes.MemberAccessMask) != MethodAttributes.Static)
545  {
546  throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicMethodFlags"));
547  }
548  if ((attributes & MethodAttributes.MemberAccessMask) != MethodAttributes.Public)
549  {
550  throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicMethodFlags"));
551  }
552  if (callingConvention != CallingConventions.Standard && callingConvention != CallingConventions.VarArgs)
553  {
554  throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicMethodFlags"));
555  }
556  if (callingConvention == CallingConventions.VarArgs)
557  {
558  throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicMethodFlags"));
559  }
560  }
561 
562  [MethodImpl(MethodImplOptions.NoInlining)]
563  [SecurityCritical]
564  private static RuntimeModule GetDynamicMethodsModule()
565  {
566  if (s_anonymouslyHostedDynamicMethodsModule != null)
567  {
568  return s_anonymouslyHostedDynamicMethodsModule;
569  }
570  lock (s_anonymouslyHostedDynamicMethodsModuleLock)
571  {
572  if (s_anonymouslyHostedDynamicMethodsModule != null)
573  {
574  return s_anonymouslyHostedDynamicMethodsModule;
575  }
576  ConstructorInfo constructor = typeof(SecurityTransparentAttribute).GetConstructor(Type.EmptyTypes);
577  CustomAttributeBuilder item = new CustomAttributeBuilder(constructor, EmptyArray<object>.Value);
579  list.Add(item);
580  ConstructorInfo constructor2 = typeof(SecurityRulesAttribute).GetConstructor(new Type[1]
581  {
582  typeof(SecurityRuleSet)
583  });
584  CustomAttributeBuilder item2 = new CustomAttributeBuilder(constructor2, new object[1]
585  {
586  SecurityRuleSet.Level1
587  });
588  list.Add(item2);
589  AssemblyName name = new AssemblyName("Anonymously Hosted DynamicMethods Assembly");
590  StackCrawlMark stackMark = StackCrawlMark.LookForMe;
591  AssemblyBuilder assemblyBuilder = AssemblyBuilder.InternalDefineDynamicAssembly(name, AssemblyBuilderAccess.Run, null, null, null, null, null, ref stackMark, list, SecurityContextSource.CurrentAssembly);
592  AppDomain.PublishAnonymouslyHostedDynamicMethodsAssembly(assemblyBuilder.GetNativeHandle());
593  s_anonymouslyHostedDynamicMethodsModule = (InternalModuleBuilder)assemblyBuilder.ManifestModule;
594  }
595  return s_anonymouslyHostedDynamicMethodsModule;
596  }
597 
598  [SecurityCritical]
599  private void Init(string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] signature, Type owner, Module m, bool skipVisibility, bool transparentMethod, ref StackCrawlMark stackMark)
600  {
601  CheckConsistency(attributes, callingConvention);
602  if (signature != null)
603  {
604  m_parameterTypes = new RuntimeType[signature.Length];
605  for (int i = 0; i < signature.Length; i++)
606  {
607  if (signature[i] == null)
608  {
609  throw new ArgumentException(Environment.GetResourceString("Arg_InvalidTypeInSignature"));
610  }
611  m_parameterTypes[i] = (signature[i].UnderlyingSystemType as RuntimeType);
612  if (m_parameterTypes[i] == null || (object)m_parameterTypes[i] == null || m_parameterTypes[i] == (RuntimeType)typeof(void))
613  {
614  throw new ArgumentException(Environment.GetResourceString("Arg_InvalidTypeInSignature"));
615  }
616  }
617  }
618  else
619  {
620  m_parameterTypes = new RuntimeType[0];
621  }
622  m_returnType = ((returnType == null) ? ((RuntimeType)typeof(void)) : (returnType.UnderlyingSystemType as RuntimeType));
623  if (m_returnType == null || (object)m_returnType == null || m_returnType.IsByRef)
624  {
625  throw new NotSupportedException(Environment.GetResourceString("Arg_InvalidTypeInRetType"));
626  }
627  if (transparentMethod)
628  {
629  m_module = GetDynamicMethodsModule();
630  if (skipVisibility)
631  {
632  m_restrictedSkipVisibility = true;
633  }
634  m_creationContext = CompressedStack.Capture();
635  }
636  else
637  {
638  if (m != null)
639  {
640  m_module = m.ModuleHandle.GetRuntimeModule();
641  }
642  else
643  {
644  RuntimeType runtimeType = null;
645  if (owner != null)
646  {
647  runtimeType = (owner.UnderlyingSystemType as RuntimeType);
648  }
649  if (runtimeType != null)
650  {
651  if (runtimeType.HasElementType || runtimeType.ContainsGenericParameters || runtimeType.IsGenericParameter || runtimeType.IsInterface)
652  {
653  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidTypeForDynamicMethod"));
654  }
655  m_typeOwner = runtimeType;
656  m_module = runtimeType.GetRuntimeModule();
657  }
658  }
659  m_skipVisibility = skipVisibility;
660  }
661  m_ilGenerator = null;
662  m_fInitLocals = true;
663  m_methodHandle = null;
664  if (name == null)
665  {
666  throw new ArgumentNullException("name");
667  }
668  if (AppDomain.ProfileAPICheck)
669  {
670  if (m_creatorAssembly == null)
671  {
672  m_creatorAssembly = RuntimeAssembly.GetExecutingAssembly(ref stackMark);
673  }
674  if (m_creatorAssembly != null && !m_creatorAssembly.IsFrameworkAssembly())
675  {
676  m_profileAPICheck = true;
677  }
678  }
679  m_dynMethod = new RTDynamicMethod(this, name, attributes, callingConvention);
680  }
681 
682  [SecurityCritical]
683  private void PerformSecurityCheck(Module m, ref StackCrawlMark stackMark, bool skipVisibility)
684  {
685  if (m == null)
686  {
687  throw new ArgumentNullException("m");
688  }
689  ModuleBuilder moduleBuilder = m as ModuleBuilder;
690  RuntimeModule left = (!(moduleBuilder != null)) ? (m as RuntimeModule) : moduleBuilder.InternalModule;
691  if (left == null)
692  {
693  throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeModule"), "m");
694  }
695  if (left == s_anonymouslyHostedDynamicMethodsModule)
696  {
697  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidValue"), "m");
698  }
699  if (skipVisibility)
700  {
702  }
703  RuntimeType callerType = RuntimeMethodHandle.GetCallerType(ref stackMark);
704  m_creatorAssembly = callerType.GetRuntimeAssembly();
705  if (m.Assembly != m_creatorAssembly)
706  {
707  CodeAccessSecurityEngine.ReflectionTargetDemandHelper(PermissionType.SecurityControlEvidence, m.Assembly.PermissionSet);
708  }
709  }
710 
711  [SecurityCritical]
712  private void PerformSecurityCheck(Type owner, ref StackCrawlMark stackMark, bool skipVisibility)
713  {
714  if (owner == null)
715  {
716  throw new ArgumentNullException("owner");
717  }
718  RuntimeType runtimeType = owner as RuntimeType;
719  if (runtimeType == null)
720  {
721  runtimeType = (owner.UnderlyingSystemType as RuntimeType);
722  }
723  if (runtimeType == null)
724  {
725  throw new ArgumentNullException("owner", Environment.GetResourceString("Argument_MustBeRuntimeType"));
726  }
727  RuntimeType callerType = RuntimeMethodHandle.GetCallerType(ref stackMark);
728  if (skipVisibility)
729  {
731  }
732  else if (callerType != runtimeType)
733  {
735  }
736  m_creatorAssembly = callerType.GetRuntimeAssembly();
737  if (runtimeType.Assembly != m_creatorAssembly)
738  {
739  CodeAccessSecurityEngine.ReflectionTargetDemandHelper(PermissionType.SecurityControlEvidence, owner.Assembly.PermissionSet);
740  }
741  }
742 
749  [SecuritySafeCritical]
750  [ComVisible(true)]
751  public sealed override Delegate CreateDelegate(Type delegateType)
752  {
753  if (m_restrictedSkipVisibility)
754  {
755  GetMethodDescriptor();
756  RuntimeHelpers._CompileMethod(m_methodHandle);
757  }
758  MulticastDelegate multicastDelegate = (MulticastDelegate)Delegate.CreateDelegateNoSecurityCheck(delegateType, null, GetMethodDescriptor());
759  multicastDelegate.StoreDynamicMethod(GetMethodInfo());
760  return multicastDelegate;
761  }
762 
771  [SecuritySafeCritical]
772  [ComVisible(true)]
773  public sealed override Delegate CreateDelegate(Type delegateType, object target)
774  {
775  if (m_restrictedSkipVisibility)
776  {
777  GetMethodDescriptor();
778  RuntimeHelpers._CompileMethod(m_methodHandle);
779  }
780  MulticastDelegate multicastDelegate = (MulticastDelegate)Delegate.CreateDelegateNoSecurityCheck(delegateType, target, GetMethodDescriptor());
781  multicastDelegate.StoreDynamicMethod(GetMethodInfo());
782  return multicastDelegate;
783  }
784 
785  [SecurityCritical]
786  internal RuntimeMethodHandle GetMethodDescriptor()
787  {
788  if (m_methodHandle == null)
789  {
790  lock (this)
791  {
792  if (m_methodHandle == null)
793  {
794  if (m_DynamicILInfo != null)
795  {
796  m_DynamicILInfo.GetCallableMethod(m_module, this);
797  }
798  else
799  {
800  if (m_ilGenerator == null || m_ilGenerator.ILOffset == 0)
801  {
802  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_BadEmptyMethodBody", Name));
803  }
804  m_ilGenerator.GetCallableMethod(m_module, this);
805  }
806  }
807  }
808  }
809  return new RuntimeMethodHandle(m_methodHandle);
810  }
811 
814  public override string ToString()
815  {
816  return m_dynMethod.ToString();
817  }
818 
821  public override MethodInfo GetBaseDefinition()
822  {
823  return this;
824  }
825 
828  public override ParameterInfo[] GetParameters()
829  {
830  return m_dynMethod.GetParameters();
831  }
832 
836  {
837  return m_dynMethod.GetMethodImplementationFlags();
838  }
839 
852  [SecuritySafeCritical]
853  public override object Invoke(object obj, BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture)
854  {
855  if ((CallingConvention & CallingConventions.VarArgs) == CallingConventions.VarArgs)
856  {
857  throw new NotSupportedException(Environment.GetResourceString("NotSupported_CallToVarArg"));
858  }
859  RuntimeMethodHandle methodDescriptor = GetMethodDescriptor();
860  Signature signature = new Signature(m_methodHandle, m_parameterTypes, m_returnType, CallingConvention);
861  int num = signature.Arguments.Length;
862  int num2 = (parameters != null) ? parameters.Length : 0;
863  if (num != num2)
864  {
865  throw new TargetParameterCountException(Environment.GetResourceString("Arg_ParmCnt"));
866  }
867  object obj2 = null;
868  if (num2 > 0)
869  {
870  object[] array = CheckArguments(parameters, binder, invokeAttr, culture, signature);
871  obj2 = RuntimeMethodHandle.InvokeMethod(null, array, signature, constructor: false);
872  for (int i = 0; i < array.Length; i++)
873  {
874  parameters[i] = array[i];
875  }
876  }
877  else
878  {
879  obj2 = RuntimeMethodHandle.InvokeMethod(null, null, signature, constructor: false);
880  }
881  GC.KeepAlive(this);
882  return obj2;
883  }
884 
892  public override object[] GetCustomAttributes(Type attributeType, bool inherit)
893  {
894  return m_dynMethod.GetCustomAttributes(attributeType, inherit);
895  }
896 
901  public override object[] GetCustomAttributes(bool inherit)
902  {
903  return m_dynMethod.GetCustomAttributes(inherit);
904  }
905 
912  public override bool IsDefined(Type attributeType, bool inherit)
913  {
914  return m_dynMethod.IsDefined(attributeType, inherit);
915  }
916 
925  public ParameterBuilder DefineParameter(int position, ParameterAttributes attributes, string parameterName)
926  {
927  if (position < 0 || position > m_parameterTypes.Length)
928  {
929  throw new ArgumentOutOfRangeException(Environment.GetResourceString("ArgumentOutOfRange_ParamSequence"));
930  }
931  position--;
932  if (position >= 0)
933  {
934  ParameterInfo[] array = m_dynMethod.LoadParameters();
935  array[position].SetName(parameterName);
936  array[position].SetAttributes(attributes);
937  }
938  return null;
939  }
940 
943  [SecuritySafeCritical]
945  {
946  new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();
947  if (m_DynamicILInfo != null)
948  {
949  return m_DynamicILInfo;
950  }
951  return GetDynamicILInfo(new DynamicScope());
952  }
953 
954  [SecurityCritical]
955  internal DynamicILInfo GetDynamicILInfo(DynamicScope scope)
956  {
957  if (m_DynamicILInfo == null)
958  {
959  byte[] signature = SignatureHelper.GetMethodSigHelper(null, CallingConvention, ReturnType, null, null, m_parameterTypes, null, null).GetSignature(appendEndOfSig: true);
960  m_DynamicILInfo = new DynamicILInfo(scope, this, signature);
961  }
962  return m_DynamicILInfo;
963  }
964 
968  {
969  return GetILGenerator(64);
970  }
971 
975  [SecuritySafeCritical]
976  public ILGenerator GetILGenerator(int streamSize)
977  {
978  if (m_ilGenerator == null)
979  {
980  byte[] signature = SignatureHelper.GetMethodSigHelper(null, CallingConvention, ReturnType, null, null, m_parameterTypes, null, null).GetSignature(appendEndOfSig: true);
981  m_ilGenerator = new DynamicILGenerator(this, signature, streamSize);
982  }
983  return m_ilGenerator;
984  }
985 
986  internal MethodInfo GetMethodInfo()
987  {
988  return m_dynMethod;
989  }
990  }
991 }
DynamicMethod(string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, Module m, bool skipVisibility)
Creates a dynamic method that is global to a module, specifying the method name, attributes,...
Performs reflection on a module.
Definition: Module.cs:17
sealed override Delegate CreateDelegate(Type delegateType)
Completes the dynamic method and creates a delegate that can be used to execute it.
Discovers the attributes of a parameter and provides access to parameter metadata.
MethodAttributes
Specifies flags for method attributes. These flags are defined in the corhdr.h file.
DynamicMethod(string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, Type owner, bool skipVisibility)
Creates a dynamic method, specifying the method name, attributes, calling convention,...
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Describes a set of security permissions applied to code. This class cannot be inherited.
DynamicMethod(string name, Type returnType, Type[] parameterTypes, Type owner)
Creates a dynamic method, specifying the method name, return type, parameter types,...
DynamicMethod(string name, Type returnType, Type[] parameterTypes, Module m)
Creates a dynamic method that is global to a module, specifying the method name, return type,...
object [] GetCustomAttributes(Type attributeType, bool inherit)
Returns an array of custom attributes defined on this member, identified by type, or an empty array i...
ReflectionPermissionFlag
Specifies the permitted use of the N:System.Reflection and N:System.Reflection.Emit namespaces.
Defines and represents a dynamic method that can be compiled, executed, and discarded....
MethodImplAttributes
Specifies flags for the attributes of a method implementation.
Discovers the attributes of a method and provides access to method metadata.
Definition: MethodInfo.cs:13
Specifies that an assembly cannot cause an elevation of privilege.
override RuntimeMethodHandle MethodHandle
Not supported for dynamic methods.
byte [] GetSignature()
Adds the end token to the signature and marks the signature as finished, so no further tokens can be ...
Represents a multicast delegate; that is, a delegate that can have more than one element in its invoc...
BindingFlags
Specifies flags that control binding and the way in which the search for members and types is conduct...
Definition: BindingFlags.cs:10
Definition: __Canon.cs:3
The exception that is thrown when the value of an argument is outside the allowable range of values a...
static void KeepAlive(object obj)
References the specified object, which makes it ineligible for garbage collection from the start of t...
Definition: GC.cs:267
override Type ReflectedType
Gets the class that was used in reflection to obtain the method.
SecurityContextSource
Identifies the source for the security context.
CallingConvention
Specifies the calling convention required to call methods implemented in unmanaged code.
Provides methods for building signatures.
override ParameterInfo ReturnParameter
Gets the return parameter of the dynamic method.
AssemblyBuilderAccess
Defines the access modes for a dynamic assembly.
override MethodAttributes Attributes
Gets the attributes specified when the dynamic method was created.
override ParameterInfo [] GetParameters()
Returns the parameters of the dynamic method.
Creates or associates parameter information.
override Module Module
Gets the module with which the dynamic method is logically associated.
DynamicMethod(string name, Type returnType, Type[] parameterTypes, Module m, bool skipVisibility)
Creates a dynamic method that is global to a module, specifying the method name, return type,...
bool IsDefined(Type attributeType, bool inherit)
Indicates whether one or more instance of attributeType is defined on this member.
Generates Microsoft intermediate language (MSIL) instructions.
Definition: ILGenerator.cs:12
CallingConventions
Defines the valid calling conventions for a method.
Indicates the set of security rules the common language runtime should enforce for an assembly.
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
override bool IsDefined(Type attributeType, bool inherit)
Indicates whether the specified custom attribute type is defined.
T:System.RuntimeMethodHandle is a handle to the internal metadata representation of a method.
override string Name
Gets the name of the dynamic method.
DynamicMethod(string name, Type returnType, Type[] parameterTypes)
Initializes an anonymously hosted dynamic method, specifying the method name, return type,...
override MethodInfo GetBaseDefinition()
Returns the base implementation for the method.
ILGenerator GetILGenerator(int streamSize)
Returns a Microsoft intermediate language (MSIL) generator for the method with the specified MSIL str...
ILGenerator GetILGenerator()
Returns a Microsoft intermediate language (MSIL) generator for the method with a default MSIL stream ...
Specifies the details of how a method is implemented. This class cannot be inherited.
override MethodImplAttributes GetMethodImplementationFlags()
Returns the implementation flags for the method.
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition: Array.cs:17
Represents a delegate, which is a data structure that refers to a static method or to a class instanc...
Definition: Delegate.cs:15
Represents type declarations: class types, interface types, array types, value types,...
Definition: Type.cs:18
sealed override Delegate CreateDelegate(Type delegateType, object target)
Completes the dynamic method and creates a delegate that can be used to execute it,...
ParameterAttributes
Defines the attributes that can be associated with a parameter. These are defined in CorHdr....
override bool IsSecuritySafeCritical
Gets a value that indicates whether the current dynamic method is security-safe-critical at the curre...
MethodImplOptions Value
Gets the T:System.Runtime.CompilerServices.MethodImplOptions value describing the attributed method.
MethodImplOptions
Defines the details of how a method is implemented.
DynamicMethod(string name, Type returnType, Type[] parameterTypes, Type owner, bool skipVisibility)
Creates a dynamic method, specifying the method name, return type, parameter types,...
Selects a member from a list of candidates, and performs type conversion from actual argument type to...
Definition: Binder.cs:10
Controls the system garbage collector, a service that automatically reclaims unused memory.
Definition: GC.cs:11
override Type ReturnType
Gets the type of return value for the dynamic method.
SecurityRuleSet
Identifies the set of security rules the common language runtime should enforce for an assembly.
The exception that is thrown when one of the arguments provided to a method is not valid.
void Demand()
Forces a T:System.Security.SecurityException at run time if all callers higher in the call stack have...
static CompressedStack Capture()
Captures the compressed stack from the current thread.
ParameterBuilder DefineParameter(int position, ParameterAttributes attributes, string parameterName)
Defines a parameter of the dynamic method.
override Type DeclaringType
Gets the type that declares the method, which is always null for dynamic methods.
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
override object [] GetCustomAttributes(bool inherit)
Returns all the custom attributes defined for the method.
override string ToString()
Returns the signature of the method, represented as a string.
override ICustomAttributeProvider ReturnTypeCustomAttributes
Gets the custom attributes of the return type for the dynamic method.
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition: List.cs:14
The exception that is thrown when the number of parameters for an invocation does not match the numbe...
override object [] GetCustomAttributes(Type attributeType, bool inherit)
Returns the custom attributes of the specified type that have been applied to the method.
DynamicMethod(string name, Type returnType, Type[] parameterTypes, bool restrictedSkipVisibility)
Initializes an anonymously hosted dynamic method, specifying the method name, return type,...
Provides support for alternative ways to generate the Microsoft intermediate language (MSIL) and meta...
Definition: DynamicILInfo.cs:8
Provides methods for setting and capturing the compressed stack on the current thread....
override object Invoke(object obj, BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture)
Invokes the dynamic method using the specified parameters, under the constraints of the specified bin...
The exception that is thrown when a method call is invalid for the object's current state.
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...
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition: CultureInfo.cs:16
The exception that is thrown when an invoked method is not supported, or when there is an attempt to ...
bool InitLocals
Gets or sets a value indicating whether the local variables in the method are zero-initialized.
override bool IsSecurityTransparent
Gets a value that indicates whether the current dynamic method is transparent at the current trust le...
SecurityPermissionFlag
Specifies access flags for the security permission object.
DynamicILInfo GetDynamicILInfo()
Returns a T:System.Reflection.Emit.DynamicILInfo object that can be used to generate a method body fr...
Controls access to non-public types and members through the N:System.Reflection APIs....
virtual bool IsAssignableFrom(Type c)
Determines whether an instance of a specified type can be assigned to an instance of the current type...
Definition: Type.cs:2707
Provides custom attributes for reflection objects that support them.
Provides a set of static methods and properties that provide support for compilers....
override bool IsSecurityCritical
Gets a value that indicates whether the current dynamic method is security-critical or security-safe-...